当前位置:网站首页>详解C语言中的wait()函数和waitpid()函数
详解C语言中的wait()函数和waitpid()函数
2022-08-09 06:29:00 【qq_36412526】
C语言wait()函数:结束(中断)进程函数(常用)
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait (int * status);
函数说明:wait()会暂时停止目前进程的执行, 直到有信号来到或子进程结束. 如果在调用wait()时子进程已经结束, 则wait()会立即返回子进程结束状态值. 子进程的结束状态值会由参数status 返回, 而子进程的进程识别码也会一快返回. 如果不在意结束状态值, 则参数 status 可以设成NULL. 子进程的结束状态值请参考waitpid().
返回值:如果执行成功则返回子进程识别码(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()函数:中断(结束)进程函数(或等待子进程中断)
#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,从而为我们编程提供了另一种更灵活的方式。
边栏推荐
- How to find package information and pin definitions for NXP S32K1xx series microcontrollers
- C# 利用iTextSharp画PDF
- Remember a nest.js route that matches all the path problems that follow
- vs番茄助手的方便功能和便捷快捷键介绍
- Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
- 什么是excel文件保护
- Use of PlantUML plugin in idea
- 中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
- Likou Brush Question 180
- Deep Learning - Principles of Neural Networks 2
猜你喜欢
[GO]、数组与切片
Use of PlantUML plugin in idea
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
Inception V3 闭眼检测
GNNExplainer applied to node classification task
运放-运算放大器经典应用电路大全-应用电路大全
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
C language implements sequential stack and chain queue
力扣刷题180
05 多线程与高并发 - ThreadPoolExecutor 源码解析
随机推荐
【R语言】交互作用 测试数据
代码目录结构
Harbor Enterprise Mirror Warehouse Construction
Program Performance Analysis - Complexity Analysis
VS2019常用快捷键
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Web APIs BOM- 操作浏览器:本地存储
按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程
Simple Factory Pattern
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
db.sqlite3没有“as Data Source“解决方法
How to automatically fill down an excel table without dragging the mouse down
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
String.toLowerCase(Locale.ROOT)
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
带头双向循环链表的增删查改(C语言实现)
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
VB.net程序关闭后后台还在与SQL连接
String.toLowerCase(Locale.ROOT)
Likou Brush Question 180