简体   繁体   English

写入管道总是失败

[英]Writing into pipe always fails

I am trying to communicate between 2 processes by trying to read data from text file into first pipe and read it on the other one but the write function always fails: 我试图通过尝试将文本文件中的数据读入第一个管道并在另一个管道上读取它来在2个进程之间进行通信,但是write函数始终失败:

Main: 主要:

int main(int argc, char* argv[])
{
    int readerPip[2], writerPip[2], reader, writer;

    if (pipe(readerPip))
    {
      fprintf (stderr, "Pipe failed.\n");
      return EXIT_FAILURE;
    }

    if(pipe(writerPip))
    {
      fprintf (stderr, "Pipe failed.\n");
      return EXIT_FAILURE;
    }

    close(readerPip[1]); // closing reader writing side pipe for main thread
    reader = fork();
    if (reader < 0)
    {
        perror("Cannot fork()");
        exit(EXIT_FAILURE);
    }
    else if (reader == 0)
    {
        do_reader(readerPip);
    }

    wait(NULL);
    close(writerPip[0]); // closing writer reader side pipe for main thread
    do_father(readerPip, writerPip);
    close(readerPip[0]); // closing reader reader side pipe for main thread
    close(writerPip[1]); // closing writer writing side pipe for main thread
    writer = fork();

    if (writer < 0)
    {
        perror("Cannot fork()");
        exit(EXIT_FAILURE);
    }
    else if (writer == 0)
    {
        do_writer(writerPip);
    }

    return 1;
}// main

do_Reader: do_Reader:

void do_reader(int readerPipe[])
{
    char stringCommand[17];
    long long unsigned command;

    close(readerPipe[0]);
    while (1)
    {
        scanf("%s", stringCommand);
        command = convertStringToPolygon(stringCommand);

        if ((command & 0xFFFFFFFF) == 0xFFFFFFFF)
        {
            break;
        }

        if (write(readerPipe[1], stringCommand, strlen(stringCommand) ) == -1)
        {
            printf("writing falied");
        }
        else
        {
            printf("success");
        }

        printf("\n");
    }

    close(readerPipe[1]);
    exit(0);
}

do_father 父亲

void do_father(int readerPipe[], int writerPipe[])
{
    long long unsigned nbytes;
    char currentPolygon[17];
    currentPolygon[16] = '\0';
    while (1)
    {
        nbytes = read(readerPipe[0], currentPolygon, sizeof(currentPolygon));
        if (nbytes == 0)
        {
            printf("done");
            break;
        }

        manageProgram(readerPipe, writerPipe,        convertStringToCommand(currentPolygon));
    }
}

do_writer do_writer

void do_writer(int pip[])
{
    close(pip[1]);
    long long unsigned command, nbytes;

    while (1)
    {
    nbytes = read(pip[0], &command, sizeof(command));
        if (nbytes == 0)
        {
            break;
        }
        if ((((command & THIRD_BIT_MASK) != FALSE) || ((command & FORTH_BIT_MASK) 
!= FALSE) ||
            ((command & FIFTH_BIT_MASK) != FALSE)) != FALSE)
            generateOutputDependsOnBits(command);
    }

    close(pip[0]);
    exit(0);
}

the rest of the code does not matter since writing for the reader pipe does not working. 其余代码无关紧要,因为为读取器管道进行的写入无法正常工作。 the text file looks like this: 文本文件如下所示:

3a 0000050000050505 3e 003cc40000c43c3c ba 000088ec9c32ce32 f8 cc 3a 000085ec9a32cd32 4c 8c 5c ac fd FFFFFFFFFFFFFFFF 3a 0000050000050505 3e 003cc40000c43c3c ba 000088ec9c32ce32 f8 cc 3a 000085ec9a32cd32 4c 8c 5c ac fd FFFFFFFFFFFFFFFFFF

You close readpipe[1] in main ( before the fork ) and close readpipe[0] in do_reader , so both sides of the pipe are closed in the child. 你接近readpipe[1]main fork )和密切readpipe[0]do_reader ,所以管的两侧在子被关闭。

Move the close(readpipe[1]) in main farther down (eg before the wait ). mainclose(readpipe[1])进一步向下移动(例如,在wait之前)。

But, you may not want to wait there. 但是,你可能不希望wait在那里。 You may want to do it after do_father as this would rely upon the kernel pipe buffers being large enough to hold all the data the child sends to the parent before the parent even tries to get the first byte. 您可能希望在do_father之后do_father此操作,因为这将取决于内核管道缓冲区足够大,以容纳在父级甚至尝试获取第一个字节之前,子级发送给父级的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM