c - 父进程和子进程使用管道进行通信的问题

标签 c pipe inter-process-communicat

我正在编写一个 C 程序,它给出了可执行文件 a.out 和输入文件 in_1.txtin_2.txt 。 .. in_n.txt,将在所有 n 个输入文件上运行 a.out,并为文件中的每个输入文件生成相应的输出参数列表。
我的程序遇到了一些奇怪的行为(称之为 tester.out),几个小时后,我能够识别我的问题,但不知道如何解决它。

我会先发布代码,然后解释问题...

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<limits.h>
#include<string.h>

#define PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP  // 660

int main(int argc, char* argv[]) {

    if (argc<3) {
        printf("Arguments required; an executable file and at least one input file.\n");
        return 1;
    }

    int channel[2];
    if (pipe(channel)<0) {
        perror("pipe");
        return 1;
    }

    int i;
    for (i=2; i<argc; ++i) {

        pid_t p=fork();
        if (p<0) {
            perror("fork");
            return 1;
        }

        if (p) { // parent

            // closing read end
            close(channel[0]); // *** The problem lies here! ***

            int indicator;

            if ((indicator=write(channel[1], argv[i], strlen(argv[i])+1))<0) {
                perror("write");
                return 1;
            }

            printf("Parent wrote %d bytes\n", indicator);

            int status;
            wait(&status);

            if (WIFEXITED(status)) {
                printf("Your program terminated normally with exit code %d\n", WEXITSTATUS(status));
                printf("See output file: %s_output\n", argv[i]);
            }
            else if (WIFSIGNALED(status)) {
                printf("Your program terminated by a signal! signal code: %d\n", WTERMSIG(status));
            }

        }
        else { // child

            // closing write end
            close(channel[1]);

            char input_file[PATH_MAX]={0};
            char output_file[PATH_MAX+10]={0};

            int indicator;

            // read input_file from pipe
            if ((indicator=read(channel[0], input_file, PATH_MAX))<1) {
                perror("child process: read");
                return 1;
            }

            printf("child read %d bytes\n", indicator);

            sprintf(output_file, "%s_output", input_file);
            printf("Got %s and output is %s\n", input_file, output_file);

            sleep(5); /* later, I will call execl() here to run argv[1] on input_file */
            return 1;
        }



    }



    return 0;
}

当我运行时,这按预期工作:

$ ./tester.out a.out input1.txt

但是当我使用多个输入文件运行它时会失败:

$ ./tester.out a.out input1.txt input2.txt

正如代码中突出显示的注释所示,问题在于调用 close(channel[0]) 的行,因为在第一次迭代中的父进程,意味着在第二次迭代中,子进程关闭了其channel[0](由其父进程 - 在第一次迭代)。

对于如何解决这种情况有什么建议吗?

最佳答案

您应该为每个子进程创建一个新管道,并在完成后关闭管道的两端。您需要关闭父级和子级的两端。

关于c - 父进程和子进程使用管道进行通信的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27187856/

相关文章:

c - 两个 child 在 pipe 里读书

sockets - 如何在本地主机中捕获两个端口之间的数据包

c - 如何检查引导加载程序项目中找到的应用程序请求

linux - 在 bash 中评估代表单个管道命令的两个变量的最佳方法是什么?

mysql - c程序添加一个用户到mysql

windows - 在函数调用窗口批处理脚本中转义 PIPE 字符

c - 如何在执行某些操作之前确保其他线程已完成

c - 如何将套接字控制传递给子进程?

C 将行读取为字符串

CryptoAPI RSA Schannel 提供程序错误