C - 在 coprocess 中读取后挂起的子进程



我正在使用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: %sn", strerror(errno));
        exit(1);
    }
    if ((pid = fork()) < 0) {
        fprintf(stderr, "fork error: %sn", 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: %sn", strerror(errno));
                exit(1);
            }
            if ((n = read(fd2[0], result, MAXSIZE)) < 0) {
                fprintf(stderr, "read from pipe error: %sn", strerror(errno));
                exit(1);
            }
            if (n == 0) {
                fprintf(stderr, "child closed the pipen");
                exit(1);
            }
            result[n] = 0;
            if (puts(result) == EOF) {
                fprintf(stderr, "fputs errorn");
                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: %sn", 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("Okn");
    fflush(stdout);
    return 0;
}

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

#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

你应该在不是feof的时候进行测试和循环,你可以使用popen和pclose。

您可能希望使用一些多路复用系统调用,例如轮询

最新更新