15-5 匿名管道进程间通信的原理
https://blog.csdn.net/gjy_it/article/details/104511556
15-6 实战通过socketpair进行进程间通信

socketpair.c
#include<stdio.h>#include<string.h>#include<unistd.h>#include<sys/socket.h>int main(){int sv[2]; //一对无名的套接字描述符if(socketpair(PF_LOCAL,SOCK_STREAM,0,sv) < 0) //成功返回零 失败返回-1{perror("socketpair");return 0;}pid_t id = fork(); //fork出子进程if(id == 0) //子进程{const char* msg = "I'm children!\n";char buf[1024];close(sv[1]);while(1){write(sv[0],msg,strlen(msg));sleep(1);ssize_t _s = read(sv[0],buf,sizeof(buf)-1);if(_s > 0){buf[_s] = '\0';printf("parent say to children : %s\n",buf);}}} else { // parent父进程const char* msg = "I'm father!\n";char buf[1024];close(sv[0]);//关闭写端口while(1){ssize_t _s = read(sv[1],buf,sizeof(buf)-1);if(_s > 0){buf[_s] = '\0';printf("Children say to parent : %s\n",buf);usleep(1);}write(sv[1],msg,strlen(msg));}}return 0;}

可以看到来回打印子进程和父进程的内容。



