2012-06-22 27 views
2

我正在使用管道編寫協處理程序。當孩子讀取一些數據,處理它並輸出它時,它工作正常。但是,當我讀取所有數據並處理它時,它只是等待。任何機構都有一些想法?謝謝。讀入協處理後的子進程掛起

這裏是源代碼:

#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <stdlib.h> 
#include <errno.h> 


int main() 
{ 
    #define MAXSIZE 1024 

    char workload[MAXSIZE]; 
    char result[MAXSIZE]; 
    workload[strlen(workload)] = EOF; 
    int workload_size = strlen(workload); 

    int fd1[2], fd2[2]; 
    int n; 
    pid_t pid; 
    if (pipe(fd1) < 0 || pipe(fd2) < 0) { 
     fprintf(stderr, "pipe error: %s\n", strerror(errno)); 
     exit(1); 
    } 

    if ((pid = fork()) < 0) { 
     fprintf(stderr, "fork error: %s\n", strerror(errno)); 
     exit(1); 
    } else if (pid > 0) { 
     close(fd1[0]); 
     close(fd2[1]); 
     while(fgets(workload, MAXSIZE, stdin) != NULL) 
     { 
      workload_size = strlen(workload); 
      if (write(fd1[1], workload, workload_size) != workload_size) { 
       fprintf(stderr, "write to pipe error: %s\n", strerror(errno)); 
       exit(1); 
      } 

      if ((n = read(fd2[0], result, MAXSIZE)) < 0) { 
       fprintf(stderr, "read from pipe error: %s\n", strerror(errno)); 
       exit(1); 
      } 

      if (n == 0) { 
       fprintf(stderr, "child closed the pipe\n"); 
       exit(1); 
      } 

      result[n] = 0; 

      if (puts(result) == EOF) { 
       fprintf(stderr, "fputs error\n"); 
       exit(1); 
      } 
     } 
    } else { 
     close(fd1[1]); 
     close(fd2[0]); 
     if (fd1[0] != STDIN_FILENO) { 
      if (dup2(fd1[0] ,STDIN_FILENO) != STDIN_FILENO) { 
       fprintf(stderr, "dup2 error to stdin.\n"); 
       exit(1); 
      } 
      close(fd1[0]); 
     } 
     if (fd2[1] != STDOUT_FILENO) { 
      if (dup2(fd2[1] ,STDOUT_FILENO) != STDOUT_FILENO) { 
       fprintf(stderr, "dup2 error to stdout.\n"); 
       exit(1); 
      } 
      close(fd2[1]); 
     } 

     if (execl("./a.out", "a.out", NULL) < 0) { 
      fprintf(stderr, "execl error: %s\n", strerror(errno)); 
      exit(1); 
     } 
     exit(0); 
    } 

    return 0; 
} 

這裏是a.out的源代碼,它的工作原理以及與此:

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    #define MAXSIZE 1024 
    char x[MAXSIZE]; 
    int n; 
    while(scanf("%s", x) != EOF) 
    { 
     printf("len:%d %s", strlen(x), x); 
     fflush(stdout); 
    } 
    return 0; 
} 

但是,當我寫的代碼似乎只是待定像這樣:

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    #define MAXSIZE 1024 
    char x[MAXSIZE]; 
    int n; 
    while(scanf("%s", x) != EOF); 
    printf("Ok\n"); 
    fflush(stdout); 

    return 0; 
} 

回答

1

您呼叫scanf%s可能會溢出x緩衝區的方式。您至少應該使用寬度修飾符修改scanf。

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    #define MAXSIZE 1024 
    char x[MAXSIZE]; 
    int n; 
    while(scanf("%1024s", x) != EOF) 
    { 
     printf("len:%d %s", strlen(x), x); 
     fflush(stdout); 
    } 
    return 0; 
} 

和其他程序類似。

您的程序被阻止的原因是因爲您的第二個a.out程序正在循環做另一個scanf,同時父程序試圖將響應讀回到result

+0

謝謝。在將必要的數據寫入子進程之後,通過關閉管道來解決問題。 –

1

你應該測試和循環,而不是feof,你可能會使用popen & pclose

你可能想使用一些複用系統調用,比如poll