c/c++语言开发共享Linux在c中使用管道传递父进程和子进程之间的值?

进程创建子进程,将x增加1,然后将x值发送到子进程,然后子进程可以将x乘以10,然后将x传递回父进程,依此类推。 你应该至少这样做5次。

输出应如下所示:

初始值0

家长:

操作后的x值:1

儿童:

操作后的x值:10

家长:

操作后的x值:11

儿童:

操作后的x值:110

家长:

操作后的x值:111

儿童

操作后的x值:1110

我所拥有的是以下…….

#include  #include  #include  #include  #define READ 0 #define WRITE 1 int main() { pid_t pid; int mypipefd[2]; int ret; int i; int x=0; int result,result2; ret = pipe(mypipefd); if(ret ==-1) //test for pipe { perror("pipe"); //show error and exit exit(1); } printf("initial value %dn", x); pid = fork(); for(i=0;i 0) { /*Parent process*/ result=x++; write(mypipefd[READ],&result,sizeof(result)); printf("Parent:nx value after operation: %dn", x); close(mypipefd[WRITE]); read(mypipefd[WRITE],&result,sizeof(result)); printf("=======================================n"); close(mypipefd[READ]); exit(0); } else { perror("fork"); exit(1); } } } 

问题是,我的代码出了什么问题? 我试着读取和写回管道,但似乎没有工作……

我现在的代码输出是什么:

初始值0

家长:

操作后的x值:1

儿童:

操作后的x值:0

儿童:

操作后的x值:0

儿童:

操作后的x值:0

儿童:

操作后的x值:0

儿童:

操作后的x值:0

    我认为要解决的第一个问题是,当您尝试读取/写入数据时,管道的两端都会打开。 如果您正在将数据写入管道

     close(fd[READ_END]) write(...) close(fd[WRITE_END]) 

    其次,unix中的管道是单工的。 您似乎同时尝试从管道读取和写入。 如果你想这样做,你将不得不打开两个管道。

    我修改了你的程序,向你展示如何从管道读取,然后写入父/子管道。 希望这对你有所帮助。

     #include  #include  #include  #include  #define BUFFER_SIZE 25 #define READ 0 #define WRITE 1 int main(void) { pid_t pid; //open two pipes, one for each direction int mypipefd[2]; int mypipefd2[2]; /* create the pipe */ if (pipe(mypipefd) == -1 || pipe(mypipefd2) == -1) { fprintf(stderr,"Pipe failed"); return 1; } /* now fork a child process */ pid = fork(); if (pid < 0) { fprintf(stderr, "Fork failed"); return 1; } if (pid > 0) { /* parent process */ int writeValue=10; int readValue=0; close(mypipefd[READ]); //close read end, write and then close write end write(mypipefd[WRITE],&writeValue,sizeof(writeValue)); //write to pipe one printf("Parent: writes value : %dn", writeValue); close(mypipefd[WRITE]); close(mypipefd2[WRITE]); //close write end, read, and then close read end read(mypipefd2[READ],&readValue,sizeof(readValue)); printf("Parent: reads value : %dn", readValue); close(mypipefd2[READ]); } else { /* child process */ int writeValue=20; int readValue=0; close(mypipefd[WRITE]); //close write end, read, and then close read end read(mypipefd[READ],&readValue,sizeof(readValue)); printf("child: read value : %dn", readValue); writeValue+=readValue; close(mypipefd[READ]); close(mypipefd2[READ]); //close read end, write and then close write end write(mypipefd2[WRITE],&writeValue,sizeof(writeValue)); printf("child: writeValue value : %dn", writeValue); close(mypipefd2[WRITE]); } return 0; } 

    这打开两个管道。 父级将值写入第一个管道,子级读取该值。 子项更新此值,并将其写入第二个方向管道。 父母然后读取此值和vuela! 双向管道通信。

    输出:

     Parent: writes value : 10 child: read value : 10 child: write value : 30 Parent: reads value : 30 

    这可以扩展到您的应用程序

      if (pid > 0) { /* parent process */ result1++; close(mypipefd[READ]); //close read end, write and then close write end write(mypipefd[WRITE],&result1,sizeof(result1)); //write to pipe one printf("Parent:nx value after operation: %dn", result1); close(mypipefd[WRITE]); close(mypipefd2[WRITE]); //close write end, read, and then close read end read(mypipefd2[READ],&result1,sizeof(result1)); close(mypipefd2[READ]); } else { /* child process */ close(mypipefd[WRITE]); //close write end, read, and then close read end read(mypipefd[READ],&result2,sizeof(result2)); result2*=10; printf("child:nx value after operation %dn", result2); close(mypipefd[READ]); close(mypipefd2[READ]); //close read end, write and then close write end write(mypipefd2[WRITE],&result2,sizeof(result2)); close(mypipefd2[WRITE]); } 

    如果你把它放在循环中那么它理论上会起作用。 但是,由于上下文切换和其他与OS相关的调度,执行顺序将在两个进程之间混乱。 这意味着读取和写入不一定是您执行它的顺序,它不会工作。 阅读此问题如何等待数据写入管道的另一端 。

      以上就是c/c++开发分享Linux在c中使用管道传递父进程和子进程之间的值?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/562327.html

      (0)
      上一篇 2021年2月4日
      下一篇 2021年2月4日

      精彩推荐