用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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值