Linux 二十一章

🐶博主主页:@ᰔᩚ. 一怀明月ꦿ 

❤️‍🔥专栏系列:线性代数C初学者入门训练题解CC的使用文章「初学」C++linux

🔥座右铭:“不要等到什么都没有了,才下定决心去做”

🚀🚀🚀大家觉不错的话,就恳求大家点点关注,点点小爱心,指点指点🚀🚀🚀

目录

非阻塞等待

当父进程进行非阻塞等待的时候,父进程完成其他的任务,比如download、printlog、show

进程等待的必要性

进程程序替换

execl

execlp

execv

execvp

替换c++程序

替换shell语言程序

exec族函数为什么可以替换不同语言的程序?

当一个程序运行的整个过程


非阻塞等待

在 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
============
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值