孤儿线程
孤立过程 (Orphan process)
The process whose parent process has finished (Completed execution) or terminated and do not exists in the process table are called orphan process. Usually, a parent process waits for its child to terminate or finish their job and report to it after execution but if he fails to do so it results in the Orphan process.
父进程已完成(完成执行)或终止并且在进程表中不存在的进程称为孤立进程 。 通常,父进程会等待其子进程终止或完成其工作,并在执行后向其报告,但如果他不这样做,则会导致孤立进程。
In most cases, the Orphan process is immediately adopted by the init process (a very first process of the system).
在大多数情况下, 孤立进程被init进程(系统的第一个进程)立即采用。
Note: fork() is a UNIX system call so following program will work only on UNIX based operating systems.
注意: fork()是UNIX系统调用,因此以下程序仅适用于基于UNIX的操作系统。
C语言中的孤儿程序 (Program for orphan process in C)
</ s> </ s> </ s>#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork() Create a child process
int pid = fork();
if (pid > 0)
{
//getpid() returns process id
// while getppid() will return parent process id
printf("Parent process\n");
printf("ID : %d\n\n",getpid());
}
else if (pid == 0)
{
printf("Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n",getpid());
// getppid() will return parent process id of child process
printf("Parent -ID: %d\n\n",getppid());
sleep(10);
// At this time parent process has finished.
// So if u will check parent process id
// it will show different process id
printf("\nChild process \n");
printf("ID: %d\n",getpid());
printf("Parent -ID: %d\n",getppid());
}
else
{
printf("Failed to create child process");
}
return 0;
}
Output
输出量
Parent process
ID : 2274
Child process
ID: 2275
Parent -ID: 2274
Child process
ID: 2275
Parent -ID: 1103
翻译自: https://www.includehelp.com/c-programs/orphan-process.aspx
孤儿线程