概览
使用 fork
后,可能需要获取 fork
的进程的运行状况,比如有没有异常、崩溃。
wait
在 man 中关键的描述如下:
All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal.
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int status;
printf("before fork\n");
fflush(stdout);
if ( (pid = fork()) < 0)
{
printf("fork error\n");
}
else if (pid == 0)
{
printf("after fork, child\n");
// 4种测试情况
exit(7); // -> normal termination, exitstatus = 7
// abort(); -> abnormal termination, signalstatus = 6 (SIGABRT)
// int i = 1 0; -> abnormal termination, signalstatus = 8 (SIGFPE)
// char *p = NULL; *p = 'a'; -> abnormal termination, signalstatus = 11 (SIGSEGV)
}
wait(&status);
if (WIFEXITED(status))
{
printf("normal termination, exitstatus = %d\n", WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
printf("abnormal termination, signalstatus = %d\n", WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status)?"(core file generated)":"");
#else
"");
#endif
}
else if (WIFSTOPPED(status))
{
printf("child stopped, signal number = %d\n", WSTOPSIG(status));
}
printf("after fork, parent\n");
return 0;
}复制
运行效果
文章转载自进击的代码,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
OceanBase 接入 MCP 架构:贯通数据孤岛,释放 AI 创新潜能
OceanBase数据库
283次阅读
2025-03-28 15:32:52
AI关键场景得到全面支持!OceanBase入选Forrester报告三大领域代表厂商
OceanBase数据库
195次阅读
2025-04-19 22:27:54
[MYSQL] 服务器出现大量的TIME_WAIT, 每天凌晨就清零了
大大刺猬
176次阅读
2025-04-01 16:20:44
Apache Doris 2025 Roadmap:构建 GenAI 时代实时高效统一的数据底座
SelectDB
173次阅读
2025-04-03 17:41:08
定档!2025 OceanBase开发者大会,5月17日广州见!
OceanBase数据库
125次阅读
2025-04-09 16:48:47
OceanBase首届合作伙伴峰会:携手伙伴共赢云和AI时代
OceanBase数据库
120次阅读
2025-03-26 15:29:59
AI加持后能自动化运维吗?
薛晓刚
109次阅读
2025-03-24 21:35:32
瓜分 10 万奖金!OceanBase 首届 AI 黑客松等你来战
OceanBase数据库
105次阅读
2025-04-10 18:19:58
[MYSQL] query_id和STATEMENT_ID在不同OS上的关系
大大刺猬
85次阅读
2025-03-26 19:08:13
OceanBase首届生态伙伴大会圆满收官,开启生态建设2.0新征程
OceanBase数据库
79次阅读
2025-03-28 15:59:27