struct sigevent
The <signal.h> header shall define the sigevent structure, which shall include at least the following members:
struct sigevent { int sigev_notify; //Notification type. int sigev_signo; //Signal number. union sigval sigev_value; //Signal value. void (*sigev_notify_function)(union sigval); //Notification function. pthread_attr_t *sigev_notify_attributes; //Notification attributes. };
sigev_notify
sigev_notify 的取值范圍如下,只有3種情況(對應的宏在<signal.h>中定義)。
- SIGEV_NONE
- 事件發生時,什么也不做.
- SIGEV_SIGNAL
- 事件發生時,將 sigev_signo 指定的信號(A queued signal)發送給指定的進程.
- SIGEV_THREAD
- 事件發生時,內核會(在此進程內)以sigev_notification_attributes為線程屬性創建一個線程,並且讓它執行sigev_notify_function,
- 傳入sigev_value作為為一個參數.
sigev_signo
在sigev_notify = SIGEV_SIGNAL 時使用,指定信號的種別(number).
sigev_value
在sigev_notify = SIGEV_THREAD 時使用,作為sigev_notify_function 的參數.
union sigval { int sival_int; void *sival_ptr; };
(*sigev_notify_function)(union sigval)
函數指針(指向通知執行函數),在sigev_notify = SIGEV_THREAD 時使用, 其他情況下置為NULL.
sigev_notify_attributes
指向線程屬性的指針,在sigev_notify = SIGEV_THREAD 時使用,指定創建線程的屬性, 其他情況下置為NULL.
strcut timespec
The <time.h> header shall declare the timespec structure, which shall include at least the following members:
struct timespec { time_t tv_sec; /* Seconds */ long tv_nsec; /* Nanoseconds(納秒:十億分之一秒) */ };
struct itimerspec
The <time.h> header shall also declare the itimerspec structure, which shall include at least the following members:
struct itimerspec { struct timespec it_interval; /* Timer interval(timer循環時間間隔) */ struct timespec it_value; /* Initial expiration(timer初次到期時間間隔) */ };
clockid_t
clockid_t is used for clock ID type in the clock and timer functions, 取值范圍如下(前4個是POSIX定義的,灰色部分為Linux的擴展),
/* Identifier for system-wide realtime clock, Setting this clock requires appropriate privileges */ #define CLOCK_REALTIME 0 /* Monotonic system-wide clock, Clock that cannot be set and represents monotonic time since some unspecified starting point */ #define CLOCK_MONOTONIC 1 /* High-resolution timer from the CPU. (since Linux 2.6.12) */ #define CLOCK_PROCESS_CPUTIME_ID 2 /* Thread-specific CPU-time clock. (since Linux 2.6.12) */ #define CLOCK_THREAD_CPUTIME_ID 3 /* Monotonic system-wide clock, not adjusted for frequency scaling. */ #define CLOCK_MONOTONIC_RAW 4 /* Identifier for system-wide realtime clock, updated only on ticks. */ #define CLOCK_REALTIME_COARSE 5 /* Monotonic system-wide clock, updated only on ticks. */ #define CLOCK_MONOTONIC_COARSE 6
CLOCK_REALTIME : 這種時鍾表示的是絕對時間, 指的是從1970年1月1月0:00到目前經過多少秒, 相當於你的linux系統中顯示的時間, 所以這個時間是可以更改的, 當系統的時鍾源被改變,或者系統管理員重置了系統時間之后,這種類型的時鍾可以得到相應的調整, 對設定為此類型的timer是有影響的.
CLOCK_MONOTONIC : 這種時鍾表示的是相對時間, 其值對通過累積時鍾節拍(嘀嗒)計算出來的, 不受時鍾源等的影響, 從系統啟動這一刻起開始計時, 如果你想計算出在一台計算機上不受重啟的影響,兩個事件發生的間隔時間的話,那么它將是最好的選擇。
CLOCK_PROCESS_CPUTIME_ID : 測量調用進程(包括該進程內所有的線程)用戶和系統消耗的總CPU時間.
CLOCK_THREAD_CPUTIME_ID : 測量調用線程消耗的CPU時間.
struct timeval
The <sys/time.h> header shall define the timeval structure, which shall include at least the following members:
struct timeval { time_t tv_sec; // Seconds(秒). suseconds_t tv_usec; // Microseconds(微秒:千分之一毫秒). };
struct itimerval
The <sys/time.h> header shall define the itimerval structure, which shall include at least the following members:
struct itimerval { struct timeval it_interval; /* Timer interval(timer循環時間間隔) */ struct timeval it_value; /* Initial expiration(timer初次到期時間間隔) */ };
strcut tm
The <time.h> header shall declare the tm structure, which shall include at least the following members:
struct tm { int tm_sec //Seconds [0,60]. 60 is used for leap seconds. int tm_min //Minutes [0,59]. int tm_hour //Hour [0,23]. int tm_mday //Day of month [1,31]. int tm_mon //Month of year [0,11]. int tm_year //Years since 1900. int tm_wday //Day of week [0,6] (Sunday =0). int tm_yday //Day of year [0,365]. The number of days since January 1 int tm_isdst //Daylight Savings flag(夏時令) };
注意: 1)使用localtime等函數取得tm結構體,表示當前時間時, 年數要加1900,月數要加1;因為tm_year是1900的偏移量,tm_mon是從0開始計算的.
2)tm_isdst是夏時令的標志, 大於0表示時間使用了夏時令, 0表示未使用夏時令,小於0表示沒有相關信息.
struct sockaddr
The <sys/socket.h> header shall define the sockaddr structure, which shall include at least the following members:
struct sockaddr { sa_family_t sa_family; //Address family. char sa_data[14]; // Socket address (variable-length data). };
The sockaddr structure is used to define a socket address which is used in the bind(), connect(), getpeername(), getsockname(), recvfrom(), and sendto() functions.
struct sockaddr_in
The <netinet/in.h> header shall define the sockaddr_in structure, which shall include at least the following members:
typedef uint32_t in_addr_t; /* Internet address. */ struct in_addr { in_addr_t s_addr; }; struct sockaddr_in { sa_family_t sin_family; //AF_INET. in_port_t sin_port; //Port number(網絡字節序). struct in_addr sin_addr; //IP address(網絡字節序). };
The sockaddr_in structure is used to store addresses for the Internet address family. Pointers to this type shall be cast by applications to struct sockaddr * for use with socket functions.
struct hostent
The <netdb.h> header shall define the hostent structure, which shall include at least the following members:
struct hostent { char *h_name; //Official name of the host. char **h_aliases; //主機的別名,可以有多個,最一個是空指針 int h_addrtype; //Address type.always AF_INET or AF_INET6 at present. int h_length; //地址的長度(IPv4的長度為4字節). char **h_addr_list; //IP地址,可以有多個,最后一個是空指針(網絡字節序) }; #define h_addr h_addr_list[0] /* for backward compatibility */ /* 注意事項
(1)h_aliases 和 h_addr_list 的用法見例子:http://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html (2)h_addr_list中存儲的IP地址格式為點分10進制,因為是網絡字節序,不能用printf直接打印出來,需要使用inet_ntop函數來轉換 */
結構圖示:

Ip Protocol
/* Standard well-defined IP protocols. */ enum { IPPROTO_IP = 0, /* Dummy protocol for TCP. */ IPPROTO_ICMP = 1, /* Internet Control Message Protocol. */ IPPROTO_IGMP = 2, /* Internet Group Management Protocol. */ IPPROTO_IPIP = 4, /* IPIP tunnels (older KA9Q tunnels use 94). */ IPPROTO_TCP = 6, /* Transmission Control Protocol. */ IPPROTO_EGP = 8, /* Exterior Gateway Protocol. */ IPPROTO_PUP = 12, /* PUP protocol. */ IPPROTO_UDP = 17, /* User Datagram Protocol. */ IPPROTO_IDP = 22, /* XNS IDP protocol. */ IPPROTO_TP = 29, /* SO Transport Protocol Class 4. */ IPPROTO_DCCP = 33, /* Datagram Congestion Control Protocol. */ IPPROTO_IPV6 = 41, /* IPv6 header. */ IPPROTO_RSVP = 46, /* Reservation Protocol. */ IPPROTO_GRE = 47, /* General Routing Encapsulation. */ IPPROTO_ESP = 50, /* encapsulating security payload. */ IPPROTO_AH = 51, /* authentication header. */ IPPROTO_MTP = 92, /* Multicast Transport Protocol. */ IPPROTO_BEETPH = 94, /* IP option pseudo header for BEET. */ IPPROTO_ENCAP = 98, /* Encapsulation Header. */ IPPROTO_PIM = 103, /* Protocol Independent Multicast. */ IPPROTO_COMP = 108, /* Compression Header Protocol. */ IPPROTO_SCTP = 132, /* Stream Control Transmission Protocol. */ IPPROTO_UDPLITE = 136, /* UDP-Lite protocol. */ IPPROTO_RAW = 255, /* Raw IP packets. */ IPPROTO_MAX };
struct addrinfo
The <netdb.h> header shall define the addrinfo structure, which shall include at least the following members:
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> /* ======================Types of sockets====================== */ enum __socket_type { SOCK_STREAM = 1, /* Sequenced, reliable, connection-based byte streams. */ SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams of fixed maximum length. */ SOCK_RAW = 3, /* Raw protocol interface. */ SOCK_RDM = 4, /* Reliably-delivered messages. */ SOCK_SEQPACKET = 5, /* Sequenced, reliable, connection-based,datagrams of fixed maximum length. */ SOCK_DCCP = 6, /* Datagram Congestion Control Protocol. */ SOCK_PACKET = 10, /* Linux specific way of getting packets at the dev level. For writing rarp and other similar things on the user level. */ /* Flags to be ORed into the type parameter of socket and socketpair and used for the flags parameter of paccept. */ SOCK_CLOEXEC = 02000000, /* Atomically set close-on-exec flag for the new descriptor(s). */ SOCK_NONBLOCK = 00004000 /* Atomically mark descriptor(s) as non-blocking. */ }; /* ============Protocol families(只列出常用幾個)================= */ #define PF_UNSPEC 0 /* Unspecified. */ #define PF_LOCAL 1 /* Local to host (pipes and file-domain). */ #define PF_INET 2 /* IP protocol family. */ #define PF_IPX 4 /* Novell Internet Protocol. */ #define PF_APPLETALK 5 /* Appletalk DDP. */ #define PF_INET6 10 /* IP version 6. */ #define PF_TIPC 30 /* TIPC sockets. */ #define PF_BLUETOOTH 31 /* Bluetooth sockets. */ /* ==============Address families(只列出常用幾個)================= */ #define AF_UNSPEC PF_UNSPEC #define AF_LOCAL PF_LOCAL #define AF_UNIX PF_UNIX #define AF_FILE PF_FILE #define AF_INET PF_INET #define AF_IPX PF_IPX #define AF_APPLETALK PF_APPLETALK #define AF_INET6 PF_INET6 #define AF_ROSE PF_ROSE #define AF_NETLINK PF_NETLINK #define AF_TIPC PF_TIPC #define AF_BLUETOOTH PF_BLUETOOTH /* ====Possible values for `ai_flags' field in `addrinfo' structure.===== */ #define AI_PASSIVE 0x0001 /* Socket address is intended for `bind'. */ #define AI_CANONNAME 0x0002 /* Request for canonical name. */ #define AI_NUMERICHOST 0x0004 /* Don't use name resolution. */ #define AI_V4MAPPED 0x0008 /* IPv4 mapped addresses are acceptable. */ #define AI_ALL 0x0010 /* Return IPv4 mapped and IPv6 addresses. */ #define AI_ADDRCONFIG 0x0020 /* Use configuration of this host to choose returned address type. */ #ifdef __USE_GNU #define AI_IDN 0x0040 /* IDN encode input (assuming it is encoded in the current locale's character set) before looking it up. */ #define AI_CANONIDN 0x0080 /* Translate canonical name from IDN format. */ #define AI_IDN_ALLOW_UNASSIGNED 0x0100 /* Don't reject unassigned Unicode code points. */ #define AI_IDN_USE_STD3_ASCII_RULES 0x0200 /* Validate strings according to STD3 rules. */ #endif #define AI_NUMERICSERV 0x0400 /* Don't use name resolution. */ /* =======================struct addrinfo======================= */ struct addrinfo { int ai_flags; /* 附加選項,多個選項可以使用或操作結合 */ int ai_family; /* 指定返回地址的協議簇,取值范圍:AF_INET(IPv4)、AF_INET6(IPv6)、AF_UNSPEC(IPv4 and IPv6) */ int ai_socktype; /* enum __socket_type 類型,設置為0表示任意類型 */ int ai_protocol; /* 協議類型,設置為0表示任意類型,具體見上一節的 Ip Protocol */ socklen_t ai_addrlen; /* socket address 的長度 */ struct sockaddr *ai_addr; /* socket address 的地址 */ char *ai_canonname; /* Canonical name of service location. */ struct addrinfo *ai_next; /* 指向下一條信息,因為可能返回多個地址 */ };
