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.pswp.cn/news/382526.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382526.shtml
英文地址,請注明出處:http://en.pswp.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…