c 编程 - 一位 parent 和 4 个 child 之间的双向沟通​​没有结果

标签 c arrays pipe

这个问题是this question的延伸修改后的要求。请引用以前版本的链接。与上一个问题的主要区别在于:

  1. 需要为每个子进程设置两个管道,并让子父进程与每个子进程通信
  2. 使用io重定向读取文件内容

我是管道新手,遇到了问题。在更新版本中:

  1. 家长分发卡片并将结果存储在数组中
  2. 将数组写入每个子进程负责的管道写入端
  3. 每个进程都需要从负责的管道读端读取数组
  4. 产生与之前版本相同的输出

问题是当我运行代码时,它没有结果。我的代码有什么问题?

我打算将每个子进程的所有管道存储在一个二维数组中,即 read_pipe[s][x],其中 s 是子进程标识符,x 是文件描述符。

我也研究过this question , 但是,这个问题是针对 1 parent 1 child 的,它无法解决我的问题,因为我有 4 个 child 。

更新版本的初步尝试:

#include <stdio.h>
#include <string.h>

#define BUFFERSIZE 51  
int main(void)
{
    int ch; 
    ssize_t rread;
    char *line = NULL;
    size_t len = 0; 
    while (rread = getdelim( &line, &len, '\0', stdin) != -1) {
        printf("Retrieved line of length %zu :\n", rread);
        printf("%s", line);
    }  
    char *array[BUFFERSIZE];
    int i=0;

    array[i] = strtok(line," ");

    while(array[i]!=NULL)
    {
        array[++i] = strtok(NULL," ");
    }
    //for (i=0; i<=BUFFERSIZE; i++)
      //  printf("\narray: %s: %d", array[i], i);
    //return 0;

    int childlimit = 4;
    int childpids[childlimit];
    int currpid, s; 
    char *buf[BUFFERSIZE];
    /** Pipe for reading for subprocess */
    int read_pipe[4][2];
    /** Pipe for writing to subprocess */
    int write_pipe[4][2];
    int n;
    char buffer[100];
    memset(buffer, '\0', 100);
    if (pipe(read_pipe) == 0 && pipe(write_pipe) == 0)
    {
      for(s=0; s<childlimit; s++){
        switch(currpid = fork()){
        case 0: 
         printf("Child : %d, pid %d : ", s, getpid() );
         close(read_pipe[s][1]);
         close(write_pipe[s][0]);
         close(write_pipe[s][1]);
         while((n = read(read_pipe[s][0], buf, BUFFERSIZE))>0){
             printf("<child> %s", buf[n]); //no output
         } 
         close(read_pipe[s][0]);
         return 0;
        case -1:
                printf("Error when forking\n");
                break;
        default:
                // in the father
                childpids[s] = currpid; 
                // store current child pid
                close(read_pipe[s][0]);
                close(read_pipe[s][1]);
                close(write_pipe[s][1]);
                int j,tt;
                for(j=0; j<BUFFERSIZE; j+=4){
                    buf[tt] = array[j];
                    tt++;
                }
                write(write_pipe[s][0],buf,BUFFERSIZE);
                close(write_pipe[s][0]);
                break;
        }
    }   
    //wait for all child created to die
    waitpid(-1, NULL, 0);
    printf("Father : %d childs created\n", s);
    }
}

最佳答案

如评论中所述,pipe() 采用大小为 2 的 int 数组,它等于管道的两端。您在 filedes[0] 上读取并在 filedes[1] 上写入。这是一起使用 pipe()fork() 的示例。

int filedes[2];
if(pipe(filedes)==0)
  {
  pid_t childpid=fork();
  if(childpid==0)
     {
     close(filedes[1]); // close write end of the pipe
     read(filedes[0],buf,BUFFERSIZE);
     close(filedes[0]);
     exit(0);
     }
  else if(childpid!=-1)
     {
     close(filedes[0]);
     write(filedes[1],buf,BUFFERSIZE);
     close(filedes[1]);
     wait(NULL);
     }
  else
     {
     printf("Fork failed\n");
     }
  }

关于c 编程 - 一位 parent 和 4 个 child 之间的双向沟通​​没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42693084/

相关文章:

c - 在 C 中为 win32 和 win64 打开 stdin/stdout/stderr 的 Windows 控制台

c - 获取缓冲区末尾的地址而不是其开头的地址

python - 无法按名称访问列数据

c++ - C/C++ 中无数组的单行快速输入字符串

winapi - ReadFile 在从子进程结束后读取 stdout 时不返回

bash - 从管道获取参数

带有循环和管道的 bash 函数

c++ - 猜测运行时函数的返回类型

python - 将 numpy 数组拆分为不重叠的数组

c - C 中 "emulate"多态性时使用结构成员和转换结构指针之间的区别