在协同处理中读取后,子进程待处理

时间:2012-06-22 03:37:03

标签: c linux ipc pipe

我正在使用管道编写协处理程序。当孩子读取一些数据,处理并输出数据时,它可以正常工作。但是当我读取所有数据并处理它时,它就等待了。任何人都有一些想法吗?谢谢。

以下是源代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>


int main()
{
    #define MAXSIZE 1024

    char workload[MAXSIZE];
    char result[MAXSIZE];
    workload[strlen(workload)] = EOF;
    int workload_size = strlen(workload);

    int fd1[2], fd2[2];
    int n;
    pid_t pid;
    if (pipe(fd1) < 0 || pipe(fd2) < 0) {
        fprintf(stderr, "pipe error: %s\n", strerror(errno));
        exit(1);
    }

    if ((pid = fork()) < 0) {
        fprintf(stderr, "fork error: %s\n", strerror(errno));
        exit(1);
    } else if (pid > 0) {
        close(fd1[0]);
        close(fd2[1]);
        while(fgets(workload, MAXSIZE, stdin) != NULL)
        {
            workload_size = strlen(workload);
            if (write(fd1[1], workload, workload_size) != workload_size) {
                fprintf(stderr, "write to pipe error: %s\n", strerror(errno));
                exit(1);
            }

            if ((n = read(fd2[0], result, MAXSIZE)) < 0) {
                fprintf(stderr, "read from pipe error: %s\n", strerror(errno));
                exit(1);
            }

            if (n == 0) {
                fprintf(stderr, "child closed the pipe\n");
                exit(1);
            }

            result[n] = 0;

            if (puts(result) == EOF) {
                fprintf(stderr, "fputs error\n");
                exit(1);
            }
        }
    } else {
        close(fd1[1]);
        close(fd2[0]);
        if (fd1[0] != STDIN_FILENO) {
            if (dup2(fd1[0] ,STDIN_FILENO) != STDIN_FILENO) {
                fprintf(stderr, "dup2 error to stdin.\n");
                exit(1);
            }
            close(fd1[0]);
        }
        if (fd2[1] != STDOUT_FILENO) {
            if (dup2(fd2[1] ,STDOUT_FILENO) != STDOUT_FILENO) {
                fprintf(stderr, "dup2 error to stdout.\n");
                exit(1);
            }
            close(fd2[1]);
        }

        if (execl("./a.out", "a.out", NULL) < 0) {
            fprintf(stderr, "execl error: %s\n", strerror(errno));
            exit(1);
        }
        exit(0);
    }

    return 0;
}

以下是a.out的源代码,它适用于此:

#include <stdio.h>
#include <string.h>

int main()
{
    #define MAXSIZE 1024
    char x[MAXSIZE];
    int n;
    while(scanf("%s", x) != EOF)
    {
        printf("len:%d %s", strlen(x), x);
        fflush(stdout);
    }
    return 0;
}

但是当我编写这样的代码时似乎正好等待:

#include <stdio.h>
#include <string.h>

int main()
{
    #define MAXSIZE 1024
    char x[MAXSIZE];
    int n;
    while(scanf("%s", x) != EOF);
    printf("Ok\n");
    fflush(stdout);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

您应该在不feof的情况下进行测试和循环,并且可以使用popen & pclose

您可能希望使用某些多路复用系统调用,如poll

答案 1 :(得分:1)

使用scanf调用%s的方式可能会溢出x缓冲区。您至少应该使用宽度修饰符修改scanf。

#include <stdio.h>
#include <string.h>

int main()
{
    #define MAXSIZE 1024
    char x[MAXSIZE];
    int n;
    while(scanf("%1024s", x) != EOF)
    {
        printf("len:%d %s", strlen(x), x);
        fflush(stdout);
    }
    return 0;
}

同样适用于您的其他计划。

您的程序被阻止的原因是因为您的第二个a.out程序循环执行另一个scanf,同时父程序正在尝试将响应读回result }。