一、vfork函数与fork函数的区别
区别1:
vfork直接使用父进程存储空间,不拷贝
区别2:
vfork保证子进程先运行,当子进程调用exit退出后,父进程才运行
代码验证
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int cnt=0; //计数
pid=vfork();
if(pid>0){
while(1){
printf("cnt is %d\n",cnt); //这里验证使用父进程存储空间
printf("this is father print,pid=%d\n",getpid());
sleep(1);
}
}
else if(pid==0){
while(1){
printf("this is child print,pid=%d\n",getpid());
sleep(1);
cnt++;
if(cnt==3){//子进程重复三次以后退出
exit(0);
}
}
}
return 0;
}
二、进程退出
1.正常退出
1.Main函数调用return
2.进程调用exit(),标准C库
3.进程调用_exit()或者 _Exit(),属于系统调用
4.进程最后一个线程返回
5.最后一个线程调用pthread_exit
2.异常退出
1.调用abort
2.当进程收到某些信号时,如ctrl+c
3.最后一个线程对取消(cancellation)请求做出响应
等待子进程退出
为什么要等待子进程退出?
创建子进程的目的是为了干活
等待子进程的退出 收集退出状态
子进程状态不被收集,会变成僵尸进程
wait
pid_t wait(int *status);
//这里的status是个整型指针 存放地址
status参数:
是一个整型数指针
非空:
子进程退出状态放在它所指向的地址中
空:
不关心退出状态
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int cnt=0;
int status=10;
pid=fork();
if(pid>0){
wait(&status); //使用wait函数 父进程会被阻塞 直到子进程结束
//被收集的子进程不会变成僵尸进程
printf("child quit,child status is:%d\n",WEXITSTATUS(status));
//调用WEXITSTATUS收集exit退出的状态描述符 并将其打印
while(1){
printf("cnt is %d\n",cnt);
printf("this is father print,pid=%d\n",getpid());
sleep(1);
}
}
else if(pid==0){
while(1){
printf("this is child print,pid=%d\n",getpid());
sleep(1);
cnt++;
if(cnt==3){
exit(3);
}
}
}
return 0;
}
waitpid
pid_t waitpid(pid_t pid, int *status, int options);
pid参数:
pid == -1 等待任一子进程。就这一方面而言,waitpid与wait等效。
pid>0 等待其进程ID与pid返回值相等的子进程(这个用的较多)
pid == 0 等待其组ID等于调用进程组ID的任一子进程
pid<0 等待其组ID等于pid绝对值的任一子进程
option参数:
这里列举一个用的较多的
WNOHANG 若由pid指定的子进程并不是立即可用的,则waitpid不阻塞,此时其返回值为0
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int cnt=0;
int status=10;
pid=fork();
if(pid>0){
//wait(&status);
waitpid(pid,&status,WNOHANG);
printf("child quit,child status is:%d\n",WEXITSTATUS(status));
while(1){
printf("cnt is %d\n",cnt);
printf("this is father print,pid=%d\n",getpid());
sleep(1);
}
}
else if(pid==0){
while(1){
printf("this is child print,pid=%d\n",getpid());
sleep(1);
cnt++;
if(cnt==3){
exit(3);
}
}
}
return 0;
}
孤儿进程
父进程如果不等待子进程退出,在子进程之前就结束了自己的“生命”,此时子进程叫做孤儿进程
Linux避免系统存在过多孤儿进程,init进程收留孤儿进程,变成孤儿进程的父进程
代码演示
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int cnt=0;
int status=10;
pid=fork();
if(pid>0){
printf("this is father print,pid=%d\n",getpid());
//这里父进程只执行1次就结束了 子进程仍在继续 但被pid为1的init进程收留
}
else if(pid==0){
while(1){
printf("this is child print,pid=%d,my father pid=%d\n",getpid(),getppid());
sleep(1);
cnt++;
if(cnt==5){
exit(3);
}
}
}
return 0;
}