2013-04-24 16 views
0

我有一个使用fork()和pipe()的标准程序,目的是为子进程中的第三方程序调用system()调用并将输出重定向到父进程。我发现如果我这样做,父进程永远不会检测到子进程已关闭管道,因此它永远不能从调用read()的while循环中退出。在Linux中,使用pipe()从分叉进程调用system()

当我使用其他一些通用系统调用(如“ls -l”)替换system()调用第三方程序时,问题消失。使用system()影响此程序的第三方程序调用可能存在什么潜在问题?

#include <iostream> 
#include <fstream> 
#include <stdlib.h>//system 
#include <sys/wait.h> 

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

    //setup pipe 
    int pipeid_L1[2]; 
    pipe(pipeid_L1); 

    pid_t pid_L1; 
    pid_L1 = fork(); 
    if(pid_L1==-1){ 
    throw std::logic_error("Fork L1 failed"); 
    } 
    else if(pid_L1 ==0){//L1 child process 
    dup2(pipeid_L1[1],STDOUT_FILENO);//redirect standard out to pipe 
    close(pipeid_L1[0]); //child doesn't read 
    system(... some program ...); //making the system call to a third party program 
    close(pipeid_L1[1]); 
    exit(0); 
    } 
    else{ 

    //setup pipe 
    close(pipeid_L1[1]);  

    int buf_size=64; 
    char L1_buf[buf_size]; 

    while(read(pipeid_L1[0],L1_buf,buf_size)){ //this while loop never exits if I make the system call to the third party program 
     ... do stuff here ... 
    } 


    } 
    exit(EXIT_SUCCESS); 
} 

回答

1

问题是,只有在所有其他进程关闭管道的写入结束时,父级才会看到EOF。有三个相关的过程 - 您分叉的孩子,系统分叉和执行的shell以及您运行的实际程序。在程序实际退出之前,前两个将不会关闭管道的末端,因此在这种情况发生并且所有进程都退出之前,父级将不会看到EOF。

如果您希望父程序在程序关闭其标准输出后立即看到EOF,而不是等到它退出,则需要使用exec而不是system来消除这些额外的进程。

或者,您可以使用popen,它可以为您执行所有所需的fork/pipe/exec。