温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[Linux管道和IPC]在兄弟进程中使用管道

发布时间:2020-07-15 16:11:21 来源:网络 阅读:317 作者:银河星君 栏目:编程语言
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <limits.h> #include <string.h> #include <errno.h> #define BUFSIZE 4096  //定义一个最大的读写空间 int main(void) {     int fd[2];     char buf[BUFSIZE] = "hello!I am your  brother!\n";        // 缓冲区     pid_t pid;     int len;     if ( (pipe(fd)) < 0 )           //创建管道      {         perror("pipe failed\n");      }     if ( (pid = fork()) < 0 )        //创建第一个子进程      {         perror("fork failed\n");      }     else if ( pid == 0 )            //子进程     {         close ( fd[0] );                //关闭不使用的文件描述符         write(fd[1], buf, strlen(buf));   //发送字符串         exit(0);      }     if ( (pid = fork()) < 0 )             //创建第二个子进程      {         perror("fork failed\n");      }     else if ( pid > 0 )             //父进程     {         close ( fd[0] );         close ( fd[1] );         exit ( 0 );      }     else                       //第二个子进程中     {         close ( fd[1] );                  //关闭管道文件描述符         len = read (fd[0], buf, BUFSIZE);  //读取消息         write(STDOUT_FILENO, buf, len); //将消息输出到标准输出         exit(0);      }     return 0; }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI