linux C++ 多进程初步02

ps:疑惑的地方,1 进程pcb的概念, 还有 ulimit -a 显示的信息 是一个进程可以最大占用资源的上限吗? 还有 文件描述符的概念?? 这里不是很明白!记录一下2还有WIFEXITED

  1. 孤儿进程 与僵尸进程
    孤儿进程: 子进程运行,父进程终止, 子进程就是孤儿进程

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

  1. wait函数 回收子进程
man wait 看到的是命令提示
man 2 wait 看到的是函数原型
pid_t wait(int *status);
作用:
	a. 阻塞等待
	b. 回收子进程资源
	c. 查看死亡原因  
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
	status 传出参数
	返回值 
		调用成功:返回终止子进程的pid,
		调用失败:返回 -1
子进程死亡原因:
	a. 正常死亡 WIFEXITED
		如果WIFEXITED为真,使用WEXITSTATUS ,得到退出状态
    b. 非正常死亡
    	如果WIFSIGNALED为真,使用WTERMSJG, 得到信号
	

以下是代码示例
在这里插入图片描述
以下是 wait 于 waitpid函数 相关文档介绍

If status is not NULL, wait() and waitpid() store status information in the int to  which  it  points.
This integer can be inspected with the following macros (which take the integer itself as an argument,
not a pointer to it, as is done in wait() and waitpid()!):

       WIFEXITED(status)
              returns true if the child terminated normally, that is, by calling exit(3) or _exit(2),  or  by
              returning from main().

       WEXITSTATUS(status)
              returns  the  exit  status  of the child.  This consists of the least significant 8 bits of the
              status argument that the child specified in a call to exit(3) or _exit(2) or  as  the  argument
              for  a  return  statement  in main().  This macro should be employed only if WIFEXITED returned
              true.

       WIFSIGNALED(status)
              returns true if the child process was terminated by a signal.

       WTERMSIG(status)
              returns the number of the signal that caused the child process to terminate.  This macro should
              be employed only if WIFSIGNALED returned true.


pid_t waitpid(pid_t pid, int *status, int options);
	pid
		a. <-1 -组id
		b. -1 回收任意
		c. 0 回收和调用进程组id相同组内的子进程
		d. >0 回收指定的pid
	options
		a. 0与wait相同,也会阻塞
		b. WNOHANG 如果没有当前子进程立即退出的,会立刻返回
	返回值
		a.如果设置了,WNOHANG,
			1).如果没有子进程退出,返回0
			2).如果有子进程退出,返回退出的pid
		b. 失败返回-1(没有子进程)

调用案例
在这里插入图片描述
以下是 pid_t pid 这个参数对应的不同值的介绍
ps: 0 这个参数不明白是什么意思??? 这里记录一下

 < -1   meaning  wait  for  any  child process whose process group ID is equal to the absolute value of
              pid.
 -1     meaning wait for any child process.

 0      meaning wait for any child process whose process group ID is  equal  to  that  of  the  calling
              process.

 > 0    meaning wait for the child whose process ID is equal to the value of pid.

  1. 用wait 回收多个子进程 调用案例

在这里插入图片描述

  1. 用waitpid函数回收多个子进程调用案例
#include "stdio.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "unistd.h"

int main() {
    int i;
    int n = 5;
    pid_t pid;
    pid_t wpid;
    for(i=0; i<n; ++i) {
        pid = fork();
        if(pid == 0) {
            break;
        }
    }
    if(i==5) {
        printf("my is father progress !\n");
        while(1) {
            wpid = waitpid(-1, NULL, WNOHANG);
            if(wpid==-1) {
                printf("zi ji cheng hui shou wan bi\n");
                break;
            } else if (wpid>0) {
                printf("hui shou de zi jin cheng pid is%d\n", wpid);
            }
        }   
        while(1) {
            sleep(1);
        }
    }
    if(i<5) {
        printf("my is son progress! my pid is%d\n", getpid());
    } 
    return 0;
}

  1. 创建子进程,调用fork之后,在子进程调用自定义程序(段错误,浮点型错误),用waitpid回收,查看退出状态
    代码示例:
#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "unistd.h"

int main () {
    pid_t pid;
    pid_t wpid;
    int status;
    pid = fork();
    if(pid==0) {
        // zi jin cheng
        execl("./test3gz.out", "test3gz.out", NULL);
    } else {
       while(1) {
           wpid = waitpid(-1, &status, WNOHANG);
           if(wpid == -1) {
                printf("suo you zi jin cheng tui chu\n");
                break;
           } else if(wpid>0) {
                printf("tui chu de zi jin cheng pid shi%d\n", wpid);
                printf("status is %d\n", status);
                if(WIFEXITED(status)) {
                    printf("zheng chang tui chu return status is %d\n", WEXITSTATUS(status));
                }
                if(WIFSIGNALED(status)) {
                    printf("zi ji cheng kill by signal is %d\n", WTERMSIG(status));
                }
           }
       } 
       while(1) {
            sleep(1);
       }
    }
    return 0;
}

输出:

a. 当test3gz.out没有发生浮点型错误的时候
this is test3gz.c
k = 3
tui chu de zi jin cheng pid shi18293
status is 0
zheng chang tui chu return status is 0
suo you zi jin cheng tui chu
b. 当test3gz.out发生浮点型错误的时候
tui chu de zi jin cheng pid shi18278
status is 136
zi ji cheng kill by signal is 8
suo you zi jin cheng tui chu

  1. 子进程与父进程共享文件描述符表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值