当前位置:网站首页>Explain the wait() function and waitpid() function in C language in detail
Explain the wait() function and waitpid() function in C language in detail
2022-08-09 06:33:00 【qq_36412526】
C语言wait()函数:结束(中断)进程函数(常用)
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait (int * status);
函数说明:wait()会暂时停止目前进程的执行, 直到有信号来到或子进程结束. 如果在调用wait()时子进程已经结束, 则wait()会立即返回子进程结束状态值. 子进程的结束状态值会由参数status 返回, 而子进程的进程识别码也会一快返回. 如果不在意结束状态值, 则参数 status 可以设成NULL. The child to the end of the status value refer towaitpid().
返回值:如果执行成功则返回子进程识别码(PID), 如果有错误发生则返回-1. 失败原因存于errno 中.
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
int status, i;
if(fork() == 0)
{
printf("This is the child process. pid =%d\n", getpid());
exit(5);
}
else
{
sleep(1);
printf("This is the parent process, wait for child...\n";
pid = wait(&status);
i = WEXITSTATUS(status);
printf("child's pid =%d . exit status=^d\n", pid, i);
}
}
This is the child process. pid=1501
This is the parent process, wait for child...
child's pid =1501, exit status =5
C语言waitpid()函数:中断(结束)进程函数(Or wait for the child to interrupt)
#include <sys/types.h> #include <sys/wait.h>
pid_t waitpid(pid_t pid, int * status, int options);
函数说明:waitpid()会暂时停止目前进程的执行, 直到有信号来到或子进程结束. 如果在调用wait()时子进程已经结束, 则wait()会立即返回子进程结束状态值. 子进程的结束状态值会由参数status 返回, 而子进程的进程识别码也会一快返回. 如果不在意结束状态值, 则参数status 可以设成NULL. 参数pid 为欲等待的子进程识别码, 其他数值意义如下:
1、pid<-1 等待进程组识别码为pid 绝对值的任何子进程.
2、pid=-1 等待任何子进程, 相当于wait().
3、pid=0 等待进程组识别码与目前进程相同的任何子进程.
4、pid>0 等待任何子进程识别码为pid 的子进程.
参数option 可以为0 或下面的OR 组合:
WNOHANG:如果没有任何已经结束的子进程则马上返回, 不予以等待.
WUNTRACED:如果子进程进入暂停执行情况则马上返回, 但结束状态不予以理会. 子进程的结束状态返回后存于status, 底下有几个宏可判别结束情况
WIFEXITED(status):如果子进程正常结束则为非0 值.
WEXITSTATUS(status):取得子进程exit()返回的结束代码, 一般会先用WIFEXITED 来判断是否正常结束才能使用此宏.
WIFSIGNALED(status):如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status):取得子进程因信号而中止的信号代码, 一般会先用WIFSIGNALED 来判断后才使用此宏.
WIFSTOPPED(status):如果子进程处于暂停执行情况则此宏值为真. 一般只有使用WUNTRACED时才会有此情况.
WSTOPSIG(status):取得引发子进程暂停的信号代码, 一般会先用WIFSTOPPED 来判断后才使用此宏.
返回值:如果执行成功则返回子进程识别码(PID), 如果有错误发生则返回-1. 失败原因存于errno 中.
范例:参考wait().
注:
从本质上讲,系统调用waitpid和wait的作用是完全相同的,但waitpid多出了两个可由用户控制的参数pid和options,从而为我们编程提供了另一种更灵活的方式.
边栏推荐
- 语句加锁分析
- vim 程序编辑器的基本操作(积累)
- 抗菌药物丨Toronto Research Chemicals 天冬酰胺D
- 什么是excel文件保护
- GNNExplainer applied to node classification task
- Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
- [R language] interaction test data
- 如何 认识与学习BASH
- 事务总结
- C# 利用iTextSharp画PDF
猜你喜欢
带头双向循环链表的增删查改(C语言实现)
缓存技术使用
sql problem solving statement to create a table
Integer 线程安全的
网络学习总结
Import the pycharm environment package into another environment
中英文说明书丨TRC D-阿卓糖(D-Altrose)
Fragments
工控设备的系统如何进行加固
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
随机推荐
Excel受保护的工作表怎么操作?
SIGINT,SIGKILL,SIGTERM信号区别,各类信号总结
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
Use of PlantUML plugin in idea
C语言实现顺序栈和链队列
2022.8.8DAY628
AD画PCB板教程 20分钟讲清楚操作流程 铺铜 网络标号
BeautifulSoup4的介绍与使用
DDD 领域驱动设计
单例模式
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
workbench 数据导出
untiy countdown
jvm线程状态
Unity 五子棋游戏设计和简单AI(2)
带头双向循环链表的增删查改(C语言实现)
.NET高级技术
Service
中英文说明书丨TRC D-阿卓糖(D-Altrose)
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比