用c ++管道()管道管道

时间:2012-02-09 21:54:49

标签: pipe sorting c++ fork

我目前正在尝试连接父项及其子项之间的管道。孩子们正在执行sort并且正在对他们从父母那里收到的输入进行排序。孩子然后写一个单独的管道。每个过程有两个管道。一个父母可以向孩子发送输入。另一个,所以父母可能会收到排序的结果。

到目前为止我的问题一直在阅读输入。我已经从fputs()得到确认我已成功写入孩子的输入。之后我fflush(NULL)并尝试阅读孩子的输出。读取块在其中永远不会返回或到达fputs之后的语句。这是相当奇怪的,因为我相信我已将读数设置为O_NONBLOCK。输出如下所示。

line 174 
line 176 
line 178 

以下是一段代码:

int sort_writes[num_sort][2];
    int sort_reads[num_sort][2];
    int i;
    int *status;
    int flags;
    char buffer[BUFFER_SIZE];
    // this should contain a bunch of write fds wrapped in fdopen
    FILE*  to_sort[num_sort];
    // the same except with reads
    FILE*  from_sort[num_sort];
    //this only include for simplicity and so that exec will happen proper
    char *sort_argv[2];
    sort_argv[0]=(char*)"sort";
    sort_argv[1]= (char *)NULL;

    // This will create all of the pipes for the sorts.
    // The parent will read  0 and the even pipes. it will write to the odd.
    for(i=0; i< num_sort; i++){
            //parent  reads from this pipe. child writes to it.
                    assert(pipe(sort_writes[i]) == 0);

            //parent write to this pipe. child reads from it.
                    assert(pipe(sort_reads[i]) ==0);
                    switch(fork()){
                            case 0: //this is the child
                                    //this closes unnecessary fds
                                    _close_less_than(i, sort_writes);
                                    _close_less_than(i, sort_reads);
                                    dup2(sort_reads[i][0], STDIN_FILENO);
                                    // standard out becomes parent pipe in
                                    dup2(sort_writes[i][1], STDOUT_FILENO);
                                                                                                   execv(SORT_LOC.c_str(), sort_argv);
                            default: // this the parent.
                                    //clean up. close unused.
                                    close(sort_writes[i][1]);
                                    close(sort_reads[i][0]);
                    }

    }
    //Creates a file pointer for all of the fds I will use to communicate with my sorts
    //It also sets all reads to nonblock and the parent write stdio buffers to zero.        
    for(i=0; i< num_sort; i++){
            assert((from_sort[i]= fdopen(sort_writes[i][0] ,"r")) != NULL);
            assert((to_sort[i]= fdopen(sort_reads[i][1] , "w")) != NULL);         //pipes ignore truncate

            flags = fcntl(sort_writes[i][0], F_GETFL);
            flags |= O_NONBLOCK;
            fcntl(sort_writes[i][0], F_SETFL, flags);


    }

    for(i=0; i<(int)theArray.size(); i++){
            fputs(theArray.back().c_str(), to_sort[i % num_sort]);
            theArray.pop_back();
            fflush(NULL); // so that the data gets from stdio buffers to pipe buffers.      
    }
    cout << "line 174 \n";
    for(i=0; i <1; i++){
            cout << "line 176 \n";
            while(!feof(from_sort[i])){
                    cout << "line 178 \n";
                    cout << fgets(buffer, BUFFER_SIZE, from_sort[i]);
                    cout << buffer;
                    cout << "at least i tried \n";
            }

1 个答案:

答案 0 :(得分:2)

您在dup2之前忘记了closeexec。使用您的代码,sort不知道要使用哪个文件描述符。我不知道_close_less_than做了什么,但是如果它关闭了描述符0,1或2,那么你将使用一个无法理解的环境调用sort。你必须将你的管子连接到孩子的stdin和stdout。