• Linux中的进程等待(超详细)


    1. 进程等待必要性

    • 我们知道,子进程退出,父进程如果不管不顾,就可能造成‘僵尸进程’的问题,进而造成内存泄漏
    • 另外,进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法杀死一个已经死去的进程。
    • 最后,父进程派给子进程的任务完成的如何,我们需要知道。如,子进程运行完成,结果对还是不对,或者是否正常退出。
    • 父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息

    2. 进程等待的方法

    2.1 wait方法

    在Linux中,wait函数是一个系统调用用于等待子进程的终止并获取其终止状态。该函数的原型如下所示:

    #include
    #include
    pid_t wait(int*status);
    返回值:
     成功返回被等待进程pid,失败返回-1。
    参数:
     输出型参数,获取子进程退出状态,不关心则可以设置成为NULL
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    wait函数的作用是暂停当前进程的执行,直到一个子进程终止。当子进程终止后,wait函数会返回子进程的进程ID(PID),并将子进程的终止状态存储在指针status指向的变量中。

    status参数是一个指向整型变量的指针,用于存储子进程的终止状态。通过status可以获取子进程的退出状态、终止信号等信息如果不关心终止状态,可以将status设置为NULL。

    wait函数返回的PID有以下几种可能的取值:

    • 如果成功等待到一个子进程的终止,返回子进程的PID。
    • 如果调用进程没有子进程,wait函数会返回-1
    • 如果调用进程被一个信号中断,wait函数会返回-1

    1.WIFEXITED(status) 是一个宏,用于判断子进程是否正常退出。当子进程正常退出时,它返回一个非零值。
    注意,这里的 status 参数不同于 wait 函数的参数(指向整数的指针),而是指向该指针所指向的整数值。请确保不要混淆它们。

    2.WEXITSTATUS(status) 是一个宏,在 WIFEXITED 返回非零值时,用于提取子进程的返回值。例如,如果子进程调用 exit(5) 退出,WEXITSTATUS(status) 将返回 5;如果子进程调用 exit(7),WEXITSTATUS(status) 将返回 7。
    请注意,如果进程不是正常退出的,即 WIFEXITED 返回 0,那么 WEXITSTATUS(status) 的值就没有意义。

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
        pid_t id = fork();
    
        if (id == 0) {
            // 子进程
            printf("子进程开始执行\n");
            sleep(3);
            printf("子进程执行完毕\n");
            exit(0);
        } else if (id > 0) {
            // 父进程
            printf("父进程等待子进程终止\n");
            int status;
            pid_t child_pid = wait(&status);
            
            if (child_pid == -1)
             {
                perror("wait");
                exit(1);
            }
            if (WIFEXITED(status)) 
            {
                printf("子进程正常终止,退出状态:%d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) 
            {
                printf("子进程被信号终止,信号编号:%d\n", WTERMSIG(status));
            }
            	printf("父进程继续执行\n");
    	    } 
    	    else 
    	    {
    	        perror("fork");
    	        exit(1);
    	    }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    运行结果:
    在这里插入图片描述

    在上面的示例中,父进程通过fork创建了一个子进程。子进程会执行一段耗时的操作(这里使用sleep模拟),然后退出。父进程调用wait函数等待子进程的终止,并获取子进程的终止状态。最后,父进程继续执行。

    2.2 waitpid方法

    waitpid函数是Linux中用于等待指定子进程终止的系统调用。与wait函数类似,waitpid函数也可以用于获取子进程的终止状态。

    pid_ t waitpid(pid_t pid, int *status, int options);
    返回值:
     当正常返回的时候waitpid返回收集到的子进程的进程ID;
     如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
     如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
    参数:
     pid:
     pid=-1,等待任一个子进程。与wait等效。
     pid>0.等待其进程ID与pid相等的子进程。
     status:
     WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)
     WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)
     options:
     WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进
    程的ID,若想使用默认状态,可以将options设成0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 如果子进程已经退出,调用wait/waitpid时,wait/waitpid会立即返回,并且释放资源,获得子进程退出信息。
    • 如果在任意时刻调用wait/waitpid,子进程存在且正常运行,则进程可能阻塞。
    • 如果不存在该子进程,则立即出错返回。

    3. 获取子进程status

    • wait和waitpid,都有一个status参数,该参数是一个输出型参数,由操作系统填充。
    • 如果传递NULL,表示不关心子进程的退出状态信息。
    • 否则,操作系统会根据该参数,将子进程的退出信息反馈给父进程。
    • status不能简单的当作整形来看待,可以当作位图来看待,具体细节如下图(只研究status低16比特位)
      在这里插入图片描述
      测试代码:
      1 #include <sys/wait.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <errno.h>
      6 #include <unistd.h>
      7 int main(void)
      8 {
      9     pid_t pid;
     10     if ((pid = fork()) == -1)
     11         perror("fork"), exit(1);
     12     if (pid == 0) {
     13         printf("pid:%d ppid: %d\n",getpid(),getppid());
     14         sleep(20);                                              
     15         exit(10);
     16     }
     17     else {
     18         int st;
     19         int ret = wait(&st);
     20 
     21         if (ret > 0 && (st & 0X7F) == 0) { // 正常退出
     22             printf("child exit code:%d\n", (st >> 8) & 0XFF);
     23         }
     24         else if (ret > 0) { // 异常退出
     25             printf("sig code : %d\n", st & 0X7F);
     26         }
     27     }
     28 }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    运行结果:
    在这里插入图片描述

    4. 具体代码实现

    • 进程的阻塞等待方式:
    #include                                                                                                                   
    #include 
    #include 
    #include 
    #include 
    #include 
    int main()
    {
    	pid_t pid;
    	pid = fork();
    	if (pid < 0) {
    		printf("%s fork error\n", __FUNCTION__);
    		return 1;
    	}
    	else if (pid == 0) { //child
    		printf("子进程已运行, pid is : %d\n", getpid());
    		sleep(5);
    		exit(257);
    	}
    	else {
    		int status = 0;
    		pid_t ret = waitpid(-1, &status, 0);//阻塞式等待,等待5S
    		printf("这是等待的测试\n");
    		if (WIFEXITED(status) && ret == pid)
    		{
    			printf("等待子进程5秒成功,子进程返回代码为:%d.\n", WEXITSTATUS(status));
    		}
    		else {
    			printf("等待失败, return.\n");
    			return 1;
    		}
    	}
    	return 0;
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    运行结果:
    在这里插入图片描述

    • 进程的非阻塞等待方式:
    #include  
    #include 
    #include 
    #include 
    int main()
    {
    	pid_t pid;
    
    	pid = fork();
    	if (pid < 0) {
    		printf("%s fork error\n", __FUNCTION__);
    		return 1;
    	}
    	else if (pid == 0) { //child
    		printf("子进程已运行: %d\n", getpid());
    		sleep(5);
    		exit(1);
    	}
    	else {
    		int status = 0;
    		pid_t ret = 0;
    		do
    		{
    			ret = waitpid(-1, &status, WNOHANG);//非阻塞式等待
    			if (ret == 0) {
    				printf("子进程正在运行\n");
    			}
    			sleep(1);
    		} while (ret == 0);
    
    		if (WIFEXITED(status) && ret == pid) {
    			printf("等待子进程5秒成功,子进程返回代码为:%d.\n", WEXITSTATUS(status));
    		}
    		else {
    			printf("等待失败, return.\n");
    			return 1;
    		}
    	}
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    运行结果:
    在这里插入图片描述
    (本章完)

  • 相关阅读:
    时序预测 | MATLAB实现XGBoost极限梯度提升树时间序列预测
    Linux
    【JSON】
    【如何使用vscode用户代码】
    PyTorch入门教学——Transforms使用
    使用JavaMailSender进行邮件发送
    点云从入门到精通技术详解100篇-基于点云数据的机器人动态分拣
    Spring系列八:Spring AOP 和 AspectJ AOP 区别
    零基础入门网络渗透到底要怎么学?_网络渗透技术自学
    LVS+DR+apache+keepalived负载均衡
  • 原文地址:https://blog.csdn.net/originalHSL/article/details/134432583