2012-04-23 2 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)'호출의 반환 값을 확인해야합니다. (매우 이상한 이유로) 실패합니다. 이 프로그램을 어디에서 실행하려고합니까? 합리적인 주인인가요? 아니면 어색합니까? 나는 [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

이게 문제인지 모르겠지만 어쩌면 그걸 닫지 말아야지 * 포크 전에, 할 수있어 * 후에 * 갈래, 만약 당신이 그들을 필요로하지 않는다면 – 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

관련 문제