fgets在C中不返回NULL

时间:2012-10-25 03:03:08

标签: c pipe fgets

循环应该会破坏,但它会一直停留在fgets上。 process_P1通过管道与inputHandler对话。问题是inputHandlerprocess_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;
}

1 个答案:

答案 0 :(得分:1)

问题可能不在您显示的代码中。既然你提到了一个管道,你的问题可能就在与之相关的管道中 - 很可能,你在管道的一端做了一个dup2(),使它成为标准输入或标准输出,但你忘了关闭您复制的文件描述符,或者您忘记关闭另一端。在没有可以写入正在读取的管道的进程之前,fgets()不会终止。如果正在读取的进程仍然打开管道的写入结束,它将一直停留在读取中,等待它写入​​内容。

所以,仔细看看你的管道代码。在将一端复制到标准输入或标准输出后,请确保已关闭pipe()返回的值。