🐶博主主页:@ᰔᩚ. 一怀明月ꦿ
❤️🔥专栏系列:线性代数,C初学者入门训练,题解C,C的使用文章,「初学」C++,linux
🔥座右铭:“不要等到什么都没有了,才下定决心去做”
🚀🚀🚀大家觉不错的话,就恳求大家点点关注,点点小爱心,指点指点🚀🚀🚀
目录
当父进程进行非阻塞等待的时候,父进程完成其他的任务,比如download、printlog、show
非阻塞等待
在 Linux 中,可以使用非阻塞方式等待子进程退出或状态改变。这通常通过设置
waitpid()
函数的选项参数中的WNOHANG
标志来实现。这样,waitpid()
函数将立即返回,而不会阻塞当前进程。事例
[BCH@hcss-ecs-6176 11_7]$ cat myprocess.c #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> void Worker(int cnt) { printf("I am a child ,pid: %d, cnt:%d\n",getpid(),cnt); } int main() { pid_t id=fork(); if(id==0) //child { int cnt=5; while(cnt--) { Worker(cnt); sleep(2); } exit(0); } while(1) { //fateher int status=0; pid_t rid=waitpid(id,&status,WNOHANG); if(rid>0) { printf("child quit success,exit code:%d exit sign:%d\n",(status>>8)&0xFF,status&0x7F); } else if(rid==0) { printf("father do other thing......\n"); } else { printf("wait fail\n"); } sleep(1); } return 0; } [BCH@hcss-ecs-6176 11_7]$ ./myprocess father do other thing...... I am a child ,pid: 20429, cnt:4 father do other thing...... I am a child ,pid: 20429, cnt:3 father do other thing...... father do other thing...... I am a child ,pid: 20429, cnt:2 father do other thing...... father do other thing...... I am a child ,pid: 20429, cnt:1 father do other thing...... father do other thing...... I am a child ,pid: 20429, cnt:0 father do other thing...... father do other thing...... child quit success,exit code:0 exit sign:0 wait fail wait fail wait fail wait fail wait fail wait fail wait fail wait fail [BCH@hcss-ecs-6176 ~]$ while :; do ps ajx | grep myprocess | grep -v grep ; sleep 1 ; echo "================================="; done ================================= ================================= ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ================================= 2499 20428 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess 20428 20429 20428 2499 pts/4 20428 S+ 1000 0:00 ./myprocess ============