2012-04-23 24 views
-1

這個程序會創建一個父親和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)'調用的返回值 - 它們看起來是失敗的(因爲看起來很奇怪)。你想在哪裏執行這個程序?這是一個合理的主機還是很尷尬?我不知道你是否受限於[強制訪問控制](http://en.wikipedia.org/wiki/Mandatory_access_control)工具,如[AppArmor](http://wiki.ubuntu.com/AppArmor/) ),[SELinux](http://en.wikipedia.org/wiki/Security-Enhanced_Linux),[TOMOYO](http://tomoyo.sourceforge.jp/index.html.en)或[SMACK](http ://schaufler-ca.com/),但我懷疑是否會打印這樣的_odd_錯誤。 – sarnold 2012-04-23 06:00:21

+0

爲什麼你關閉描述符? – ShinTakezou 2012-04-23 06:02:31

+0

我不確定這是否是問題,但也許你不應該在*叉子之前關閉它們*,你可以在*分叉之後做到這一點*如果你不需要它們 – ShinTakezou 2012-04-23 06:09:53

回答

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 ()複製了打開的文件描述符,所以如果你在fork之前關閉它們,孩子將會錯過關閉的文件描述符。 – ShinTakezou 2012-04-23 06:14:34