孤儿线程_C程序演示孤儿过程

孤儿线程

孤立过程 (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

孤儿线程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在中,如果一个子进程先于父进程退出,而父进程没有对子进程的退出结果进行读取,那么该子进程会成为孤儿进程。孤儿进程会被1号init进程领养,并且进入僵尸状态,由1号init进程负责处理和回收。 为什么要回收孤儿进程呢?因为对于任何一个进程来说,当它被执行起来时,系统会为其分配一块虚拟地址空间。但是当进程退出时,它只能释放自己用户区的资源。如果不及时回收孤儿进程,就会造成内存泄漏,导致系统的虚拟地址空间资源耗尽。因此,为了保证系统资源的正常使用,孤儿进程的回收是必要的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Linux操作系统孤儿进程](https://blog.csdn.net/weixin_63449996/article/details/130533945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Linux:虚拟地址空间,程序和进程,创建并回收子进程,孤儿进程和僵尸进程。为什么要回收子进程?](https://blog.csdn.net/qq_51004011/article/details/126407281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值