设为首页 加入收藏

TOP

用socketpair()进行进程间的全双工通讯
2014-11-24 02:01:53 来源: 作者: 【 】 浏览:0
Tags:socketpair 进行 进程 全双工 通讯

用socketpair()进行进程间的全双工通讯


/*************************************************
* description: use socketpair() to implete IPC(全双工的IPC)
* author: chengshuguang
**************************************************/
#include
#include
#include


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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++11新特性:自动类型推断和类型.. 下一篇C语言在Linux下递归遍历文件夹中..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: