管道坏文件描述符错误

时间:2015-10-25 15:05:44

标签: c pipe

确定。所以我正在尝试创建一个能够创建3个进程的程序:A,B和C.我希望这些消息能够像这样传播:

std.input - > (A-> B-> C-> A) - > std.output

现在问题是当我运行这个程序时,我得到了:

  

错误:进程B出错:关闭(pipeB [1])3 4    (9;错误的文件描述符)

有趣的是,如果我注释掉前一行,那就是:

if(close(pipeB[0] == -1))       syserr("Error in process B: %s\n", "close(pipeB[0])")

运行程序后我没有收到任何错误消息。

char message[] = "Hello from process A!\n";
int buffA_len;
char buffA [BUF_SIZE];

int main (int argc, char *argv[])
{
    int pipeA[2];
    int pipeC[2];

if(pipe(pipeA) == -1)                       syserr("Error in pipeA\n");
if(pipe(pipeC) == -1)                       syserr("Error in pipeC\n");
    switch(fork()){
        case -1:
            syserr("Error in first fork()\n");
        case 0: ;
            int buffA_rec_len;
            int a = pipeA[1];
            int b = pipeA[0];
            if(close (pipeA[1]) == -1)      syserr("Error in process B: %s\n", "close(pipeA[1])");
            if(close (0)        == -1)      syserr("Error in process B: %s\n", "close(0)");
            if(dup   (pipeA[0]) == -1)      syserr("Error in process B: %s\n", "dup(pipeA[0])");
            if(close (pipeA[0]) == -1)      syserr("Error in process B: %s\n", "close(pipeA[0])");


        int pipeB[2];
        if(pipe(pipeB) == -1)           syserr("Error in pipeB\n");

        switch(fork()){
            case -1:
                syserr("Error in second fork()\n");
            case 0:

                if(close (pipeB[1]) == -1)      syserr("Error in process C: %s\n", "close(pipeB[1])");
                if(close (0) == -1)             syserr("Error in process C: %s\n", "close(0)");
                if(dup(pipeB[0]) == -1)         syserr("Error in process C: %s\n", "dup(pipeB[0])");
                if(close (pipeB[0]) == -1)      syserr("Error in process C: %s\n", "close(pipeB[0])");

                if(close(pipeC[0]) == -1)       syserr("Error in process C: %s\n", "close(pipeC[0])");
                if(close (1)   == -1)           syserr("Error in process C: %s\n", "close(1)");
                if(dup(pipeC[1]) == -1)         syserr("Error in process C: %s\n", "dup(pipeC[1])");
                if(close (pipeC[1]) == -1)      syserr("Error in process C: %s\n", "close(pipeC[1])");
                exit(0);
            default:
                if(close (1)   == -1)           syserr("Error in process B: %s\n", "close(1)");
                if(dup(pipeB[1]) == -1)         syserr("Error in process B: %s\n", "dup(pipeB[1])");

/*Here are those lines!*/

                int b0 = pipeB[0];
                int b1 = pipeB[1];
                if(close(pipeB[0] == -1))       syserr("Error in process B: %s\n", "close(pipeB[0])");
                if(close(pipeB[1] == -1))       syserr("Error in process B: %s %d %d\n", "close(pipeB[1])",b0,b1);


                if(close(pipeC[0]) == -1)       syserr("Error in process B: %s\n", "close(pipeC[0])");
                if(close(pipeC[1]) == -1)       syserr("Error in process B: %s\n", "close(pipeC[1])");
                exit(0);
        }


        exit(0);
    default:
        if(close(pipeA[0]) == -1)   syserr("Error in process A: %s\n", "close(pipeA[0])");
        if(close(pipeC[1]) == -1)   syserr("Error in process A: %s\n", "close(pipeC[1])");


}

}

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

以下几行试图关闭int的{​​{1}}值:

pipeB[0] == -1

您实际上在寻找:

if(close(pipeB[0] == -1))       syserr("Error in process B: %s\n", "close(pipeB[0])");
if(close(pipeB[1] == -1))       syserr("Error in process B: %s %d %d\n", "close(pipeB[1])",b0,b1);

请注意比较如何超出if(close(pipeB[0]) == -1) syserr("Error in process B: %s\n", "close(pipeB[0])"); if(close(pipeB[1]) == -1) syserr("Error in process B: %s %d %d\n", "close(pipeB[1])",b0,b1); 调用参数。