0

循环应该中断,但它一直卡在fgets. 通过管道process_P1交谈。inputHandler问题是inputHandler没有意识到什么时候停止写作......永远不会达到process_P1'lalala' 。printf

void process_P1(char *argv[], int fd[2], pid_t child)
{
    int bytes = 0;

    static char bufferIn[BUFFER_SIZE];
    static char bufferOut[BUFFER_SIZE];

    char line[BUFFER_SIZE];

    // close reading end of pipe
    close(fd[0]);

    FILE *in = fopen(getInput(argv), "r");
    FILE *out = fdopen(fd[1], "w");

    if (in == NULL) {
            sys_err("fopen(r) error (P1)");
    }
    int ret = setvbuf(in, bufferIn, _IOLBF, BUFFER_SIZE);
    if (ret != 0) {
            sys_err("setvbuf error (P1)");
    }

    if (out == NULL) {
            sys_err("fdopen(w) error (P1)");
    }
    ret = setvbuf(out, bufferOut, _IOLBF, BUFFER_SIZE);
    if (ret != 0) {
            sys_err("setvbuf error (P1)");
    }

    while (fgets(line, BUFFER_SIZE, in) != NULL)
    {
            fprintf(out, "%s", line);
            bytes += count(line) * sizeof(char);
    }

    // alert P2 to stop reading
    //fprintf(out, "%s", STOP);

    fclose(in);
    fflush(out);
    fclose(out);

    printf("P1: file %s, bytes %d\n", getInput(argv), bytes);

    // wait P2 ends
    if (waitpid(child, NULL, 0) < 0) {
            sys_err("waitpid error (P1)");
    }
}

void *inputHandler(void *args)
{
    int ret;

    static char bufferIn[BUFFER_SIZE];
    char line[BUFFER_SIZE];
    struct node *iterator;

    int *fd = (int*)args;

    close(fd[1]);

    FILE *in = fdopen(fd[0], "r");
    if (in == NULL) {
            sys_err("fdopen(r) error (P2)");
    }
    ret = setvbuf(in, bufferIn, _IOLBF, BUFFER_SIZE);
    if (ret != 0) {
            sys_err("setvbuf(in) erro (P2)");
    }

    while (fgets(line, BUFFER_SIZE, in) != NULL)
    {
    //      printf("%s", line);
            iterator = firstArg;
            while (iterator->next != NULL)
            {
                    ret = sem_wait(&(iterator->sem));
                    if (ret == 0) {
                            strcat(iterator->buffer, line);
                    } else {
                            sys_err("sem_wait error");
                    }
                    ret = sem_post(&(iterator->sem));
                    if (ret != 0) {
                            sys_err("sem_post error");
                    }
                    iterator = iterator->next;
            }
            line[0] = '\0';
    }
    printf("lalala\n");
    iterator = firstArg;
    while (iterator->next != NULL)
    {
            ret = sem_wait(&(iterator->sem));
            if (ret != 0) {
                    sys_err("sem_wait error");
            }
            iterator->shouldStop = 1;
            ret = sem_post(&(iterator->sem));
            if (ret != 0) {
                    sys_err("sem_post error");
            }
            iterator = iterator->next;
    }

    fclose(in);

    return NULL;
}
4

1 回答 1

1

问题可能不在您显示的代码中。既然你提到了一个管道,你的问题可能出在与它相关的管道上——而且最有可能的是,你dup2()在管道的一端做了一个使其成为标准输入或标准输出,但你忘记关闭你的文件描述符重复,或者您忘记关闭另一端。在fgets()没有进程可以写入它正在读取的管道之前,它不会终止。如果正在读取的进程仍然打开管道的写入端,它将停留在读取中,等待它写一些东西。

因此,请仔细查看您的管道代码。pipe()确保在将一端复制到标准输入或标准输出后关闭了返回的两个值。

于 2012-10-25T05:03:12.153 回答