2012-04-23 4 views
-1

このプログラムは、父親と2人の子供を作成し、チェーンの文字が存在し、父親は2本のパイプ、最初の子は最初のパイプから読み込み、2番目のパイプは2番目のパイプから読み込み、どれくらいの文字を返すかを返します。プログラムは実行されますが、すべてではありませんが、私はこのエラーが何を意味するのかわかりません

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

main() 
{ 
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n"); 
    char *word="alibas123sam"; 

    printf("Now 2 pipes will be created\n"); 
    int fd1[2]; 
    int fd2[2]; 
    pipe(fd1); pipe(fd2); 
    printf("Now the father will write numbers in the first pipe, and letters in the second\n"); 
    int i; 
    char numbers[20]; int j=0; 
    char caracters[20]; int k=0; 
    for (i=0;i<20;i++) 
    { 
     if(word[i]>='0' && word[i]<='9') //if number 
     { 
      close(fd1[0]); //closing reading 
      write(fd1[1],&word[i],1); 

     } 
     else 
     { 
      close(fd2[0]); 
      write(fd2[1],&word[i],1); 
     } 

    } 
    printf("The father has wrote in the 2 pipes, now its time for the sons\n"); 
    int f=fork(); 
    if(f==0) //first son 
    { 
     for(i=0;i<20;i++) {   
      close(fd1[1]); //closing writing 
      read(fd1[0],&numbers[j],1); 
      j++; 

     } 
     printf("first son read everything, he got %d Numbers\n", j); 
    } 
    else 
    { 
     f=fork(); 
     if(f==0) 
     { 
      for(i=0;i<20;i++) {   
      close(fd2[1]); //closing writing 
      read(fd2[0],&caracters[k],1); 
      k++; 

     } 
     printf("second son read everything, he got %d caracters\n", k); 
    } 
}} 

エラー:問題は、あなたがあなたのフォークの前にファイルディスクリプタをクローズするという事実から生じる可能性

Disallowed system call: SYS_pipe 
+0

'pipe(2)'コールからの戻り値をチェックする必要があります。それらは失敗しているように見えます。このプログラムをどこで実行しようとしていますか?それは合理的なホストですか、それとも厄介ですか?私はあなたが[AppArmor](http://wiki.ubuntu.com/AppArmor/)のような[必須のアクセス制御](http://en.wikipedia.org/wiki/Mandatory_access_control)ツールに限定されているのだろうかと思います。 )、[SELinux](http://en.wikipedia.org/wiki/Security-Enhanced_Linux)、[TOMOYO](http://tomoyo.sourceforge.jp/index.html.en)、[SMACK](http ://schaufler-ca.com/)、私はそのような_odd_エラーを印刷するとは思わない。 – sarnold

+0

なぜ記述子を閉じますか? – ShinTakezou

+0

私はこれが問題だとは思っていませんが、もしあなたがそれらを必要としなければforkした後*フォークする前にフォークする前にそれらを閉じるべきではないかもしれません: – ShinTakezou

答えて

1

。コードを並べ替えて

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

main() 
{ 
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n"); 
    char *word="alibas123sam"; 

    printf("Now 2 pipes will be created\n"); 
    int fd1[2]; 
    int fd2[2]; 
    pipe(fd1); pipe(fd2); 
    printf("Now the father will write numbers in the first pipe, and letters in the second\n"); 
    int i; 
    char numbers[20]; int j=0; 
    char caracters[20]; int k=0; 
    for (i=0;i<20;i++) 
    { 
     if(word[i]>='0' && word[i]<='9') //if number 
     { 
      close(fd1[0]); //closing reading 
      write(fd1[1],&word[i],1); 

     } 
     else 
     { 
      close(fd2[0]); 
      write(fd2[1],&word[i],1); 
     } 

    } 
    printf("The father has wrote in the 2 pipes, now its time for the sons\n"); 
    int f=fork(); 
    if(f==0) //first son 
    { 
     for(i=0;i<20;i++) {   
      close(fd1[1]); //closing writing 
      read(fd1[0],&numbers[j],1); 
      j++; 

     } 
     printf("first son read everything, he got %d Numbers\n", j); 
    } 
    else 
    { 
     f=fork(); 
     if(f==0) 
     { 
      for(i=0;i<20;i++) {   
      close(fd2[1]); //closing writing 
      read(fd2[0],&caracters[k],1); 
      k++; 

     } 
     printf("second son read everything, he got %d caracters\n", k); 
    } 
}} 

のように見えました。

+0

私はこれが理由だと思う:fork ()は開いているファイル記述子を複製しているので、フォークの前にそれらを閉じると、子は閉じたものを見逃します。 – ShinTakezou

関連する問題