2.1 Linux中wait、system 分析

摘要:
父进程查询子进程的退出状态可以用wait/waitpid函数。wait获取status后检测处理:WIFEXITED如果子进程正常结束,返回一个非零值WEXITSTATUS如果WIFEXITED非零,返回子进程退出码WIFSIGNALED子进程因为捕获信号而终止,返回非零值WTERMSIG如果WIFSIGNALED非零,返回信号代码WIFSTOPPED如果子进程被暂停,返回一个非零值WSTOPSIG如果WIFSTOPPED非零,返回一个信号代码示例程序如下:1#include2#include34#include5#include6#include78#include9#include1011#include12#include13#include1415intmain16{17pid_tpid;1819pid=fork();2021if22{23perror;24exit(-1);25}2627if28{29sleep;30printf;31exit;3233}3435intret=0;36printf;37intstatus;38ret=wait;39//ret=waitpid;40printf;414243if44{45printf;46}47elseif48{49printf;50}51elseif52{53printf;54}555657return0;58}子进程退出码为100,我们在父进程中将这个退出码检测出来,执行程序,结果如下:可见exit是正常退出,而且检测出了退出码100。

wait与waitpid:

当子进程退出的时候,内核会向父进程发送SIGCHID信号,子进程的退出是一个异步事件(子进程可以在父进程运行的任何时刻终止)。

子进程退出时,内核将子进程置为僵尸状态,这个进程称为僵尸进程,它只保留最小的一些内核数据结构,以便父进程查询子进程的退出状态。

父进程查询子进程的退出状态可以用wait/waitpid函数。

当我们用fork启动一个进程时,子进程就有了自己的生命,并将独立的运行,有时候,我们需要知道某个子进程是否已经结束了,可以通过wait安排父进程等待子进程结束。

wait函数原型:

pid_t wait(&status)

status:该参数可以获得你等待子进程的信息

执行成功就会返回子进程pid。

wait系统调用会使父进程暂停执行,直到它的一个子进程结束为止。

返回的是子进程的pid,它通常是结束的子进程。

状态信息允许父进程判定子进程的退出状态,即从子进程的main函数返回的值或子进程中exit语句的退出码。

如果status不是空指针,状态信息将被写入它指向的位置。

wait获取status后检测处理:

WIFEXITED(status)如果子进程正常结束,返回一个非零值

WEXITSTATUS(status)如果WIFEXITED非零,返回子进程退出码

WIFSIGNALED(status) 子进程因为捕获信号而终止,返回非零值

WTERMSIG(status) 如果WIFSIGNALED非零,返回信号代码

WIFSTOPPED(status) 如果子进程被暂停,返回一个非零值

WSTOPSIG(status) 如果WIFSTOPPED非零,返回一个信号代码

示例程序如下:

