2016-06-16 9 views
-2

1つの親と2つの子を持つコードを作成しようとしています。メソッドproccess_pairと単語1にラインを送る、ラインがペアであれば誰かが私のコードをCで助けてくれますか?

  • :親が行毎にファイルを読み込む original_fileワード1ワード2

    :メソッドは、3つのパラメータをrecive。 ラインは、メソッドproccess_oddとWORD2にラインを送る、ラインが奇数の場合file_1.txt

  • にラインを保存し、単語1が含まれている場合。 ラインは、cにfile_2.txt

イム初心者の行を保存し、WORD1が含まれている、と私はこれにしようとしている場合:(chil1とchil2用)

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

どちらの方法になります同じ(しかし、パイプの正しい側面を閉じて)、このような理由のために私はそれらのいずれかを実装します。

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

イム学習と私は助けを必要とするこのような理由のために、多くの多くのエラーを持っていることを知っています。

よろしく私はC言語で配列内の多くのパイプを作成するにはどうすればよい

+0

あなたはその後、2人の子供のために何かを書くと、コードを投稿しよう1人の子供とのパイプを使用する方法を知っている場合。 –

+0

@ Jean-BaptisteYunès編集されています!ありがとうejeje – randall

答えて

0

? POSIX準拠のシステムでは

、あなたが提示され、intの2次元配列の要素にpipe()何度も呼び出すことによってそれを行うことができます。

❑2つの異なるパイプ(parent-child1、parent-child2)を使用できますか?私はパイプの配列を使用できますか?

パイプ自体はカーネルだけに住んでいます。ユーザー空間データ 構造体はパイプを表していないので、パイプの配列を持つことはできません。

パイプのファイル記述子は、で終了しますが、ちょうどintです。 pipe()機能は、それが配列に適切なファイル記述子を書き込む(成功した)その引数として、少なくとも二つのintの配列の最初の要素へのポインタをとり、。

Cの観点から、パイプの端部が返されるアレイに関する特別なものは何もありません。特に望むなら、多次元配列の要素となることができます。または、ローカル変数にすることもできます。または、structまたはunionのメンバーになることができます。または、動的に割り当てられた十分な大きさのブロックにすることもできます。それは特別ではありません。このような

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

は失敗に必要なコントロールを追加することを忘れないでください。

関連する問題