[英]After fork(), how to keep running execve() in for() loop?
Here is an example, ignorning the error checking: 这是一个示例,初始化错误检查:
int main()
{
pid_t pid = fork();
if(0 == pid)
{
for(int i = 0; i < 5; ++i)
{
char* const args[] = { "/bin/ls", nullptr };
execve("/bin/ls", args, nullptr);
}
}
else if(pid > 0)
{
wait(nullptr);
}
}
If exec() after fork(), as far as I know, the linux will not copy but cover original system. 据我所知,如果在fork()之后执行exec(),Linux将不会复制而是覆盖原始系统。
If I want to keep running execve() in for() loop like this, what should I do ? 如果我想像这样继续在for()循环中运行execve(),该怎么办?
exec
(all of the different forms) will replace your current executable with the one given to exec
, so NOTHING you do within the forked code will matter. exec
(所有不同的形式) 都将当前可执行文件替换为exec
,因此您在分叉代码中所做的一切都不重要。 You need to either do a loop around fork
, or convince the author of the other program to run the loop for you. 您需要围绕
fork
进行循环,或者说服其他程序的作者为您运行循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.