1 #include <sys/types.h>
2 #include <unistd.h>
3 
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <signal.h>
9 #include <errno.h>
10 
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 
15 int main(int argc, char *argv[])
16 {
17 pid_t pid;
18     
19     pid =fork();
20     
21     if(pid == -1)
22 {
23         perror("exit error");
24         exit(-1);
25 }
26     
27     if(pid == 0)
28 {
29         sleep(3);
30         printf("this is child
");
31         exit(100);
32         
33 }
34     
35     int ret = 0;
36     printf("this is parent
");
37     intstatus;
38     ret = wait(&status);
39     //ret = waitpid(pid, &status, 0);
40     printf("ret = %d  pid = %d
", ret, pid);
41     
42     
43     if(WIFEXITED(status))
44 {
45         printf("child exited normal exit status = %d
", WEXITSTATUS(status));
46 }
47     else if(WIFSIGNALED(status))
48 {
49         printf("child exited abnormal signal number = %d
", WTERMSIG(status));
50 }
51     else if(WIFSTOPPED(status))
52 {
53         printf("child stopped signal number = %d
", WSTOPSIG(status));
54 }
55     
56     
57     return 0;
58 }

子进程退出码为100,我们在父进程中将这个退出码检测出来,执行程序,结果如下:

2.1 Linux中wait、system 分析第1张

可见exit(100)是正常退出,而且检测出了退出码100。

将31行的exit(100)改为abort(),再次执行程序,结果如下:

2.1 Linux中wait、system 分析第2张

可见abort()是异常退出,父进程中也是走的第二个分支,属于异常退出。

wait函数执行成功的话返回被处理的进程的pid,失败返回-1,并设置errno, wait使主进程进入睡眠,但是在子进程死亡之前可能会被信号中断,这时候就会返回-1。

如果我们有10个子进程,那么父进程怎么等待所有子进程全部结束呢?如果只用一个wait,那么只要一个子进程结束,父进程就会被唤醒,然后结束运行,这样的话其他的子进程还是会成为僵尸进程(如果在父进程结束前,子进程全死了的话,因为父进程的一个wait只会处理一个子进程,父进程结束的时候,如果还有活着的子进程,那么它们会挂到1号进程上,1号进程将来会给它们收尸)。

那么我们是否可以通过一个循环来调用wait等待所有的子进程呢?

示例程序如下:

1 #include <sys/types.h>
2 #include <unistd.h>
3 
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <signal.h>
9 #include <errno.h>
10 
11 void TestFunc(intnum)
12 {
13     printf("TestFunc : %d
", num);
14 }
15 
16 int main(void)
17 {
18     int i = 0;
19     int j = 0;
20     
21     int ret = 0;
22     
23     int ProcNum = 0;
24     int LoopNum = 0;
25     
26     printf("please enter the ProcNum : 
");
27     scanf("%d", &ProcNum);
28     
29     printf("please enter the LoopNum : 
");
30     scanf("%d", &LoopNum);
31     
32 pid_t pid;
33     
34     for(i = 0; i < ProcNum; i++)
35 {
36         pid =fork();
37         
38         if(0 ==pid)
39 {
40             for(j = 0; j < LoopNum; j++)
41 {
42 TestFunc(j);
43 }
44             sleep(10);
45             exit(0);
46 }
47 }
48 
49     while(1)
50 {
51         ret =wait(NULL);
52         
53         if(ret == -1)
54 {
55             if(errno ==EINTR)
56 {
57                 continue;
58 }
59             
60             break;
61 }
62 }    
63         
64     printf("parent process exit
");
65     return 0;
66 }

49-62行通过一个循环等待所有子进程结束,wait有可能被其他信号中断,中断时返回-1,并设置errno,所以返回-1时我们进一步判断errno,如果是被中断了,那么我们继续wait,如果返回-1,但不是被信号中断的,那就说明所有子进程都已经结束了(当没有活着的子进程时,调用wait直接返回-1)。那么我们就可以跳出循环了。

waitpid可以用来等待某个特定的进程,函数原型如下:

pid_t waitpid(pid_t pid, int *status, int options)

status如果不为空,会把状态信息写到它指向的位置

options允许改变pid的行为,最有用的一个选项是WNOHANG,它的作用是防止waitpid把调用者的执行挂起

如果执行成功,返回等待子进程的pid,失败返回-1

第一个参数pid的解释:

pid == -1,等待任一子进程,这样和wait等效

pid > 0,等待特定的子进程

pid == 0,等待组id等于调用进程组id的任一子进程,换句话说就是与调用进程同在一个组的进程

pid < -1,等待其组id等于pid的绝对值的任一子进程

wait和waitpid的区别和联系:

在一个子进程终止前,wait使其调用者阻塞,而waitpid有一个选项,可使调用者不阻塞

waitpid并不等待第一个终止的子进程,它有若干个选项,可以控制它等待特定的进程

实际上wait函数是waitpid的一个特例

示例程序如下:

1 #include <sys/types.h>
2 #include <unistd.h>
3 
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <signal.h>
9 #include <errno.h>
10 
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 
15 int main(int argc, char *argv[])
16 {
17 pid_t pid;
18     
19     pid =fork();
20     
21     if(pid == -1)
22 {
23         perror("exit error");
24         exit(-1);
25 }
26     
27     if(pid == 0)
28 {
29         sleep(3);
30         printf("this is child
");
31         //exit(100);
32 abort();
33 }
34     
35     int ret = 0;
36     printf("this is parent
");
37     intstatus;
38     //ret = wait(&status);
39     ret = waitpid(pid, &status, 0);
40     printf("ret = %d  pid = %d
", ret, pid);
41     
42     
43     if(WIFEXITED(status))
44 {
45         printf("child exited normal exit status = %d
", WEXITSTATUS(status));
46 }
47     else if(WIFSIGNALED(status))
48 {
49         printf("child exited abnormal signal number = %d
", WTERMSIG(status));
50 }
51     else if(WIFSTOPPED(status))
52 {
53         printf("child stopped signal number = %d
", WSTOPSIG(status));
54 }
55     
56     
57     return 0;
58 }

38行的wait改成39行的waitpid,执行结果如下:

2.1 Linux中wait、system 分析第3张

system C库函数:

system()函数调用“bin/sh -c command”执行特定的命令,阻塞当前进程,直到command执行完毕。原型如下:

int system(const char* command)

返回值:

如果无法启动shell运行命令,system返回127,出现不能执行system调用的其他错误时返回-1,如果system顺利执行,返回那个命令的退出码。

system函数执行时,会调用fork、execve、waitpid等函数。

我们自己编写一个my_system函数,功能和system相同,如下所示:

1 #include <sys/types.h>
2 #include <unistd.h>
3 
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <signal.h>
9 #include <errno.h>
10 
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 
15 int my_system(char *command)
16 {
17 pid_t pid;
18     intstatus;
19     
20     if(command ==NULL)
21 {
22         return 1;
23 }
24     
25     pid =fork();
26     
27     if(pid < 0)
28 {
29         status = -1;
30 }
31     else if(pid == 0)
32 {
33         execl("/bin/sh", "sh", "-c", command, NULL);
34         exit(127);
35 }
36     else
37 {
38         while(waitpid(pid, &status, 0) < 0)
39 {
40             if(errno ==EINTR)
41 {
42                 continue;
43 }
44             
45             status = -1;
46             break;
47             
48 }
49 }
50     
51     returnstatus;
52 }
53 
54 intmain()
55 {
56     my_system("ls -l");
57         
58     return 0;    
59 }

执行结果如下:

2.1 Linux中wait、system 分析第4张

第38-48的while循环表示等待子进程结束,并获取子进程结束状态。

可以看到 ls -l 命令成功执行了。 sh -c其中的-c表示执行系统命令,用sh执行一个shell脚本时不用加-c。我们也可以在命令行直接使用sh -c执行一个命令,如下所示:

2.1 Linux中wait、system 分析第5张

免责声明:文章转载自《2.1 Linux中wait、system 分析》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Clion+Cmake+Qt5+Qwt+msys2+MinGW在Windows下的安装配置使用教程BASE64编码的图片在网页中的显示问题的解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux CentOS7 VMware find命令、文件名后缀

一、find命令 Linux系统中的 find 命令在查找文件时非常有用而且方便。它可以根据不同的条件来查找文件,例如权限、拥有者、修改日期/时间、文件大小等等。在这篇文章中,我们将学习如何使用 find 命令以及它所提供的选项来查找文件。 列出当前目录和子目录下的所有文件 [root@davery ~]# find../.bash_logout./.ba...

Linux c 管道文件-进程间的通信 mkfifo、pipe

管道文件: 1. 创建管道mkfifo(命名管道) #include<sys/stat.h> intmkfifo( const char *pathname,mode_t mode); 参数:pathname:管道文件名/路径+文件名 Mode: 文件权限 返回值:0成功,-1失败 2. 体会管道文件的特点 案例: fifoA fifoB 建立...

Linux服务器---流量监控MRTG

MRTG        MRTG可以分析网络流量,但是它必须依赖SNMP协议。将收集到的数据生成HTML文件,以图片的形式展示出来 1、安装一些依赖软件 [root@localhost bandwidthd-2.0.1]# yum install -y net-snmp 2、配置snmp,编辑配置文件“/etc/snmp/snmpd.conf” [roo...

[笔记]--Linux下运行Python时报错解决办法

1、提示:bash: ./mp.py:/usr/bin/python^M:损坏的解释器: 没有该文件或目录 解决办法: $ sed -i 's/ $//' *.py 有时候在windows下编写的python脚本在linux下不能运行,就是因为^M的原因,因为windows下行结束符是/r/n,而liinux只需要/n. 我们可以使用cat –v来显示一...

kettle-3(linux环境调度kjb并配置定时读取)

1.首先要把kettle搬到Linux系统下,rar包解压需要操作下,直接提供.tar.gz : https://pan.baidu.com/s/1_tzcy8GCKJ7qHzjTSqNnxQ (7rv6) [.tar.gz] linux 2.将Windows中配置好的.ktr&.kjb搬到Linux目录下,没滑头,具体配置如下: #建立.sh的任...

Linux中查看CPU信息【转】

转自:http://blog.chinaunix.net/uid-23622436-id-3311579.html cat /proc/cpuinfo中的信息 processor       逻辑处理器的id。 physical id    物理封装的处理器的id。 core id        每个核心的id。 cpu cores      位于相同物...