mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-06-30 18:06:04 +08:00
P169 用 signal 函数进行信号处理
This commit is contained in:
28
ch10/waitpid.c
Normal file
28
ch10/waitpid.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user