[转]POSIX-Data Structure

Posted Jun 26, 20209 min read

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

The value range of sigev_notify is as follows, there are only 3 cases(the corresponding macro is defined in <signal.h>).

1.SIGEV_NONE

When the event occurs, do nothing.

2.SIGEV_SIGNAL

When an event occurs, send the signal(A queued signal) specified by _sigev_signo to the specified process.

3.SIGEV_THREAD

When an event occurs, the kernel will(in this process) create a thread with _sigev_notification_attributes_ as the thread attribute, and let it execute _sigev_notify_function_, pass in _sigev_value_ as As a parameter.

sigev_signo

Used when _sigev_notify = SIGEV_SIGNAL_, specify the signal type(number).

sigev_value

Used when _sigev_notify = SIGEV_THREAD_, as a parameter of _sigev_notify_function__._

union sigval
{
    int sival_int;
    void *sival_ptr;
};

(*sigev_notify_function)(union sigval)

The function pointer(pointing to the notification execution function) is used when _sigev_notify = SIGEV_THREAD_, otherwise it is set to NULL.

sigev_notify_attributes

The pointer to the thread attribute, used when _sigev_notify = SIGEV_THREAD_, specifies the attribute of the thread created, otherwise set to NULL.

struct 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(nanoseconds:one billionth of a second) */
};

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 cycle interval) */
    struct timespec it_value; /* Initial expiration(timer first expiration time interval) */
};

clockid_t

clockid_t is used for clock ID type in the clock and timer functions, the value range is as follows(The first 4 are defined by POSIX, the gray part is the extension of 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:This clock represents absolute time, which refers to how many seconds have passed from January 1, 1970, 0:00 to the present, which is equivalent to the time displayed in your Linux system, so this time It can be changed. When the system clock source is changed, or the system administrator resets the system time, this type of clock can be adjusted accordingly, which has an effect on the timer set to this type.

CLOCK_MONOTONIC:This clock represents relative time, its value is calculated by accumulating clock ticks(tick), and is not affected by the clock source, etc., and starts counting from the moment the system starts, If you want to calculate the interval between two events that is not affected by a restart on a computer, then it will be the best choice.

CLOCK_PROCESS_CPUTIME_ID:measure the total CPU time consumed by the user and the system of the calling process(including all threads in the process).

CLOCK_THREAD_CPUTIME_ID:Measures the CPU time consumed by the calling thread.

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(seconds).
    suseconds_t tv_usec;   //Microseconds(microseconds:one thousandth of a millisecond).
};

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 cycle interval) */
    struct timeval it_value; /* Initial expiration(timer first expiration time interval) */
};

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(Summer Time)
};

note:

1) Use localtime and other functions to obtain the tm structure, indicating that at the current time, the number of years should be increased by 1900, and the number of months should be increased by 1; because tm\_year is the offset of 1900, tm\_mon is calculated from 0.

2) tm_isdst is a sign of daylight saving time.A value greater than 0 indicates that daylight saving time is used, 0 indicates that daylight saving time is not used, and less than 0 indicates no relevant information.

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\()]( http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html ), _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(network byte order).
    struct in_addr sin_addr; //IP address(network byte order).
};

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; //The alias of the host, there can be multiple, the most is a null pointer
    int h_addrtype; //Address type.always AF_INET or AF_INET6 at present.
    int h_length; //The length of the address(IPv4 length is 4 bytes).
    char **h_addr_list; //IP address, there can be multiple, the last one is a null pointer(network byte order)
};
#define h_addr h_addr_list[0]/* for backward compatibility */

/* Precautions

(1) See the example for the usage of h_aliases and h_addr_list: http://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html
(2) The format of the IP address stored in h_addr_list is dotted decimal, because it is a network byte order, it cannot be printed directly using printf, and it needs to be converted using the inet_ntop function
*/

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 theaddrinfostructure, 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(only listed a few commonly used) ================= */
#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(only list a few commonly used) ================= */
#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; /* Additional options, multiple options can be used or combined */
int ai_family; /* Specify the protocol cluster of the return address, the value range:AF_INET(IPv4), AF_INET6(IPv6), AF_UNSPEC(IPv4 and IPv6) */
int ai_socktype; /* enum __socket_type type, set to 0 means any type */
int ai_protocol; /* Protocol type, set to 0 means any type, see Ip Protocol */in the previous section for details
socklen_t ai_addrlen; /* length of socket address */
struct sockaddr *ai_addr; /* address of socket address */
char *ai_canonname; /* Canonical name of service location. */
struct addrinfo *ai_next; /* points to the next message, because multiple addresses may be returned */
};