P169 用 signal 函数进行信号处理

This commit is contained in:
riba2534
2019-01-20 21:08:23 +08:00
parent f62528d701
commit 9d447f635e
5 changed files with 456 additions and 1 deletions

28
ch10/waitpid.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int status;
pid_t pid = fork();
if (pid == 0)
{
sleep(15); //用 sleep 推迟子进程的执行
return 24;
}
else
{
//调用waitpid 传递参数 WNOHANG 这样之前有没有终止的子进程则返回0
while (!waitpid(-1, &status, WNOHANG))
{
sleep(3);
puts("sleep 3 sec.");
}
if (WIFEXITED(status))
printf("Child send %d \n", WEXITSTATUS(status));
}
return 0;
}