2013-05-11 45 views
2

我有,我必須實現一個鍵盤記錄到我們在課堂上做了一個殼的問題。我遇到了麻煩while循環內的程序的流程創建一個子進程後繼續循環,它已經跑了execlp()。Ç - while循環與叉()/管()內

下面是一個簡單的程序,我已經對我有麻煩的部分工作..我的主程序,pipe.c,包括while循環的父/子的過程,「應該」繼續獲得輸入從用於fgets()的用戶,創建子進程,使用DUP2(),寫入stdout,然後子進程調用receive.c的可執行文件,將從標準輸入獲取輸入並顯示它..

/* file: pipe.c */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h> 

int main() { 
    int key_logger_on = 0; 
    int p[2]; 
    pid_t pid; 
    char str[256]; 
    char input[1024]; 
    int status; 
    char * file = "test.txt"; 

    printf("Input :: "); 
    while(fgets(input, sizeof(input), stdin)) { 

    if (pipe(p)==-1) { 
     perror("Pipe create error"); 
     exit(1); 
    } 

    if ((pid=fork())==-1) { 
     perror("Fork create error"); 
     exit(1); 
    } 

    if (pid==0) { 
     close(p[1]); // Close write 
     dup2(p[0],0); 
     close(p[0]); 
     execlp("receive",file,NULL); 
    } 

    else { 
     close(p[0]); // Close read 
     fflush(stdout); 
     dup2(p[1],1); 
     close(p[1]); 
     write(1, input, strlen(input)+1); 
     waitpid(pid, NULL, 0); 
    } 
    printf("Input :: "); 
    } 
} 

這裏是簡單的receive.c獲取輸入的stdin並顯示它。該文件只是傳遞參數的測試。

/* file: receive.c */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

int main(int argc, char *argv[]) { 
    char input[256]; 
    fgets(input, sizeof(input), stdin); 
    printf("FILE: %s RECEIVE: %s", argv[0],input); 
    return 0; 
} 

現在,這一切都做對我來說,當跑的第一次,它得到的輸入,將其發送到標準輸出,兒童電話接收,打印出的輸入,然後整個父程序退出, while循環被忽略,一切都結束了。我對叉子和喉管很陌生,所以這是非常令人沮喪的處理!甚至讓我第一次在這裏發佈一個問題!非常感謝你提前。

+0

嗨,我今天做到了。對我來說也是重複。檢查我的答案 – qwr 2013-05-12 18:27:53

回答

1

今天做這對我重複任務。查看這個代碼。我測試了它與您的接收太:

#define PREAD 0 
#define PWRITE 1 

/* 
* 
*/ 

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

     int key_logger_on = 0; 
     int pIn[2]; 
     int pOut[2]; 
     pid_t pid; 
     char str[256]; 
     char input[1024] = ""; 
     int status; 

     char file[] = "test.txt"; 
     char buf; 
     printf("Input :: "); 
     while (fgets(input,sizeof(input),stdin)) { 

      char nChar; 
      int nResult; 

      if (pipe(pIn) < 0) { 
       perror("allocating pipe for child input redirect"); 
       return -1; 
      } 
      if (pipe(pOut) < 0) { 
       close(pIn[PREAD]); 
       close(pIn[PWRITE]); 
       perror("allocating pipe for child output redirect"); 
       return -1; 
      } 

      pid = fork(); 
      if (pid==0) { 
       // child continues here 

       // redirect stdin 
       if (dup2(pIn[PREAD], 0) == -1) { 
        perror("stdin"); 
        return -1; 
       } 

       // redirect stdout 
       if (dup2(pOut[PWRITE], 1) == -1) { 
        perror("stdout"); 
        return -1; 
       } 

       // redirect stderr 
       if (dup2(pOut[PWRITE], 2) == -1) { 
        perror("stderr"); 
        return -1; 
       } 

       // all these are for use by parent only 
       close(pIn[PREAD]); 
       close(pIn[PWRITE]); 
       close(pOut[PREAD]); 
       close(pOut[PWRITE]); 

       // run child process image 
       nResult = execl("receive",file,NULL); 

       exit(nResult); 
      } else if (pid > 0) { 
       // parent continues here 

       // close unused file descriptors, these are for child only 
       close(pIn[PREAD]); 
       close(pOut[PWRITE]); 

       write(pIn[PWRITE], input, strlen(input)); 

       // char by char reading 
       while (read(pOut[PREAD], &nChar, 1) == 1) { 
        write(STDOUT_FILENO, &nChar, 1); 
       } 

       // close we done 
       close(pIn[PWRITE]); 
       close(pOut[PREAD]); 
      } 
      printf("Input :: "); 
     } 
    } 
+0

非常感謝你!奇蹟般有效!進出的父管道比我想象的要更有意義,謝謝! =) – 2013-05-14 09:28:44