0
我有一个函数,说pfoo(),我想通过调用fork来创建一个子进程。这个函数被重复调用很多次,但我希望只在第一次调用该函数时创建子进程。fork()函数里面只有第一次调用的函数
有没有可能做到这一点? 我应该如何更改下面的函数以确保fork()仅在第一次被调用?我需要缓存cpid吗?
void pfoo()
{
pid_t cpid = fork();
if (cpid == 0)
{
// child process
}
else
{
// parent process
}
....
....
}
'static int forked = 0; if(!forked){...}' – StoryTeller
'static pid_t cpid = 0;如果(cpid == 0){...}' – ikegami
@ikegami - 我想到了,但如果OP想要在子进程中阻止它分叉呢? – StoryTeller