mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-02-03 01:53:19 +08:00
29 lines
594 B
C
29 lines
594 B
C
#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;
|
||
}
|