用socketpair()进行进程间的全双工通讯
/*************************************************
* description: use socketpair() to implete IPC(全双工的IPC)
* author: chengshuguang
**************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#define child "wo shi child fa lai de"
#define parent "wo shi parent fa lai de"
int main()
{
int fd[2];
int ret;
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
pid_t pid;
pid = fork();
printf("here\n");
if(pid == 0)
{
char buf[20];
close(fd[0]);
read(fd[1],buf,20);
printf("child: %s\n",buf);
write(fd[1],child,sizeof(child));
close(fd[1]);
}
else
{
char buf[20];
close(fd[1]);
write(fd[0],parent,sizeof(child));
read(fd[0],buf,20);
printf("parent: %s\n",buf);
close(fd[0]);
}
sleep(10);
return 0;
}