socketpair is a function used to create a pair of sockets connected to each other. It is widely used in Unix systems for inter-process communication (IPC) within the same process. The prototype of its function is "int socketpair(int domain, int type, int protocol, int sv[2]);". The socket created by this function can realize two-way communication between processes and can be used to implement functions such as data transmission, synchronization and notification between processes.
socketpair is a function used to create a pair of sockets connected to each other. It is widely used in Unix systems for inter-process communication (IPC) within the same process.
The prototype of the socketpair function is as follows:
int socketpair(int domain, int type, int protocol, int sv[2]);
Parameter description:
- domain: specifies the protocol family of the socket, usually AF_UNIX.
- type: Specifies the type of socket, usually SOCK_STREAM (stream socket) or SOCK_DGRAM (datagram socket).
- protocol: Specifies the protocol of the socket, usually 0.
- sv: The file descriptor used to store the created socket.
The return value of the socketpair function is 0 for success, and -1 for failure.
Use the socketpair function to create a pair of connected sockets, which can be used for inter-process communication. One of the sockets in the pair is used to read data and the other is used to write data. In the same process, you can use this pair of sockets for inter-process communication to realize data transmission between processes.
The following is an example of using the socketpair function for inter-process communication:
#include#include #include #include #include int main() { int sv[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) { perror("socketpair"); exit(1); } pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // 子进程 close(sv[0]); // 关闭子进程中的读取套接字 char *message = "Hello from child process!"; if (write(sv[1], message, strlen(message)) == -1) { perror("write"); exit(1); } close(sv[1]); // 关闭子进程中的写入套接字 exit(0); } else { // 父进程 close(sv[1]); // 关闭父进程中的写入套接字 char buffer[1024]; if (read(sv[0], buffer, sizeof(buffer)) == -1) { perror("read"); exit(1); } printf("Message from child process: %s\n", buffer); close(sv[0]); // 关闭父进程中的读取套接字 exit(0); } }
In this example, the socketpair function is first called to create a pair of sockets, and then a pair is created through the fork function. child process. In the child process, the read socket is closed, and then the write function is used to write data to the write socket. In the parent process, the write socket is closed, and then the read function is used to read data from the read socket.
The socket created through the socketpair function can realize two-way communication between processes, and can be used to implement functions such as data transmission, synchronization and notification between processes. In practical applications, the socketpair function can be used for inter-process communication according to specific needs.
The above is the detailed content of What is socketpair usage?. For more information, please follow other related articles on the PHP Chinese website!