Day 27 exit , atexit, wait, waitpid, getpid, getppid, 僵尸进程, 孤儿进程,

1.当一个在硬盘里的程序被加载到内存中运行后就会成为进程。在Linux下可执行文件名为ELF。
一个进程运行起来后系统大概会给其分配大概 3GB(2.9GB)左右的内存
Linux下命令格式一般有三种:
(1)外部命令;
(2)别名;比如: ll 命令
(3)内置。 bash中大概有40 个内置命令。

2.进程和程序的区别?
程序:静态
存储在硬盘中代码,数据的集合
进程:动态
程序执行的过程,包括进程的创建、调度、消亡
.c ----> a.out-----> process(pid)
1)程序是永存,进程是暂时的
2)进程有程序状态的变化,程序没有
3)进程可以并发,程序无并发
4)进程与进程会存在竞争计算机的资源
5)一个程序可以运行多次,变成多个进程
一个进程可以运行一个或多个程序
内存的分布
0-3G,是进程的空间,3G-4G是内核的空间,虚拟地址
虚拟地址 * 物理内存和虚拟内存的地址 映射表 1page=4k

3.父子进程的关系:
子进程是父进程的副本。子进程获得父进程数据段,堆,栈,正文段共享,数据段,堆,栈不共享。

在fork之后,一般情况那个会先运行,是不确定的。如果非要确定那个要先运行,需要IPC机制。

区别:
1)fork的返回值
2)pid不同

4.僵进程与孤儿进程,
(1)僵尸进程是子进程生命结束后,pcb块(process control block)里面包含的子进程信息还没有被回收,
它等待父进程进行回收,因为父进程还没有结束,所以还不会回收它,等父进程结束后,如果父进程回收它则子现销毁,然后父再销毁,
如果父没有回收它,则两个进程再程序结束时被系统统一回收。
(2)孤儿进程是子进程还没有结束,但父进程已经结束了。因为父进程已经结束,所以子进程会挂载到PID为1 的进程(init)。

5.exit 与 _exit的区别
exit 为c 库里面的函数,会进行相关的清理退出。
_exit 只会退出关闭文件,但不会清理什么的。

  1. atexit 注册函数, int atexit(void(*function)(void)); //函数指针,回调函数。

7.回调函数
3.atexit
int atexit(void (*function)(void));
功能:
注册进程退出前执行的函数
参数:
function:函数指针
指向void返回值void参数的函数指针
返回值:
成功返回0
失败返回非0

当程序调用exit或者由main函数执行return时,所有用atexit
注册的退出函数,将会由注册时顺序倒序被调用

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int *p;
void cleaup()
{
printf(“这是一个清理函数\n”);
free§;
}

int main(int argc, const char *argv[])
{
atexit(cleaup);
p = malloc(50);
int i = 5;

while(i--)
{

	printf("++++++ %p\n", p);
	sleep(1);
}
exit(1);   //退出时注册函数将会被执行。
return 0;

}



8.进程空间的回收

exit(20);

wait/waitpid

pid_t wait(int *status);
功能:该函数可以阻塞等待任意子进程退出
并回收该进程的状态。
一般用于父进程回收子进程状态。

参数:status 进程退出时候的状态
如果不关心其退出状态一般用NULL表示
如果要回收进程退出状态,则用WEXITSTATUS回收。

返回值:成功 回收的子进程pid
失败 -1;

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

int main(int argc, const char *argv[])
{
	pid_t id = fork();
	if(id > 1)
	{
	   pid_t child_pid = wait(NULL);  //再此会等待子进程结束后,父进程才会执行。
	   printf("这是父进程 %d\n", getpid());
	   printf("the child_pid is %d\n", child_pid);
	}
	if(0 == id)
	{
		int i = 5;
		while(i--)
		{
		  sleep(1);
		  printf("这是子进程pid %d\n", getpid());
		}
		exit(1);

	}
	if(id < 0)
	{
		exit(0);
	}
	return 0;
}

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

