我的操作系统类中的赋值要求我通过在同一程序上递归调用exec来构建二进制进程树.目标是将一些任意任务拆分为单独的进程.父母应该通过未命名的管道与孩子和父母进行交流.这个想法是父母将每个孩子的一半工作发送,并且这继续递归,直到满足基本案例,其中传递给每个孩子的字符串的长度<= 2.然后孩子处理这些数据并将结果发回通过管道给父母.
为了更好地理解双向通信如何与c中的管道一起工作,我在继续实际分配之前创建了以下简单程序.父节点从不从子进程读取数据.我期待输出......
在父母| 收到的消息:测试
相反,当我打印时,我得到......
在父母| 收到消息:
似乎buff是空的,而不是从子进程中读取.有人可以解释我做错了什么和/或标准方式
我需要使用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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新
根据Jonathon的回答,我修改了传递给execvp的arg2数组到......
char *argv2[] = {"two_way_pipes", "1", NULL};
execvp("two_way_pipes", argv2);
Run Code Online (Sandbox Code Playgroud)
这并没有解决问题.父母仍然无法从客户端读回"测试".然而,为了回应乔纳森的回答和威廉的评论,我开始调整我的执行电话,并且出于某种原因将其更改为下面的线下节目.
execl("two_way_pipes", "two_way_pipes", "1", NULL);
Run Code Online (Sandbox Code Playgroud)
我很乐意接受任何解释,解释为什么execvp调用不起作用,但execl调用.
除了提到的问题由乔纳森·莱因哈特,最有可能调用execv()失败.
要测试这个修改这些行
execvp("two_way_pipes", argv2);
_exit(0);
Run Code Online (Sandbox Code Playgroud)
成为
...
#include <errno.h>
...
execvp("two_way_pipes", argv2); /* On sucess exec*() functions never return. */
perror("execvp() failed); /* Getting here means execvp() failed. */
_exit(errno);
Run Code Online (Sandbox Code Playgroud)
期待收到
execvp() failed: No such file or directory
Run Code Online (Sandbox Code Playgroud)
要修复此更改
execvp("two_way_pipes", argv2);
Run Code Online (Sandbox Code Playgroud)
成为
execvp("./two_way_pipes", argv2);
Run Code Online (Sandbox Code Playgroud)
如果孩子没有exec*()ed那么这一行
read(PARENT_READ, buff, 4); // should read "test" which was written by the child to stdout
Run Code Online (Sandbox Code Playgroud)
失败,反过来buff没有初始化,因此这一行
fprintf(stderr, "in parent | message received: %s\n", buff);
Run Code Online (Sandbox Code Playgroud)
挑起未定义的行为.
要buff通过更改来解决此问题至少正确初始化
char buff[5];
Run Code Online (Sandbox Code Playgroud)
成为
char buff[5] = "";
Run Code Online (Sandbox Code Playgroud)