2016-04-13 36 views
0

如何使用兩條管道用管父母和孩子之間發送消息父/子之間發送消息

我不能在我的代碼

這裏找出錯誤是我的代碼:

/* Using a pipe to send data from a parent to a child process 
*/ 

#include <iostream> 
#include <cstdio> 
#include <unistd.h> 
#include <string.h> 

using namespace std; 

int main(int argc, char *argv[ ]) { 

    int   f_des[2]; 
    int   f_des2[2]; 

    static char message[5]; 

    if (argc != 2) { 
     cerr << "Usage: " << *argv << " message\n"; 
     return 1; 
    } 

    if (pipe(f_des) == -1 || pipe(f_des2) == -1) {    // generate the pipe 

     perror("Pipe"); 
     return 2; 
    } 

    switch (fork()) { 

     case -1: 
      perror("Fork"); 
      return 3; 

     case 0:        // In the child 

      close(f_des[1]); 

      if (read(f_des[0], message, BUFSIZ) != -1) { 
       cout << "Message received by child: [" << message 
        << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Read"); 
       return 4; 
      } 

      close(f_des[0]); 
      close(f_des2[0]); 

      if (write(f_des[1], argv[2], strlen(argv[2])) != -1) { 
       cout << "Message sent by child : [" << 
         argv[1] << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Write"); 
       return 5; 
      } 

      break; 

     default:        // In the Parent 

      close(f_des[0]); 

      if (write(f_des[1], argv[1], strlen(argv[1])) != -1) { 
       cout << "Message sent by parent : [" << 
         argv[1] << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Write"); 
       return 6; 
      } 

      close(f_des[0]); 
      close(f_des2[0]); 

      if (read(f_des[0], message, BUFSIZ) != -1) { 
       cout << "Message received by parent: [" << message 
        << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Read"); 
       return 7; 
      } 
     } 

     return 0; 
    } 
+0

您的代碼存在一些有問題的格式錯誤,需要列出具體問題,代碼應如何正常運行以及可驗證的結果。 – Stephen

+0

有關正確格式化發佈的代碼的說明,請參閱stackoverflow.com的幫助中心。 –

回答

0

此代碼看起來很混亂。

子進程close(f_des[1]); -s,然後稍後嘗試寫入它。

父進程close(f_des[0]);-s。它關閉它不是一次,而是兩次。然後它試圖從中讀取。關閉一次後,從文件中讀取已經很困難;你可以放心,在完成兩次任務後,你絕對沒有機會成功。

您可能需要坐下來,在一張紙上繪製所有四個文件描述符,兩對管道以及他們每個人在每個過程中應該做的事,父母和孩子;以及在每個過程中需要關閉哪一個。

+0

它必須從父母發送消息到孩子的代碼。顯示消息。然後孩子發送消息給父母。 –

+0

聽起來像一個計劃。祝你好運,正確實施你的代碼,但stackoverflow.com是不是一個網站,你可以找到其他人爲你寫正確的代碼。 –

+0

-Sam Varshavchik ..我試圖解決它,並要求這個網站上的專業人士看看我的錯誤是什麼,我沒有要求任何機構給我寫代碼。順便說一句,我解決它 –

0

我試圖修改此程序,以便孩子在收到消息後更改其大小寫並將消息(通過管道)返回給父級,然後顯示。

#include <iostream> 
#include <cstdio> 
#include <unistd.h> 
#include <string.h> 
using namespace std; 
int 
main(int argc, char *argv[ ]) { 
    int   f_des[2]; 
    static char message[5]; 
    if (argc != 2) { 
     cerr << "Usage: " << *argv << " message\n"; 
     return 1; 
    } 
    if (pipe(f_des) == -1) {    // generate the pipe 
     perror("Pipe");  return 2; 
    } 
    switch (fork()) { 
     case -1: 
      perror("Fork");  return 3; 
     case 0:        // In the child 
      close(f_des[1]); 
      if (read(f_des[0], message, BUFSIZ) != -1) { 
       cout << "Message received by child: [" << message 
       << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Read"); return 4; 
      } 
      break; 
     default:        // In the Parent 
      close(f_des[0]); 
      if (write(f_des[1], argv[1], strlen(argv[1])) != -1) { 
       cout << "Message sent by parent : [" << 
       argv[1] << "]" << endl; 
       cout.flush(); 
      } else { 
       perror("Write"); return 5; 
      } 
    } 
    return 0; 
} 
相關問題