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  byreturning from main().WEXITSTATUS(status)returns  the  exit  status  of the child.  This consists of the least significant 8 bits of thestatus argument that the child specified in a call to exit(3) or _exit(2) or  as  the  argumentfor  a  return  statement  in main().  This macro should be employed only if WIFEXITED returnedtrue.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 shouldbe employed only if WIFSIGNALED returned true.

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

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

 < -1   meaning  wait  for  any  child process whose process group ID is equal to the absolute value ofpid.-1     meaning wait for any child process.0      meaning wait for any child process whose process group ID is  equal  to  that  of  the  callingprocess.> 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 chengexecl("./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. 子进程与父进程共享文件描述符表

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382526.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux C语言C++ makefile文件编写

ps: 这里 不是很明白&#xff1f;尤其是 后面 三个变量&#xff0c;什么区别&#xff1f; $ 代表目标 $^ 代表全部依赖 $< 第一个依赖 $? 第一个变化的依赖 makefile makefile 命名规则makefileMakefilemakefile 三要素目标依赖规则命令 写法&#xff1a; 目标:依赖 tab键…

Ubuntu升级

apt-get update: 升级安装包相关的命令,刷新可安装的软件列表(但是不做任何实际的安装动作) apt-get upgrade: 进行安装包的更新(软件版本的升级) apt-get dist-upgrade: 进行系统版本的升级(Ubuntu版本的升级) do-release-upgrade: Ubuntu官方推荐的系统升级方式,若加参数-…

软件工程学习笔记《一》什么是软件工程

文章目录软件工程学习笔记目录软件工程过程软件工程方法软件质量软件质量如何评价软件的质量模型ISO9126模型易用性&#xff1a;效率可维护性可移植性为什么内存缓冲区是2048或4096软件工程学习笔记目录 [https://blog.csdn.net/csdn_kou/article/details/83754356] 单纯摆出一…

linux C语言 文件相关知识01

ps:文件描述符表&#xff0c;与文件指针 有什么联系&#xff1f;&#xff1f;&#xff1f; 1. linux 系统&#xff0c;一般一个进程 允许打开的最大文件数量是 1024&#xff0c; 对应内核区的进程控制块&#xff08;pcb&#xff09;中的文件描述符表的范围&#xff0c; 在shell…

linux 系统函数调用:open close read write lseek

open函数 查看函数原型 man 2 open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);相关参数用法介绍&#xff1b; a. pathname …

PyCharm安装和配置教程

文章目录官网链接错误类型&#xff01;1.你的用户名是中文解决方案PyCharm的汉化和配置PyCharm的简单使用入门PyCharm和git安装教程官网链接 www.jetbrains.com 错误类型&#xff01;1.你的用户名是中文 安装第一次各种问题&#xff01;就不多了&#xff0c;反正各种报错 原…

linux 进程通信 pipe

pipe函数 管道函数 man pipe #include <unistd.h> int pipe(int pipefd[2]);参数介绍&#xff1a;pipefd读写文件描述符&#xff0c;0-代表读&#xff0c; 1-代表写父子进程实现pipe通信&#xff0c;实现ps aux | grep bash 功能 经常出现的问题&#xff1a; 父进程认为…

软件工程学习笔记《二》代码规范

文章目录软件工程学习笔记目录google代码规范节选python来自google翻译错误注释的示例命名规范import语句的规范import this 源码软件工程学习笔记目录 [https://blog.csdn.net/csdn_kou/article/details/83754356] google代码规范 https://github.com/google/styleguide 节…

Linux 进程通信之FIFO

FIFO通信&#xff08;first in first out&#xff09; FIFO 有名管道&#xff0c;实现无血缘关系进程通信。 ----创建一个管道的伪文件 a.mkfifo testfifo 命令创建 b.也可以使用函数int mkfifo(const char *pathname, mode_t mode); ----内核会针对fifo文件开辟一个缓冲区&…

PyCharm和git安装教程

文章目录先到官网下载git进入setting&#xff0c;如黄色部分如果你用的是github那么直接setting登陆就行了如果你是gitee的话首先进入setting然后Plugins点击browse查找gitee如图所示&#xff01;最后点击重启ok《不要自己关闭&#xff0c;否则安装失败》安装好了以后,输入你的…

linux 进程通信子mmap

mmap 文件–内存映射 函数原型 #include <sys/mman.h>void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);参数介绍&#xff1a; add 传 NULL length 映射区的长度 protPROT_READ 可读PROT_WRITE可写 flagsMAP_SHARED 共享的&#xff0c…

malloc和calloc的区别

是否对申请的区域进行初始化而已 但是我想你也知道我们写程序的时候多用malloc而很少用calloc&#xff0c;何解&#xff1f; 因为calloc虽然对内存进行了初始化&#xff08;全部初始化为0&#xff09;&#xff0c;但是同样也要降低效率的 calloc相当于 p malloc(); memset(p,…

linux信号学习02

未决信号集与阻塞信号集(信号屏蔽字) 阻塞信号集&#xff1a; 将某些信号加入集合&#xff0c;对他们设置屏蔽&#xff0c;当屏蔽x信号后&#xff0c;再收到该信号&#xff0c;该信号的处理将推后(解除屏蔽后) 未决信号集&#xff1a; a. 信号产生&#xff0c;未决信号集中描述…

task_struct 结构如何查看及分析

cd /find -name sched.hvim usr/src/kernels/3.10.0862.6.3.el7.x86_64/include/linux/sched.hhttps://www.cnblogs.com/zxc2man/p/6649771.html 进程是处于执行期的程序以及它所管理的资源&#xff08;如打开的文件、挂起的信号、进程状态、地址空间等等&#xff09;的总称。…

linux 与信号集操作相关的函数

与信号集操作相关的函数 #include <signal.h> 清空信号集 全都为0 int sigemptyset(sigset_t *set);填充信号集 全都为1 int sigfillset(sigset_t *set);添加某个信号到信号集 int sigaddset(sigset_t *set, int signum);从集合中删除某个信号 int sigdelset(sigset_t *s…

软件工程学习笔记《三》代码优化和性能测试

文章目录软件工程学习笔记目录如何在开源社区提问&#xff1f;代码审查代码优化运行结果参数解释代码优化原则对常见的数据结构排序算法进行测试关于冒泡排序优化的探讨结果软件工程学习笔记目录 [https://blog.csdn.net/csdn_kou/article/details/83754356] 如何在开源社区提…

linux信号捕捉

信号捕捉&#xff0c;防止进程意外死亡 signal函数 man signal #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);参数介绍&#xff1b; signum 要捕捉的信号 handler 要执行的捕捉函数指针&#xff0c…

软件工程学习笔记《目录》

软件工程学习笔记《目录》 软件工程学习笔记《一》什么是软件工程 软件工程学习笔记《二》代码规范 软件工程学习笔记《三》代码优化和性能测试 软件工程学习笔记《四》需求分析

linux进程利用SIGCHLD信号,来实现父进程回收子进程

子进程执行完毕后&#xff0c;会向父进程发出 SIGCHLD信号 &#xff0c; 这段代码实现的就是i&#xff0c;父进程接受到子进程 发出的SIGCHLD信号&#xff0c;实现对子进程进行回收&#xff0c;从而避免僵尸进程 #include <stdio.h> #include <unistd.h> #include…