2014-11-01 146 views
0

任何人都可以告訴我爲什麼從管道讀取時出現無限循環?我不明白我做錯了什麼。 message是我在主頂部定義的名稱。 MESSAGE是我的結構名稱。它甚至不打印出測試值。從管道讀取時無限循環

if(manager_pid == 0) 
{ 
    printf("Hello? \n"); 
    if(close(pipe1[READING]) != 0) 
    { 
     printf("Error in closing pipe1 \n"); 
    } 
    if(close(pipe2[READING]) != 0) 
    { 
     printf("Error in closing pipe2 \n"); 
    } 
    if(close(pipe3[WRITING]) != 0) 
    { 
     printf("Error in closing pipe \n"); 
    } 
} 
i = 0; 
printf("work please \n"); 
//test_value = read(pipe3[READING], &boo, sizeof(echo)); 
//printf("test_value is %d \n", test_value); 

while(i < 10) 
{ 
    printf("In while \n"); 
    //printf("Hello?? \n"); 
    //test_value = read(pipe3[READING], &boo, sizeof(echo)); 
    //printf("test_value is %d \n", test_value); 
    //printf("Entering infinite loop \n"); 
    //printf("i is %d \n", i); 
    //nbytes = read(pipe3[0], array, 45); 
    //printf("nbytes is %d \n", nbytes); 
    //log_dat_fp = fopen(argv[2], "a"); 
    if(read(pipe3[READING], &message, sizeof(struct MESSAGE)) != -1) 
    { 
     printf("Entering if \n"); 
     log_dat_fp = fopen(argv[2], "a"); 
     printf("First if \n"); 
     time(&current_time); 
     //if(message.owner == 1 && (message.instruction == 'r' || message.instruction == 'R')) 
     if(message.instruction == 'r' || message.instruction == 'R') 
     { 
      if(message.owner == 1) 
      { 
       printf("message.owner == 1 with r or R \n"); 
       fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s", strtok(ctime(&current_time), "\n"), 
       message.owner, getpid(), message.instruction, message.id); 
       pclose(log_dat_fp); 
      } 
      else if(message.owner == 2) 
      { 
       printf("message.owner == 2 with r or R \n"); 
       fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s", strtok(ctime(&current_time), "\n"), 
       message.owner, getpid(), message.instruction, message.id); 
       pclose(log_dat_fp); 
      } 
      else 
      { 
       printf("You have junk \n"); 
      } 
     } 
     else if(message.instruction == 'u' || message.instruction == 'U') 
     { 
      if(message.owner == 1) 
      { 
       printf("message.owner == 1 with u or U \n"); 
       fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"), 
       message.owner, getpid(), message.instruction, message.id, message.value); 
       pclose(log_dat_fp); 
      } 
      else if(message.owner == 2) 
      { 
       printf("message.owner == 2 with u or U \n"); 
       fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"), 
       message.owner, getpid(), message.instruction, message.id, message.value); 
       pclose(log_dat_fp); 
      } 
      else 
      { 
       printf("You have junk \n"); 
      } 
     } 
     else 
     { 
      printf("manager can't read from pipe\n"); 
      exit(1); 
     } // read no good 
     if(message.instruction == 'r' || message.instruction == 'R') 
     { 
      if(message.owner == 1) 
      { 
       for(i = 0; i < 200; i++) 
       { 
        if(strcmp(storage, table[i].id) == 0) 
        { 
         match_flag = 1; 
         value = table[i].value; 
        } 
       } 
       if(match_flag == 1) 
       { 
        message.value = value; 
        message.owner = 0; 
        if(write(pipe1[WRITING], &message, sizeof(struct MESSAGE)) == sizeof(struct MESSAGE)) 
        { 
         log_dat_fp = fopen(argv[2], "a"); 
         time(&current_time); 
         fprintf(log_dat_fp, "Store Manager at time: %s sent message: %c %d %s %d\n", strtok(ctime(&current_time), "\n"), 
         message.instruction, message.owner, message.id, message.value); 
         fclose(log_dat_fp); 
        } 
        else 
        { 
         printf("error returning message to process 1"); 
         exit(1); 
        } 
       } 
       else 
        message.owner = 1; 
        if(write(pipe1[WRITING], &message, sizeof(struct MESSAGE)) == sizeof(struct MESSAGE)) 
        { 
         log_dat_fp = fopen(argv[2], "a"); 
         time(&current_time); 
         fprintf(log_dat_fp, "Store Manager at time: %s sent message: %c %d %s \n", strtok(ctime(&current_time), "\n"), 
         message.instruction, message.owner, message.id); 
         fclose(log_dat_fp); 
        } 

      } 
      else if(message.owner == 2) 
      { 
       printf("message.owner == 2 with u or U \n"); 
       fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"), 
       message.owner, getpid(), message.instruction, message.id, message.value); 
       pclose(log_dat_fp); 
      } 
      else 
      { 
       printf("You have junk \n"); 
      } 

     } 
    } 
    else 
    { 
     printf("manager had pipe issues.\n"); 
     exit(1); 
    }// read no good 
    i++; 
    //log_dat_fp = fopen(argv[2], "a"); 
    printf("Each pass \n"); 
} 
+1

誰在寫信給管道?是你父母的過程嗎? – radar 2014-11-01 01:07:30

+0

您不檢查部分讀取或EOF。 – o11c 2014-11-01 01:07:58

+0

@RADAR另一個我的進程稍後將寫'(pipe3r [寫作],&消息,sizeof(結構MESSAGE))'。我試圖找出爲什麼我先有一個無限循環。它看起來並不像是閱讀。我的printf被完全忽略,所以我認爲我對這個'printf(「test_value是%d \ n」,test_value);'有問題。 – cokedude 2014-11-01 01:16:51

回答

3

的POSIX read() 功能上EOF返回0,不爲-1。既然你測試了錯誤的情況,你會得到一個無限循環。

注意,描述說:

當試圖從一個空的管道或FIFO讀:

  • 如果沒有進程打開寫管道,read()應返回0,表示檔案結尾。

  • 如果某些進程打開的寫作和O_NONBLOCK設置管道,read()將返回-1,並設置errno[EAGAIN]

  • 如果某些進程打開了寫作的管O_NONBLOCK是明確的,read()應當續延至一些數據被寫入或關閉管道時通過了對寫開放管道的所有進程阻止調用線程。

閱讀零個字節,因爲沒有要讀取的字節是成功的。這就是EOF如何由read()表示,無論它是在文件還是其他任何類型的設備上。終端是一個特例; (在Unix上)control-D後,它們可能會返回0個字節,然後重試可能會返回其後鍵入的額外數據。很久以前,磁帶驅動器有點類似。但是當read()返回0時,(暫時)沒有更多數據要讀取。對於一個管道來說,這意味着所有的編寫者關閉他們的寫入文件描述符。