900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Linux系统编程--3(exec 函数族 僵尸进程和孤儿进程 wait和wait_pid回收子进程)

Linux系统编程--3(exec 函数族 僵尸进程和孤儿进程 wait和wait_pid回收子进程)

时间:2020-04-30 03:08:38

相关推荐

Linux系统编程--3(exec 函数族 僵尸进程和孤儿进程 wait和wait_pid回收子进程)

exec 函数族

fork 创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支) ,子进程往往要调用一种 exec 函数以执行另一个程序。当进程调用一种 exec 函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序 的启动例程开始执行。调用 exec 并不创建新进程,所以调用 exec 前后该进程的 id 并未改变。

将当前进程的.text、.data 替换为所要加载的程序的.text、.data,然后让进程从新的.text 第一条指令开始执行, 但进程 ID 不变,换核不换壳。

其实有六种以 exec 开头的函数,统称 exec 函数:

1. int execl(constchar*path,const char*arg,...);2. int execlp(constchar*file,const char*arg,...);3. int execle(constchar*path,const char*arg,...,char*constenvp[]);4. int execv(constchar*path,char*constargv[]);5. int execvp(constchar*file,char*constargv[]);6. int execve(constchar*path,char*constargv[],char*constenvp[]);

execlp 函数

加载一个进程,借助 PATH 环境变量

intexeclp(constchar*file,constchar*arg,...);

成功:无返回;失败:-1

参数 1:要加载的程序的名字。该函数需要配合 PATH 环境变量来使用,当 PATH 中所有目录搜索后没有参 数 1 则出错返回。

该函数通常用来调用系统程序。如:ls、date、cp、cat 等命令。

编写一个程序,实现Linux下shell命令ls -l -a

#include<stdio.h>#include<unistd.h>#include<stdlib.h>int main(void){pid_t pid;pid=fork();if(pid==-1){ perror("fork error:");exit(1);}else if(pid>0){ sleep(1);printf("parent\n");}else{execlp("ls","ls","-l","-a",NULL);} return 0;}

execl 函数

这个程序可以加载自定义的函数加载一个进程, 通过 路径+程序名 来加载。

intexecl(constchar*path,constchar*arg,...);

成功:无返回;失败:-1

对比 execlp,如加载"ls"命令带有-l,-F 参数

execlp("ls","ls","-l","-F",NULL);使用程序名在 PATH 中搜索。

execl("/bin/ls","ls","-l","-F",NULL);使用参数 1 给出的绝对路径搜索。

这个函数可以加载自定义的程序

被加载的程序:

#include<stdlib.h>#include<unistd.h>#include<stdio.h>int main(){while(1){sleep(1);printf("-------\n");} return 0;}

加载程序:

#include<stdio.h>#include<unistd.h>#include<stdlib.h>int main(void){pid_t pid;pid=fork();if(pid==-1){ perror("fork error:");exit(1);}else if(pid>0){ sleep(1);printf("parent\n");}else{// execlp("ls","ls","-l","-h",NULL);// execl("/bin/ls","ls","-l","-h",NULL);execl("./execl_test","execl_test","NULL"); } return 0;}

execvp 函数

加载一个进程,使用自定义环境变量 env intexecvp(constcharfile,constcharargv[]);

变参形式:

…argv[] (main 函数也是变参函数,形式上等同于 intmain(intargc,char*argv0,…))

变参终止条件

NULL 结尾固参指定 execvp 与 execlp 参数形式不同,原理一致。

将当前进程信息打印到文件中

int dup2(int oldfd, int newfd);完成文件描述符拷贝#include<unistd.h>#include<fcntl.h> #include<stdio.h>#include<stdlib.h>int main(){int fd; //打开文件,文件名为ps.out,只写方式打开,文件不存在创建,存在截断>为0.指定打开文件权限为644fd=open("ps.out",O_WRONLY|O_CREAT|O_TRUNC,0644);//返回为0为打开失败if(fd<0){perror("open ps.out error");exit(1);} dup2(fd,STDOUT_FILENO);//dup2(3,1);fd,stdoutexeclp("ps","ps","ax",NULL);//隐式回收,进程结束时,会把所有打开的文件都关闭掉return 0;}

exec 函数族一般规律

exec 函数一旦调用成功即执行新的程序,不返回。只有失败才返回,错误值-1。所以通常我们直接在 exec 函数 调用后直接调用 perror()和 exit(),无需 if 判断。

l (list) 命令行参数列表p (path) 搜素 file 时使用 path 变量v(vector)使用命令行参数数组e(environment) 使用环境变量数组,不使用进程原有的环境变量,设置新加载程序运行的环境变量

事实上,只有 execve 是真正的系统调用,其它五个函数最终都调用 execve,所以 execve 在 man 手册第 2 节, 其它函数在 man 手册第 3 节。这些函数之间的关系如下图所示。

僵尸进程和孤儿进程

孤儿进程

孤儿进程: 父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为 init 进程,称为 init 进程领 养孤儿进程。

创建孤儿进程

#include<stdio.h>#include<unistd.h>#include<stdlib.h>int main(){pid_t pid;pid=fork();if(pid==-1){perror("fork");exit(1);}else if(pid>0){sleep(1);printf("I am parent pid= %d,parentID= %d\n",getpid(),getppid());}else if(pid==0){printf("child pid = %d,parentID=%d \n",getpid(),getppid());sleep(3);printf("child pid = %d,parentID=%d \n",getpid(),getppid());} return 0;}

僵尸进程

僵尸进程: 进程终止,父进程尚未回收,子进程残留资源(PCB)存放于内核中,变成僵尸(Zombie)进程。

