【发布时间】:2013-04-10 20:38:10
【问题描述】:
所以这是我的代码的 sn-p:
pid_t children[MAX_PIPES];
for (i = 0; i < numPipes; i++)
{
pid_t child_pid = fork();
children[i] = child_pid;
switch(child_pid)
{
case 0:
// Some code thats executed in the child that executes execv()
break;
case -1: // Unable to create child
perror("fork");
break;
}
}
// Wait for children
for (j = 0; j < numPipes && children[j] != 0; j++)
{
if (background)
waitpid(children[j], &status, WNOHANG);
else
waitpid(children[j], &status, 0);
}
目前,当我只调用一次 execv() 时它工作正常,但我试图在已经分叉的子进程中一个接一个地使用 execv() 执行多个命令。
如何添加该功能?我试图在孩子内部的循环中添加另一个 fork(),但没有成功。
任何想法将不胜感激。谢谢
【问题讨论】:
-
您可以查看What is the proper way to pipe when making a shell 或Pipe stream and child processes 或Pipe communication in C 或Multiple processes and pipes 中的代码,或者毫无疑问还有很多其他代码;这些只是我在过去几个月中看到的一些。
标签: c