linux wait 子进程,linux – 为什么我必须`wait()`用于子进程?

I’ll probably have to add a process table that stores the child pids

and have to use waitpid – not immideately, but after some time has

passed – which is a problem, because the running time of the children

varies from few microseconds to several minutes. If I use waitpid too

early, my parent process will get blocked

查看documentation for waitpid.您可以告诉waitpid不阻止(即,如果没有孩子可以立即返回)使用WNOHANG选项.而且,你不需要给waitpid一个PID.你可以指定-1,它会等待任何孩子.所以调用waitpid如下所示适合您的无阻塞约束和no-saving-pids约束:

waitpid( -1, &status, WNOHANG );

如果你真的不想正确地处理流程创建,那么你可以通过两次分享来获得init的收获责任,收获孩子,并给exec提交给孙子:

pid_t temp_pid, child_pid;

temp_pid = fork();

if( temp_pid == 0 ){

child_pid = fork();

if( child_pid == 0 ){

// exec()

error( EXIT_FAILURE, errno, "failed to exec :(" );

} else if( child_pid < 0 ){

error( EXIT_FAILURE, errno, "failed to fork :(" );

}

exit( EXIT_SUCCESS );

} else if( temp_pid < 0 ){

error( EXIT_FAILURE, errno, "failed to fork :(" );

} else {

wait( temp_pid );

}

在上述代码片段中,子进程分叉自己的子进程,立即存在,然后立即由父进程获取.孙子是孤儿,由init采用,将自动收回.

Why does Linux keep zombies at all? Why do I have to wait for my

children? Is this to enforce discipline on parent processes? In

decades of using Linux I have never got anything useful out of zombie

processes, I don’t quite get the usefulness of zombies as a “feature”.

If the answer is that parent processes need to have a way to find out

what happened to their children, then for god’s sake there is no

reason to count zombies as normal processes and forbid the creation of

non-zombie processes just because there are too many zombies.

你如何提出一个可以有效地检索进程的退出代码?问题是PID =退出代码(等)必须是一对一的.如果内核一旦退出,即可释放进程的PID,然后一个新的进程继承同一个PID并退出,那么你如何处理一个PID的两个代码?感兴趣的进程将如何检索第一个进程的退出代码?不要以为没有人关心退出代码.你认为是一个麻烦/ bug被广泛认为是有用和干净的.

On the system I’m currently developing for I can only spawn 400 to 500

processes before everything grinds to halt (it’s a badly maintained

CentOS system running on the cheapest VServer I could find – but still

400 zombies are less than a few kB of information)

关于使受到广泛接受的内核行为的一个替罪羊,显然是对维持不良/廉价系统的挫折显得不正确.

通常,您的最大进程数仅受您的内存限制.你可以看到你的极限:

cat /proc/sys/kernel/threads-max

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值