特别注意,僵尸进程是不能使用 kill 命令清除掉的。因为 kill 命令只是用来终止进程的,而僵尸进程已经终止

用wait或者waitpid回收或者杀死他爸

回收子进程

wait 函数

一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的 PCB 还保留着,内核在其中保 存了一些信息:

如果是正常终止则保存着退出状态,

如果是异常终止则保存着导致该进程终止的信号是哪个。

这个 进程的父进程可以调用 wait 或 waitpid 获取这些信息,然后彻底清除掉这个进程。

我们知道一个进程的退出状态可 以在 Shell 中用特殊变量$?查看,因为 Shell 是它的父进程,当它终止时 Shell 调用 wait 或 waitpid 得到它的退出状态 同时彻底清除掉这个进程。

父进程调用 wait 函数可以回收子进程终止信息。该函数有三个功能:

阻塞等待子进程退出

回收子进程残留资源

获取子进程结束状态(退出原因)。

pid_t wait(int*status); 成功:清理掉的子进程 ID;失败:-1(没有子进程)

当进程终止时,操作系统的隐式回收机制会:1.关闭所有文件描述符 2. 释放用户空间分配的内存。内核的 PCB 仍存在。其中保存该进程的退出状态。(正常终止→退出值;异常终止→终止信号)

可使用 wait 函数传出参数 status 来保存进程的退出状态。借助宏函数来进一步判断进程终止的具体原因

宏函 数可分为如下三组

1

WIFEXITED(status) 为非 0 → 进程正常结束 WEXITSTATUS(status) 如上宏为真,使用此宏 → 获取进程退出状态 (exit 的参数)`

示例代码:

#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/wait.h>int main(void){pid_t pid,wpid;pid=fork();int status;if(pid==0){// pritnf("---child,my parent= %d,going to sleep 10s\n",getppid());sleep(3);printf("----------------child die--------------\n");return 100 ; }else if(pid>0){wpid=wait(&status);if(wpid==-1){perror("wait error:");exit(1);} if(WIFEXITED(status)){printf("child exit with %d\n",WEXITSTATUS(status));} while(1){// printf("I am parent,pid= %d,myson = %d\n",getpid(),pid);sleep(1);} }else{perror("fork");return 1;} return 0;}

2

WIFSIGNALED(status) 为非 0 → 进程异常终止 WTERMSIG(status) 如上宏为真,使用此宏 → 取得使进程终止的那个信号的编号。

示例代码

int main(void){pid_t pid,wpid;pid=fork();int status;if(pid==0){// pritnf("---child,my parent= %d,going to sleep 10s\n",getppid());sleep(20);printf("----------------child die--------------\n");return 100 ;}else if(pid>0){wpid=wait(&status);if(wpid==-1){perror("wait error:");exit(1);} if(WIFEXITED(status)){printf("child exit with %d\n",WEXITSTATUS(status));} if(WIFSIGNALED(status)){printf("child killed by %d\n",WTERMSIG(status));} while(1){// printf("I am parent,pid= %d,myson = %d\n",getpid(),pid);sleep(1);} }else{perror("fork");return 1;} return 0;}

3. 3

WIFSTOPPED(status) 为非 0 → 进程处于暂停状态 WSTOPSIG(status) 如上宏为真,使用此宏 → 取得使进程暂停的那个信号的编号。 WIFCONTINUED(status) 为真 → 进程暂停后已经继续运行

示例代码

#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/wait.h>int main(void){pid_t pid,wpid;pid=fork();if(pid==0){// pritnf("---child,my parent= %d,going to sleep 10s\n",getppid()); sleep(10);printf("----------------child die--------------\n");}else if(pid>0){wpid=wait(NULL);if(wpid==-1){perror("wait error:");exit(1);} while(1){// printf("I am parent,pid= %d,myson = %d\n",getpid(),pid);sleep(1);} }else{perror("fork");return 1;} return 0;}

waitpid 函数

作用同 wait,但可指定 pid 进程清理,可以不阻塞。

pid_t waitpid(pid_t pid,int* status,in options); 成功:返回清理掉的子进程 ID;失败:-1(无子进程)

特殊参数和返回情况:

参数 pid:

>0 回收指定 ID 的子进程-1 回收任意子进程(相当于 wait)0 回收和当前调用 waitpid 一个组的所有子进程<-1 回收指定进程组内的任意子进程如果传的是进程组ID,则回收全部该组内全部进程,kill也可以用

返回 0:参 3 为 WNOHANG(指定为非阻塞),且子进程正在运行。

注意:一次 wait 或 waitpid 调用只能清理一个子进程,清理多个子进程应使用循环。

#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/wait.h>int main(int argc,char *argv[]){int n=5,i; //默认创建5个进程pid_t p,q;pid_t wpid;if(argc==2){n=atoi(argv[1]);} for(i=0;i<n;i++){ //出口1,父进程专用出口p=fork();if(p==0){break; //出口2,子进程出口,i不自增}else if(i==3){q=p;} } //父进程if(n==i){sleep(n);printf("I am parent,pid= %d\n",getpid(),getgid());do{ wpid=waitpid(-1,NULL,WNOHANG);//====wait(NULL);if(wpid>0){n--;} //if wpid==0 说明子进程正在运行,sleep(1);}while(n>0); //循环回收多个子进程//while(wait(NULL));//while(1);printf("wait finish\n");//打印子进程}else{sleep(i);printf("I am %dth child,pid= %d,gpid=%d\n",i+1,getpid(),getgid());}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。