int main(int argc, const char *argv[])
{
	pid_t id = fork();
	if(id > 1)
	{ 
		printf("这是父进程 %d\n", getpid());
		int status = 0;
		pid_t child_pid = wait(&status);  //status 为状态
		if( WIFEXITED(status))
		{
			printf("child terminated normally exit val %d\n", WEXITSTATUS(status));
		}
		if( WIFSIGNALED(status))
		{
			printf(" terminated by a signal  val\n",  WTERMSIG(status));
		}
		printf("the child_pid is %d\n", child_pid);
	}
	if(0 == id)
	{
		int i = 5;
		while(i--)
		{
			sleep(1);
			printf("这是子进程pid %d\n", getpid());
		}
	   exit(60);

	}
	if(id < 0)
	{
		exit(0);
	}
	return 0;
}

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

int main(int argc, const char *argv[])
{
	pid_t id = fork();

if(id > 1)
{

	printf("这是父进程 %d\n", getpid());
	while(1)
	{
		int status = 0;
		pid_t child_pid = waitpid(id, &status, WNOHANG);
		
		if(id == child_pid)
		{
			if( WIFEXITED(status))
			{
				printf("child terminated normally exit val %d\n", WEXITSTATUS(status));
			}
			if( WIFSIGNALED(status))
			{
				printf(" terminated by a signal  val\n",  WTERMSIG(status));
			}
			printf("the child_pid is %d\n", child_pid);

			break;
		}
		else
		{
			printf("子进程未结束\n");
		}
 	}
}

if(0 == id)
{
	int i = 5;
	while(i--)
	{
		sleep(1);
		printf("这是子进程pid %d\n", getpid());
	}
   exit(60);

}
if(id < 0)
{
	exit(0);
}
return 0;

}



父进程回收指定子进程的僵尸状态。
此处用waitpid(); 可以指定子进程的PID
分析;
创建子进程时,将所有子进程的PID装到一个数组中,到时候取数组值就是指定的子进程。
此处写的为不阻塞的等待回收。

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



int main(int argc, const char *argv[])
{
	int i;
	int pid[10] = {0};
	for(i = 0; i < 10; ++i)
	{
		 pid[i] = fork();
		if(pid[i] > 1)
		{
			printf("父进程 %d %d\n", getpid(),pid[i]);

	}
	if(0 == pid[i])
	{
		printf("子进程 %d\n",getpid());
		exit(1);

	}
	if(pid < 0)
	{
       exit(0);
	}

}
printf("这是父进程 %d\n", getpid());
while(1)
{
	int status = 0;
	pid_t child_pid = waitpid(pid[4], &status, WNOHANG);  //如果要变为等待回收,将此处WNOHANG变为0, 去掉while循环即可。

	if(pid[4] == child_pid)
	{
		if( WIFEXITED(status))  //是否正常退出
		{
			printf("child terminated normally exit val %d\n", WEXITSTATUS(status));  //拿到退出条件
		}
		if( WIFSIGNALED(status))  //是否信号退出
		{
			printf(" terminated by a signal  val\n",  WTERMSIG(status)); //拿到信号条件。
		}
		printf("the child_pid is %d\n", child_pid);

		break;
	}
	else
	{
		printf("子进程未结束\n");
	}
}


 return 0;
}


getpid 可以获得自己的PID, getppid 可以获得父线程的PID。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int a = 20;
int main(int argc, const char *argv[])
{

pid_t id = fork();
if(id >0)
{
//	system("firefox www.baidu.com"); //可以调用system()函数直接启动火狐浏览器,打开百度。
	sleep(4);
	printf("这个是老爸进程a%d pid:%d ppid:%d\n",a,getpid(),getppid());
}

if(0 == id)
{
//	system("nautilus");
	a+=20;
	printf("这个是儿子进程a %d  pid:%d ppid:%d\n",a,getpid(),getppid());
}
if(id <0)
{
	perror("fork");
	exit(1);
}
printf("hello ok a %d pid:%d, ppid:%d\n",a,getpid(),getppid());
return 0;

}



僵尸进程与孤儿进程。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	
	pid_t pid = fork();
	if(pid > 1)
	{
		printf("这是父进程 pid:%d ppid:%d \n", getpid(), getppid());
		sleep(10);  //此出这样操作为僵尸进程。

	}
	if(0 == pid)
	{

		printf("这是子进程 pid:%d ppid:%d \n", getpid(), getppid());
		exit(1);
		//slee(10); 如果打开此句屏蔽此句的exit(1),关闭父进程里面的sleep(10), 则会变为孤儿进程。
	}
	if(pid < 0)
	{
		exit(1);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值