我的程序,连同它的两个子进程,都以有序的方式从stdin读取输入。
我发现的问题是:
给定此输入:

32
51453a
140

父进程读取32,这意味着第一个子进程需要再读取2个数字。然后,父进程通过管道发送1个字节,向需要读取两个数字的第一个进程发送信号。当子进程接收到信号并开始读取这些数字时,子进程将读取32 51453a,而不是51453a和140。
我一直在努力理解为什么会发生这种情况,我如何能解决它或更好,我如何能避免它。
我在Mac OSX上运行
编辑:此问题仅在从文件(running./polygon还添加了更多代码以提供更好的图片。
void runWriterProcess(char *outFile, int writerFd[2]) {
    close(writerFd[1]);
    close(STDIN_FILENO);
    dup(writerFd[0]);
    close(writerFd[0]);
    char *const params[] = {"./writer", outFile, NULL};
    execv("./writer", params);
}
void reader32(int INSIG, int OUTPUT) {
    long unsigned polygonParts[2];
    char runSignal[2], polygonBuffer[17];
    int64 nextPolygon;
    while(read(INSIG, runSignal, 1) > 0) {
        scanf("%lx", &polygonParts[0]);
        scanf("%lx", &polygonParts[1]);
        nextPolygon = polygonParts[1];
        nextPolygon = nextPolygon << 32;
        nextPolygon += polygonParts[0];
        sprintf(polygonBuffer, "%16llx", nextPolygon);
        write(OUTPUT, polygonBuffer, (int)strlen(polygonBuffer));
    }
    finishError("reader32\0");
    exit(EXIT_SUCCESS);
}

void reader64(int INSIG, int OUTPUT) {
    char runSignal[2], polygonBuffer[17];
    int64 nextPolygon;
    while(read(INSIG, runSignal, 1) > 0) {
        scanf("%16llx", &nextPolygon);
        sprintf(polygonBuffer, "%16llx", nextPolygon);
        write(OUTPUT, polygonBuffer, 16);
    }
    finishError("reader64\0");
    exit(EXIT_SUCCESS);
}

void runMainLoop(int reader32, int reader64, int readPipe) {
    long unsigned dummy[2];
    int64 bigDummy, nextPolygon;
    char polygonBuffer[17];
    int readerToRun;
    for(;;) {
        scanf("%d", &readerToRun);
        if (readerToRun == 32) {
            write(reader32, "1", 1);
        } else {
            write(reader64, "1", 1);
        }
        read(readPipe, polygonBuffer, 16);
        sscanf(polygonBuffer, "%16llx", &nextPolygon);
        if(runOnPolygon(nextPolygon)) break;
    }
    write(reader64, "0\n", 2);
}

void createReaders(pid_t *reader32pid, pid_t *reader64pid) {
    int fd32[2], fd64[2], fdBoth[2], readerToRun;
    long unsigned polygonParts[2];
    int64 nextPolygon, dummy;
    char runSignal[2], polygonBuffer[17];
    polygonBuffer[16] = '\0';
    pipe(fd32);
    pipe(fd64);
    pipe(fdBoth);
    if ((*reader32pid = fork()) == 0) {
        close(fd32[1]);
        close(fd64[0]);
        close(fd64[1]);
        close(fdBoth[0]);
        reader32(fd32[0], fdBoth[1]);
    }
    if ((*reader64pid = fork()) == 0) {
        close(fd32[0]);
        close(fd32[1]);
        close(fd64[1]);
        close(fdBoth[0]);
        reader64(fd64[0], fdBoth[1]);
    }
    close(fd32[0]);
    close(fd64[0]);
    close(fdBoth[1]);
    runMainLoop(fd32[1], fd64[1], fdBoth[0]);
    close(fd32[1]);
    close(fd64[1]);
    wait(NULL);
    wait(NULL);
    finishError("main_process\0");
}

void finishError(char processName[13]) {
    char output[50];
    sprintf(output, "%s pid=%d is going to exit\n", processName, getpid());
    write(STDERR_FILENO, output, strlen(output) + 1);
}

int main(int argc, const char *argv[])
{
    polygonList.head = NULL;
    polygonList.tail = NULL;
    pid_t writerPid, reader32pid, reader64pid;
    int writerFd[2];
    char outFile[11];
    scanf("%s", outFile);
    pipe(writerFd);
    if (fork() == 0) {
        runWriterProcess(outFile, writerFd);
    } else {
        close(STDOUT_FILENO);
        dup(writerFd[1]);
        close(writerFd[0]);
        close(writerFd[1]);
        createReaders(&reader32pid, &reader64pid);
    }
    freeList();
    return 0;
}

最佳答案

@一些程序员已经解释了为什么会这样。如果确实无法避免从标准输入中读取父项和子项(或多个子项),则可以通过setvbuf()函数将其设置为无缓冲。如果你这样做了,你应该首先这样做,然后才能从stdin中读取任何内容:

int result = setvbuf(stdin, NULL, _IONBF, 0);
if (result != 0) {
    // handle error
}

请注意,这会影响性能。

10-07 23:13