尋找fork一個進程,在C++中,它不會掛起它的父進程 - 它的父進程是一個守護進程並且必須保持運行。如果我在分叉進程中等待()分叉execl不會defunt - 但是 - 它也將掛起應用程序 - 不等待修復應用程序掛起 - 但命令成爲defant。C++ fork,無需等待,解除execl
if((pid = fork()) < 0)
perror("Error with Fork()");
else if(pid > 0) {
//wait here will hang the execl in the parent
//dont wait will defunt the execl command
//---- wait(&pid);
return "";
} else {
struct rlimit rl;
int i;
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; (unsigned) i < rl.rlim_max; i++)
close(i);
if(execl("/bin/bash", "/bin/bash", "-c", "whoami", (char*) 0) < 0) perror("execl()");
exit(0);
}
如何,我可以用叉子叉execl的無等待(& PID),其中EXECL的命令不會倒閉?
UPDATE 由叉
signal(SIGCHLD, SIG_IGN);
在基於公認的答案更兼容的解決方案以我有限的技能還是工作之前添加下列固定。謝謝!
最簡單的解決方案是添加一個信號(SIGCHLD,SIG_IGN);在我的前叉之前 - 我嘗試了幾乎所有其他選項,這些選項似乎超出了我的有限技能。我會繼續努力以防萬一人想要使用2.4.x. – 2012-02-07 02:18:57