gpt4 book ai didi

c - 使用 exec 程序从父级读取和写入多个子级

转载 作者:行者123 更新时间:2023-11-30 17:28:07 27 4
gpt4 key购买 nike

我对 C 还很陌生,不确定如何继续。

使用此代码,我尝试创建多个子进程,这些子进程会将其标准输出发送到其父级标准输入,并可使用数组中指针位置的 fdprintf 写入其标准输入。

当使用读取标准输入并打印到其标准输出(应该通过管道返回)的基本程序执行时,代码似乎不起作用。 (在主代码的不同部分中,我 fprintf 到管道开始的位置,然后读取 stdin 等待应写回的内容)。

int plumber(int *pipes[], int numChildren, char* command[]) {
int i;
char id;
int nullSpace = open("/dev/null", O_WRONLY);

for(i = 0; i < numChildren; ++i) {
id = 'A' + i;
pipe(pipes[2 * i]);
pipe(pipes[2 * i + 1]);
switch(fork()) {
case (-1):
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
break;
case 0:
//child
//close child's write, dupe its stdin to read
//close childs old read
close(pipes[2 * i][1]);
if(dup2(pipes[2 * i][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i][0]);
//close child's read, dupe its stdout to write
//close childs old write
close(pipes[2 * i + 1][0]);
if(dup2(pipes[2 * i + 1][1], 1) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][1]);
close(1);
//child stderr to nullspace
if(dup2(nullSpace, 2) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(2);
execlp(command[i], "childprocess", numChildren, id, NULL);
break;
default:
//parent
//close read pipe from writing pipe
close(pipes[2 * i][0]);
//close write pipes and dupe stdin to read
//close parents old read
close(pipes[2 * i + 1][1]);
if(dup2(pipes[2 * i + 1][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][0]);
}
}
close(nullSpace);
return 0;
}

该命令只是运行子进程,它还获取子进程的数量和从 A 到 D 的 id。 *pipes[] 是 numChildren*2 x 2 (因此沿着其子 1 读取管道,child1 写入管道,child2 读取,child2 写入等。请提前提供帮助并致谢。

最佳答案

父级只能有一个 stdin 。每次家长这样做时:

dup2(pipes[2 * i + 1][0], 0)

它正在关闭之前的 stdin并将其替换为管道的读取端。这意味着除了最后一个 child 之外的所有 child 都将拥有stdout。具有封闭的读取端,这应该会导致它们收到 SIGPIPE (如果 EPIPE 被忽略,则 SIGPIPE 错误)如果它们尝试生成任何输出。此外,父级现在已经丢失了原来的 stdin ,这可能重要也可能不重要。

您还应该检查execlp没有返回错误。如果确实如此,您将得到非常奇怪的行为,因为失败的子级将开始与父级同时生成所有剩余的子级。

如果您确实希望所有子级的输出都出现在单个 stdin 上,那么它们必须都使用相同的管道。然而,这将导致他们的输出随机混合,因此 parent 不可能知道谁发送了什么。如果父级仅将每个管道的读取端保留在其原始描述符中而不是尝试dup2,那就更好了。将它们设置为 0。这样,父级将保留其原始 stdin ,它可以使用 select (或 poll ,或...)来确定何时从哪个子项获取输入。

连接 child 的stdin通常是一个坏主意。和stdout到同一个进程(父进程),因为它很容易导致死锁。该管道只能缓冲有限数量的数据,并且在满时会对写入器产生反压(即阻塞)。如果父进程和子进程都向stdout写入大量数据s,它们都会受到背压,这将阻止它们读取输入并排空管道。您必须确保子级在生成自己的任何输出之前消耗掉父级的所有输出,或者找到一种方法来保证您始终可以从 stdin 读取内容。即使stdout被阻止。后者可以通过让单独的线程处理 stdin 来完成。和stdout ,或者通过启用非阻塞 I/O 并使用 select等来确定何时可以写入管道。

关于c - 使用 exec 程序从父级读取和写入多个子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26121869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com