先决条件: C中的fork()
null
在下面的代码中,当父对象睡眠50秒时,子对象使用exit()系统调用完成其执行,因此不调用 等等 子进程的条目仍然存在于进程表中。
// A C program to demonstrate Zombie Process. // Child becomes Zombie as parent is sleeping // when child process exits. #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main() { // Fork returns process id // in parent process pid_t child_pid = fork(); // Parent process if (child_pid > 0) sleep(50); // Child process else exit (0); return 0; } |
请注意,由于禁用了fork(),上述代码可能无法与联机编译器一起使用。
在下面的代码中,父进程完成执行并退出,而子进程仍在执行,现在称为孤立进程。
然而,一旦父进程死亡,孤儿进程很快就会被init进程采用。
// A C program to demonstrate Orphan Process. // Parent process finishes execution while the // child process is running. The child process // becomes orphan. #include<stdio.h> #include <sys/types.h> #include <unistd.h> int main() { // Create a child process int pid = fork(); if (pid > 0) printf ( "in parent process" ); // Note that pid is 0 in child process // and negative if fork() fails else if (pid == 0) { sleep(30); printf ( "in child process" ); } return 0; } |
请注意,由于禁用了fork(),上述代码可能无法与联机编译器一起使用。
相关的: 知道操作系统中的僵尸是什么吗? 僵尸过程及其预防
本文由 马图尔夫人 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END