函数原型:
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socketpair(int domain, int type, int protocol, int sv[2]);
函数功能描述:
socketpair函数创建两个随后连接起来的套接字,在nginx中master进程与worker进程就是通过socketpair创建的全双工流管道来通信的。
例子:
include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/socket.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int channel[2];
pid_t pid;
int nbytes = 0;
char buff[1024];
char string[1024] = "hello,world";
if(socketpair(AF_UNIX,SOCK_STREAM,0,channel) == -1)
{
printf("create unnamed socket pair failed:%s\n",strerror(errno) );
exit(-1);
}
pid = fork();
if (pid < 0)
{
perror("fork erro