#include
#include
int main(void)
{
pid_t child;
int status,retval;
if((child = fork()) < 0)
{
perror("fork");
exit(EXIT_FAILURE);
}
if(child == 0)
{
sleep(1000);
exit(EXIT_SUCCESS);
}
else
{
if((waitpid(child, &status, WNOHANG)) == 0)
{
retval = kill(child, SIGKILL);
if(retval)
{
puts("kill failed\n");
perror("kill");
waitpid(child, &status, 0);
}
else
{
printf("%d kill\n", child);
}
}
}
exit(EXIT_SUCCESS);
}
在fork执行成功后,子进程睡眠 1000 秒钟,然后退出。同时父进程调用waitpid,使用WNOHANG选项,调用会立即返回。父进程接着杀死子进程。
暂无评论,赶紧抢沙发吧