日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux C++ 多进程初步02

發布時間:2023/11/30 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux C++ 多进程初步02 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ps:疑惑的地方,1 進程pcb的概念, 還有 ulimit -a 顯示的信息 是一個進程可以最大占用資源的上限嗎? 還有 文件描述符的概念?? 這里不是很明白!記錄一下2還有WIFEXITED

  • 孤兒進程 與僵尸進程
    孤兒進程: 子進程運行,父進程終止, 子進程就是孤兒進程
  • 僵尸進程:進程終止,父進程尚未回收,子進程殘留資源(pcb)存放于內核中,變成(Zombie)僵尸進程

  • 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.
  • 用wait 回收多個子進程 調用案例
  • 用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; }
  • 創建子進程,調用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
  • 子進程與父進程共享文件描述符表
  • 總結

    以上是生活随笔為你收集整理的linux C++ 多进程初步02的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。