有人可以帮我处理c中的代码吗?

时间:2016-06-16 17:10:25

标签: c pipe fork

Iam尝试使用1个父级和2个子级创建代码。该方法重现3个参数:                        original_file word1 word2

父母逐行阅读文件:

  • 如果该行是对,则将该行发送到方法proccess_pair和word1。 如果该行包含word1,请将这些行保存在file_1.txt
  • 如果该行是奇数,则将该行发送到方法proccess_odd和word2。 如果该行包含word1,请将这些行保存在file_2.txt

我是初学者,我试着用这个:

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);
}

两种方法(对于chil1和chil2)都是相同的(但是关闭管道的正确边),因此我只实现其中一种:

void proccess_pair(char *word1){
    FILE *fd;
    fd =  fopen("file_1.txt","w");
    //closing the not used
    close(p_h1[1]);
    close(p_h2[1]);
    close(p_h2[0]);

    int nsto =  dup(1)//duplicate the stdout
    dup2(fd,1);//changing stdout->file_1.txt
    execlp("grep","grep",word1,NULL);//execution of grep
    exit(EXIT_SUCCESS);
    }

我正在学习,我知道我有很多错误,因此我需要帮助。

问候

2 个答案:

答案 0 :(得分:0)

  

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

在符合POSIX标准的系统上,您可以通过在pipe()的2D数组的元素上多次调用int来实现此目的。

  

¿我可以使用两个不同的管道(parent-child1,parent-child2)?我可以使用一系列管道吗?

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

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

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

答案 1 :(得分:0)

这样的事情应该有效:

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);
}

不要忘记为失败添加必要的控制。