c - Pipe、Fork 和 Exec - 父子进程之间的双向通信

标签 c exec fork pipe

我的操作系统类中的一项作业要求我通过对同一程序递归调用 exec 来构建二进制进程树。目标是将一些任意任务拆分为单独的进程。 parent 应该与 child 沟通,而 child 只能通过未命名的管道与 parent 沟通。这个想法是 parent 向每个 child 发送一半的工作,并且递归地继续,直到满足基本情况,其中传递给每个 child 的字符串的长度 <= 2。然后 child 处理这些数据并将结果发回通过管道传递给父级。

为了更好地理解双向通信如何使用 c 中的管道进行工作,我在进行实际作业之前创建了以下简单程序。父进程从不读取子进程的数据。我期待输出...

in parent | message received: test

相反,当我打印时,我得到...

in parent | message received:

看来buff是空的,没有从子进程中读取。有人可以解释我做错了什么和/或标准的方式吗

  1. 写给 parent 执行的 child
  2. 从 exec'd child 的 parent 读取
  3. 从被执行的 child 那里写回给 parent
  4. 从父级执行的子级读取

我需要使用 exec()、pipe()、fork()。谢谢。

/**
 * *********************************
 * two_way_pipes.c
 * *********************************
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h> 
#include <sys/types.h> 
#include <unistd.h>

#define PARENT_READ read_pipe[0]
#define PARENT_WRITE write_pipe[1]
#define CHILD_WRITE read_pipe[1]
#define CHILD_READ  write_pipe[0]

#define DEBUGGING 1

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

    // in the child process that was exec'd on the orginal call to two_way_pipes
    if(argc == 2) {
        read(STDIN_FILENO, buff, 4); // this should read "test" from stdin
        buff[4] = '\0';
        fprintf(stdout, "%s\n", buff); // this should right "test" to stdout and be read by the parent process
    // int the root process, the original call to two_way_pipes with no args
    } else {
        int pid;
        int read_pipe[2];
        int write_pipe[2];

        pipe(read_pipe);
        pipe(write_pipe);

        pid = fork();

        // parent process
        if(pid > 0) {
            close(CHILD_READ);
            close(CHILD_WRITE);

            write(PARENT_WRITE, "test", 4); // attempting to write this to the child

            struct timeval tv;
            fd_set readfds;
            tv.tv_sec = 10;
            tv.tv_usec = 0;
            FD_ZERO(&readfds);
            FD_SET(PARENT_READ, &readfds);
            select(PARENT_READ + 1, &readfds, NULL, NULL, &tv);

            if(FD_ISSET(PARENT_READ, &readfds)) {
                read(PARENT_READ, buff, 4); // should read "test" which was written by the child to stdout
                buff[4] = '\0';
                close(PARENT_READ);
                close(PARENT_WRITE);
                fprintf(stderr, "in parent | message received: %s\n", buff);  // "test" is not in buff
            }

        // child process
        } else if(pid == 0) {
            close(PARENT_READ);
            close(PARENT_WRITE);

            dup2(CHILD_READ, STDIN_FILENO);
            dup2(CHILD_WRITE, STDOUT_FILENO);
            close(CHILD_READ);
            close(CHILD_WRITE);

            char *argv2[] = {"some random arg to make sure that argc == 2 in the child", NULL};
            execvp("two_way_pipes", argv2);
            _exit(0);
        // error forking child process
        } else {
            fprintf(stderr, "error forking the child\n");
        }
    }
}

更新

根据 Jonathon 的回答,我将传递给 execvp 的 arg2 数组修改为...

char *argv2[] = {"two_way_pipes", "1", NULL};
execvp("two_way_pipes", argv2);

这并没有解决问题。 parent 仍然无法从客户那里读回“测试”。然而,为了回应 Jonathon 的回答和 William 的评论,我开始调整我的 exec 调用,并且出于某种原因将其更改为下面的行显示。

execl("two_way_pipes", "two_way_pipes", "1", NULL);

我很乐意接受任何解释为什么 execvp 调用不起作用但 execl 调用起作用的答案。

最佳答案

此外the issue mentioned通过 Jonathon Reinhart ,很可能调用 execv() 失败。

要测试这个修改这些行

execvp("two_way_pipes", argv2);
_exit(0);

成为

...
#include <errno.h>
...


execvp("two_way_pipes", argv2); /* On sucess exec*() functions never return. */
perror("execvp() failed); /* Getting here means execvp() failed. */
_exit(errno);

期待收到

execvp() failed: No such file or directory

修复此更改

execvp("two_way_pipes", argv2);

成为

execvp("./two_way_pipes", argv2);

另外,如果 child 不是 exec*()ed 那么这一行

read(PARENT_READ, buff, 4); // should read "test" which was written by the child to stdout

失败,反过来 buff 没有初始化,因此这一行

fprintf(stderr, "in parent | message received: %s\n", buff);  

引发未定义的行为。

要解决此问题,至少要通过更改来正确初始化 buff

char buff[5];

成为

char buff[5] = "";

关于c - Pipe、Fork 和 Exec - 父子进程之间的双向通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191030/

相关文章:

c - 使用 fputs 写入文件

c - 使用 1 个管道通过 fork() 设置四个进程

c - 局部变量不是静态的,但为什么会出现这种行为?

c - 对于包括设置在内的小型 C 程序,合理的最少汇编指令数是多少?

find - 更改 {} 的参数 find exec

python - 在 python 的 for 循环中使用 exec

c - fork 再次写入文件句柄

c - fork() 流程执行结束后的输出

c - 如何找到缓冲区溢出和内存损坏的地方?

java - 如何从 Java 程序启动完全独立的进程?