当前位置:网站首页>详解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,从而为我们编程提供了另一种更灵活的方式。
边栏推荐
- Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
- INSTALL_RPATH and BUILD_RPATH problem in CMake
- 22 high mid term paper topics forecast
- BeautifulSoup4的介绍与使用
- Unity Gobang Game Design and Simple AI (2)
- [MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
- The singleton pattern
- CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
- Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
- MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
猜你喜欢
Harbor Enterprise Mirror Warehouse Construction
zip压缩包密码解密
Use of PlantUML plugin in idea
6 states of a thread
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
C语言实现顺序栈和链队列
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
Data center project preliminary summary
idea中PlantUML插件使用
GNNExplainer applied to node classification task
随机推荐
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
CMake中INSTALL_RPATH与BUILD_RPATH问题
使用百度EasyDL实现智能垃圾箱
中英文说明书丨CalBioreagents 醛固酮单克隆抗体
The solution that does not work and does not take effect after VScode installs ESlint
2022.8.8DAY628
[HNOI2002]营业额统计
Program Performance Analysis - Complexity Analysis
vs番茄助手的方便功能和便捷快捷键介绍
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
VS2019常用快捷键
uniapp实现防抖搜索
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
Kubernetes apparmor profile
golang zip aes base64
Initials-Letter Query Tool-Word Abbreviation Query Online Tool
像天才一样思考:如何培养自己的创造力?
输入框最前面添加放大镜&&background-image和background-color冲突问题
VB.net程序关闭后后台还在与SQL连接
[R language] Normalize and organize files into folders of various file types