2017-11-25 8 views
1

C에서 간단한 프로듀서 소비자 프로그램이 있습니다. 포크로 해결하려고 시도합니다. 생산자가 파이프에 쓰려고 할 때 정확히 오류가 발생합니다.이 논리는 있지만 다른 하나의 프로그램으로이 프로그램을 작성했습니다. 왜 그런지 알지 못해요? 나는이 오류가 왜C 파이프 : 잘못된 파일 설명자

누구나 생각이 잘못된 파일 설명 :

프로듀서 파이프 항목을 쓰지 못했습니다? 감사

#define READ 0 
#define WRITE 1 
int mutex = 1, full = 0, empty = BUFFER_SIZE, x = 0; 

void consumer(); 

void producer(); 

int wait_(int); 

int signal_(int); 

int pipefd[2]; 

int main() { 
    printf("Starting producer-consumer problem!\n"); 
    //We intend to run the producer in parent process and the consumer in the child process 
    if (pipe(pipefd) == -1) {  /* An error has occurred. */ 
     fprintf(stderr, "%s", "The call to pipe() has failed.\n"); 
     exit(EXIT_FAILURE); 
    } 
    for (int j = 0; j < sizeof(pipefd); j++) { 
     if (pipe(&pipefd[j]) < 0) { //Initialize each pipe appropriately 
      perror("Error in making pipe..."); 
     } 
    } 
    pid_t pid = fork(); 
    if (pid < 0) { 
     perror("**********Error in creating fork()!**************\n"); 
     exit(STDERR_FILENO); 
    } else if (pid == 0) { 
     consumer();//We intend to run the consumer in child 
    } else { 
     producer();//We intend to run the producer in parent 
    } 
    return 0; 
} 

int wait_(int s) { 
    return (--s); 
} 

int signal_(int s) { 
    return (++s); 
} 

void producer() { 
    printf("Starting Producer\n"); 
    //while (1) { 
    //sleep(1); 
    if (close(pipefd[READ]) != 0) { 
     perror("Error in closing reading pipe"); 
    } 
    if (write(pipefd[WRITE], &full, 1) < 0) { 
     perror("Producer failed to write item on pipe"); 
    } 
    if ((mutex == 1) && (empty != 0)) { 
     mutex = wait_(mutex); 
     full = signal_(full); 
     empty = wait_(empty); 
     x++; 
     printf("Producer produces the item %d\n", x); 
     mutex = signal_(mutex); 
    } 
    if (close(pipefd[WRITE]) != 0) { 
     perror("Error in closing writing pipe"); 
    } 
    //} 
} 

void consumer() { 
    printf("Starting Consumer\n"); 
    //while (1) { 
    //sleep(1); 
    int status = 0; 
    wait(&status);    /* wait for all children to return back the result */ 
    if (close(pipefd[WRITE]) != 0) { 
     perror("Error in closing reading pipe"); 
    } 
    if (read(pipefd[READ], &full, 1) > 0) { 
     printf("Consumer\t%d\n", full); 
    } 
    if ((mutex == 1) && (full != 0)) { 
     mutex = wait_(mutex); 
     full = wait_(full); 
     empty = signal_(empty); 
     printf("Consumer consumes item %d\n", x); 
     x--; 
     mutex = signal_(mutex); 
    } 
    if (close(pipefd[READ]) != 0) { 
     perror("Error in closing reading pipe"); 
    } 
    //} 
} 
+0

'READ'와 'WRITE'는 어디에 정의되어 있습니까? – Frxstrem

+0

REAd & WRITE 값이란 무엇입니까? 0 & 1입니까? – achal

답변

1

sizeof 운영자는 바이트의 크기 를 반환합니다. 따라서 int이 4 바이트 인 일반적인 시스템에서는 sizeof(pipefd)이 값 8이됩니다. 루프의 요소 수가 올바르지 않습니다.

또한 pipe(&pipefd[j])도 올바르지 않습니다. pipefd의 두 파이프는 이며 이미이 "적절하게"초기화되었습니다. 더 이상 초기화 할 필요가 없습니다. 특히이 경우와 이전의 경우 모두 undefined behavior이됩니다.

+0

감사합니다! 이것은 내 문제를 해결했다. 초기화를 위해 pipe (pipefd)를 호출하는 것으로 충분합니까? – matio

+0

@ 메 타오 그렇습니다. –

+0

다시 한번 감사드립니다. 파이프의 쓰기 디스크립터를 닫아야합니까? iterator를 사용하는 동안 그것을 수행하고 두번 닫지 않도록 어떻게 할 것인가? – matio