Using waitpid to Reap Zombie Children

When a process terminates for any reason, the kernel does not remove it from the system immediately. Instead, the process is kept around in a terminated state until it is reaped by its parent. When the parent reaps the terminated child, the kernel passes the child's exit status to the parent, and then discards the terminated process, at which point it cease to exit. A terminated process that has not yet been reaped is called a zombie.

      A process waits for its children to terminate or stop by calling the waitpid function. The waitpid function suspends the execution of the calling process until a child process in its wait set terminates. If a process in the wait set has already terminated at the time of the cal, then waitpid function returns immediately. In either case, waitpid returns the PID of the terminated child that caused waitpid to return, and the terminated child is removed from the system.

      Here we are going to use waitpid function to reap zombie children both in no particular order and in the order they were created. First, consider the following code which reaping zombie children in no particular order.

#include "csapp.h"
const int N = 2;

int main()
{
    int status;
    pid_t pid;
    
	// Parent creates N children
    for (int i=0; i!=N; ++i)
        if ((pid=Fork()) == 0) // Child
            exit(100+i);

    // Parent reaps N children in no particular order
    while ((pid = waitpid(-1, &status, 0)) > 0) {
        if (WIFEXITED(status))
            printf("child %d terminated normally with exit status=%d\n",
                    pid, WEXITSTATUS(status));
        else
            printf("child %d terminated abnormally\n", pid);
    }
    
    // The only normal termination is if there are no more children
    if (errno != ECHILD)
        unix_error("waitpid error");

    exit(0);
}
The program reaps its children in no particular order. The order that they were reaped is a property of this specific computer system. On another system, or even another execution on the same system, the two children might have been reaped in the opposite order.

      A simple change that eliminates this nondeterminism in the output order by reaping the children in the same order that they were created by the parent is presented below.

#include "csapp.h"
const int N = 2;

int main()
{
    int status;
    pid_t pid[N], retpid;
    
	// Parent creates N children
    for (int i=0; i!=N; ++i)
        if ((pid[i]=Fork()) == 0) // Child
            exit(100+i);

    // Parent reaps N children in no particular order
    int i = 0;
    while ((retpid = waitpid(pid[i++], &status, 0)) > 0) {
        if (WIFEXITED(status))
            printf("child %d terminated normally with exit status=%d\n",
                    pid, WEXITSTATUS(status));
        else
            printf("child %d terminated abnormally\n", pid);
    }
    
    // The only normal termination is if there are no more children
    if (errno != ECHILD)
        unix_error("waitpid error");

    exit(0);
}

      The essay ends with a conclusion, which we presented two examples of using the waitpid function to wait, both in no particular order and in the order children were created, for all of its N children to terminate.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值