加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

管道通信——Pipeline communication

发布时间:2020-12-13 19:40:54 所属栏目:百科 来源:网络整理
导读://using pipe the communication between f proccess and c proc // // #include stdio.h #include stdlib.h #include unistd.h #include string.h #include sys/types.h #include sys/wait.h #define BUFSIZE1024 int main() { int ret,retw; pid_t pid; ch

//using pipe the communication between f proccess and c proc
//
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUFSIZE1024

int main()
{
int ret,retw;
pid_t pid;
char buf[BUFSIZE] = {''};
int pd[2];

// pipe() creates a pair of file descriptors,pointing to a pipe inode,and places them in the
// array pointed to by filedes. filedes[0] is for reading,filedes[1] is for writing.

if(pipe(pd) < 0)
{
perror("pipe()");
exit(1);
}

pid = fork();
if(pid < 0)
{
perror("fork()");
exit(1);
}

if(pid == 0)//child read
{
close(pd[1]);//prevent the error
ret = read(pd[0],buf,BUFSIZE);
puts(buf);
close(pd[0]);
exit(0);
}
else// parent write
{
close(pd[0]);
retw = write(pd[1],"Hello",5);
close(pd[1]);
wait(NULL);
exit(0);
}

exit(0); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读