c - 如何在 c 中将数据从一个管道重定向到另一个管道?

标签 c linux pipe

我正在尝试使用两个单独的管道从一个进程写入另一个进程。通过以下方式:

  1. child1 写入 parent(使用 pipe1)
  2. parent 写入 child2(使用 pipe2)

我在写入父文件时没有问题,但是当我尝试将数据中继到子文件 2 时,文件描述符似乎为 NULL,我不确定为什么。为清楚起见,我试图强调我遇到问题的领域。我还删除了很多错误处理。

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main (void)
  {
      pid_t pid;
      pid_t pid1;
      int mypipe[2];
      int mypipe1[2];
      int file;
      char buf[100];
      FILE *stream;
      FILE *stream2;
      FILE *rm;
      ssize_t numbersread;

          if (pipe (mypipe))
            {
              fprintf (stderr, "Pipe failed.\n");
              return EXIT_FAILURE;
            }
          if (pipe (mypipe1))
            {
              fprintf (stderr, "Pipe2 failed.\n");
              return EXIT_FAILURE;
            }

          /* CREATE THE FIRST CHILD HERE. */
          pid = fork ();

          if (pid == (pid_t) 0)
            {
                rm = fopen("Readme.txt","r");

                //10 BYTES AT A TIME
                close(mypipe[0]);
                for(k=0;k<=10;k++)
                {
                  transmitor(mypipe[1],rm); // GO READ FILE AND THEN WRITE ON PIPE
                }
                fclose(rm);

                return EXIT_SUCCESS;
            }


          // BACK TO THE PARENT PROCESS
          else
            {
            /*OBJECTIVES:
                1. READ THE FILE FROM THE PIPE
                2. WRITE THE FILE ONTO A SECOND PIPE
                3.SEND IT TO THE RECEIVER
            */

             FILE *file1;
             ssize_t numbersread1;
             file1 = fdopen(mypipe[0],"r");

             close (mypipe[1]);
             close(mypipe1[0]);
             stream2 = fdopen(mypipe1[1],"w");
             while(!feof(file1)){
              numbersread1 = fread(buf, 1, (sizeof buf),file1);
              printf("%zd\n", numbersread1);
              **fwrite(buf,1,numbersread1,stream2);**
              buf[numbersread1] = 0;
             }
             printf("%s\n","finished parent");
             fclose(file1);// FINISHED READING
             fclose(stream2);

           ** /* CREATE THE SECOND CHILD HERE #2. */
            /*OBJECTIVES:
               1. READ DATA FROM PIPE
               2. WRITE DATA TO FILE*/

             pid1 = fork ();
             sleep(2);
             if (pid1 == (pid_t) 0)
                {
                  /* This is the child process.
                     Close read end first. */
                     FILE *stream3;
                     stream3 = fdopen(mypipe1[0],"r");
                     close (mypipe1[1]);
                    if(stream3==NULL)
                     {
                      printf("%s","NULL Stream3 Variable");
                     }
                    else
                     {
                      while (!feof(stream3)) {
                      printf("\r\nIN WHILE\r\n");
                      numbersread = fread(buf, 1, (sizeof buf),stream3);
                      printf("%zd\n", numbersread);
                      buf[numbersread] = 0;
                    }
                     fclose(stream3);
                    }**

                    printf("%s","FINISHED RECEIVER");

                    return EXIT_SUCCESS;
                }


             return EXIT_SUCCESS;
            }// THIS CLOSES THE FIRST ENTRANCE TO THE PARENT PROCESS WHERE WE ARE WRITING TO THE FIRST RECEIVER



        }// THIS IS THE END OF THE MAIN FUNCTION

最佳答案

close(2) 稍后使用的文件描述符,例如您的这段代码:

file1 = fdopen(mypipe[0],"r");

close (mypipe[1]);
close(mypipe1[0]);

您关闭 mypipe1[0]。再往下你做:

FILE *stream3;
stream3 = fdopen(mypipe1[0],"r");
close (mypipe1[1]);

因此 stream3 将为 NULL

我还强烈建议为变量命名更多它们的作用。例如,mypipe 可以是 c1_to_parentmypipe1 可以是 parent_to_c2。这将使您的代码更具可读性。

关于c - 如何在 c 中将数据从一个管道重定向到另一个管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143225/

相关文章:

c++ - 将对象 (.o) 文件添加到 qtcreator 项目

c++ - iostream 链接器错误

c - 为什么 g_thread_supported 在我的机器上返回 false

linux - 向 USB 驱动程序注册用户空间回调函数

python - UNIX 命名的 PIPE 文件结尾

c - 如何使用 fprintf 并写入管道?

将 C 字符串转换为二进制表示

c - Linux/ARM "kuser_helper"函数的运行时检测

linux - 检查目录中是否存在某种文件类型/扩展名

angular - ngFor Angular2 中的动态管道