Day5(Io进程)

在子进程终止子进程

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
	pid_t id1 = fork(); 	//创建子1进程
	if(id1 < 0)
	{
		printf("错误在:%d", __LINE__);
		perror("fork");
	}

	else if(id1 > 0) 	//在父进程中为真
	{
		printf("This is father process\n");

		pid_t id2 = fork(); 	//创建子2进程
		if(id2 < 0)
		{
			printf("错误在:%d", __LINE__);
			perror("fork");
		}

		pid_t rec = waitpid(id1, NULL, 0); 	//在子2进程中
		//尝试关闭子1进程,接受返回值。
		printf("%d\n", rec);
		printf("This is id2 process\n");
		if(rec != -1)
			printf("能回收兄弟进程\n");
		else
			printf("无法回收兄弟进程\n");

		while(1)
		{
			sleep(1);
			printf("parent = %d, id1 = %d, id2 = %d,\
					line = %d\n", getpid(), id1, id2,  __LINE__);
		}

	}
	else if(0 == id1)
	{
		printf("This is id1 processi\n");
		
		int i = 0;
		while(i < 3)
		{
			printf("--------------分割线---------------\n");
			i++;
		}

		printf("This is ready exit\n");

		exit(3);
	}


	while(1)
	{
		sleep(1);
	}

	return 0;
}

输出结果:

This is father process
-1
This is id2 process
无法回收兄弟进程
This is id1 processi
--------------分割线---------------
--------------分割线---------------
--------------分割线---------------
This is ready exit
8124
This is id2 process
能回收兄弟进程
parent = 8123, id1 = 8124, id2 = 8125,					line = 42
parent = 8125, id1 = 8124, id2 = 0,					line = 42
parent = 8123, id1 = 8124, id2 = 8125,					line = 42
parent = 8125, id1 = 8124, id2 = 0,					line = 42
parent = 8123, id1 = 8124, id2 = 8125,					line = 42
parent = 8125, id1 = 8124, id2 = 0,					line = 42
parent = 8123, id1 = 8124, id2 = 8125,					line = 42
parent = 8125, id1 = 8124, id2 = 0,					line = 42
^C
ubuntu@ubuntu:Day5$ 


在子进程终止父进程

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
	pid_t id1 = fork(); 	//创建子1进程
	if(id1 < 0)
	{
		printf("错误在:%d", __LINE__);
		perror("fork");
	}

	else if(id1 > 0) 	//在父进程中为真
	{
		printf("This is father process\n");

		sleep(1);

		while(1)
		{
			sleep(1);
			printf("parent = %d, id1 = %d, \
					line = %d\n", getpid(), id1,   __LINE__);
		}

	}
	else if(0 == id1) 	//在子进程中为真
	{
		printf("This is id1 processi\n");

		int i = 0;
		while(i < 3)
		{
			printf("--------------分割线---------------\n");
			i++;
		}
		pid_t rec = waitpid(getppid(), NULL, 0);
		printf("%d\n", rec);
		printf("This is id1 process\n");
		if(rec != -1)
			printf("能回收父进程\n");
		else
			printf("无法回收父进程\n");
	}


	while(1)
	{
		sleep(1);
	}

	return 0;
}

输出结果:

This is father process
This is id1 processi
--------------分割线---------------
--------------分割线---------------
--------------分割线---------------
-1
This is id1 process
无法回收父进程
parent = 8103, id1 = 8104, 					line = 28
parent = 8103, id1 = 8104, 					line = 28
parent = 8103, id1 = 8104, 					line = 28
^C

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值