有人能帮我用c写代码吗?

有人能帮我用c写代码吗?,c,pipe,fork,C,Pipe,Fork,我正在尝试创建一个包含1个父项和2个子项的代码。该方法接收3个参数: 原始文件word1 word2 父级逐行读取文件: 如果该行是pair,则将该行发送到方法PROCESS_pair和单词1。 如果该行包含单词1,请将该行保存在文件_1.txt中 如果该行为奇数,则将该行发送到方法PROCESS_odd,并发送单词2。 如果该行包含单词1,请将该行保存在文件_2.txt中 我是c语言的初学者,我正在尝试: int p_h1[2] // pipe from parent to child1

我正在尝试创建一个包含1个父项和2个子项的代码。该方法接收3个参数: 原始文件word1 word2

父级逐行读取文件:

  • 如果该行是pair,则将该行发送到方法PROCESS_pair和单词1。 如果该行包含单词1,请将该行保存在文件_1.txt中
  • 如果该行为奇数,则将该行发送到方法PROCESS_odd,并发送单词2。 如果该行包含单词1,请将该行保存在文件_2.txt中
我是c语言的初学者,我正在尝试:

int p_h1[2] // pipe from parent to child1
int p_h2[2];// pipe from parent to child2

int main(int argc, char **argv){
    pid_t pdi1, pdi2;
    FILE *fd; // for original file
    FILE *p_h1f, *p_h2f; //file create for child1 and child2 respectively
    char buffer[1024];//buffer 
    if (pid1<0){
        fprintf(stderr,"Error fork \n %s \n",strerror(errno));
        exit(EXIT_FAILURE);
        }
    else if (pid1==0){//Im the child1
        //proccess for child 1
        proccess_pair(arg[2]);
        exit(EXIT_SUCCESS);
         }
pid2 = fork();
if (pid2<0){
    fprintf(stderr,"Error fork \n %s \n",strerror(errno));
    exit(EXIT_FAILURE);
    }
else if (pid2==0){//Im the child2
        //proccess for child 2
        proccess_odd(arg[2]);
        exit(EXIT_SUCCESS);
     }

//Parent dont read from pipe

close(p_h1[0]);
close(p_h2[0]);

fd = fopen(argv[1],"r"); //I openthe file for read it;

p_h1f = fdopen(p_h1[1],"w")
p_h2f = fdopen(p_h2[1],"w")
int i = 1;

while(fgets(buffer,1024,fd) != NULL){
    if (i % 2 ==0){ //check if the lines is pairs
        fputs(buffer,p_h1f);
        fflush(p_h1f);
    }else{
        fputs(buffer,p_h2f);
        fflush(p_h2f);          
    }
    i++;
}
close(p_h1[1]);
close(p_h2[1]);
fclose(fd);
wait(NULL);
wait(NULL);
}
我在学习,我知道我有很多错误,因此我需要帮助

问候

如何在c中创建一个数组中的多个管道

在符合POSIX的系统上,您可以通过多次调用
int
的2D数组元素
pipe()
来实现这一点

?我可以使用两种不同的管道(parent-child1,parent-child2)?我可以使用一组管道吗

管道本身只存在于内核中。没有用户空间数据 表示管道的结构,因此不能有管道数组

然而,管道末端的文件描述符只是
int
s。
pipe()
函数将指向至少两个
int
数组的第一个元素的指针作为其参数,并且(成功时)将适当的文件描述符写入数组


从C的角度来看,返回管道端点的数组没有什么特别之处。特别是,如果您愿意,它可以是多维数组的元素。也可以是局部变量。或者它可以是
结构
联合
的成员。或者它可以是一个足够大的动态分配空间块。这没什么特别的。

像这样的东西应该有用:

int new_process(char *word){ // return the writing part of a pipe to a newly created child
  int p[2];
  pipe( p ); // get a new pipe
  if (fork()==0) { // create a new child
    dup2(p[0],0); // child's in is pipe's entry
    close(p[1]);  // close writing part
    execlp("grep","grep",word,NULL); // exec
  }
  close(p[0]); // parent don't need the reading part
  return p[1]; // send back the writing part to caller
}

int main(int argc, char **argv) {
  int f1 = new_process(argv[1]); // get the pipe to first child
  int f2 = new_process(argv[1]); // ...second...
  char buffer[1024];//buffer 

  FILE *fd = fopen(argv[1],"r"); // open the file for reading;

  while(fgets(buffer,1024,fd) != NULL) { // read aline
    if (i % 2 ==0) { //check if the line no is pair
      fputs(buffer,f1); // send it to first child
      fflush(f1);
    } else{
      fputs(buffer,f2); // send it to second child
      fflush(f2);          
    }
    i++;
  }
  close(f1);
  close(f2);
  fclose(fd);
  wait(NULL);
  wait(NULL);
}

不要忘了在失败时添加必要的控件。

如果你知道如何将管道与一个孩子一起使用,那么试着为两个孩子编写一些东西并发布代码。@Jean BaptisteYunès编辑!谢谢ejeje
int new_process(char *word){ // return the writing part of a pipe to a newly created child
  int p[2];
  pipe( p ); // get a new pipe
  if (fork()==0) { // create a new child
    dup2(p[0],0); // child's in is pipe's entry
    close(p[1]);  // close writing part
    execlp("grep","grep",word,NULL); // exec
  }
  close(p[0]); // parent don't need the reading part
  return p[1]; // send back the writing part to caller
}

int main(int argc, char **argv) {
  int f1 = new_process(argv[1]); // get the pipe to first child
  int f2 = new_process(argv[1]); // ...second...
  char buffer[1024];//buffer 

  FILE *fd = fopen(argv[1],"r"); // open the file for reading;

  while(fgets(buffer,1024,fd) != NULL) { // read aline
    if (i % 2 ==0) { //check if the line no is pair
      fputs(buffer,f1); // send it to first child
      fflush(f1);
    } else{
      fputs(buffer,f2); // send it to second child
      fflush(f2);          
    }
    i++;
  }
  close(f1);
  close(f2);
  fclose(fd);
  wait(NULL);
  wait(NULL);
}