代码分析:
/* waitpid.c */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> int main()
{
pid_t pc, pr; pc = fork();
if( pc < )
{
printf("Error fork\n");
exit();
}
else if( pc == ) /* 子进程 */
{
/* 子进程暂停5s */
sleep();
/* 子进程正常退出 */
exit();
}
else /* 父进程 */
{
/* 循环测试子进程是否退出 */
do
{
/* 调用waitpid,且父进程不阻塞 */
pr = waitpid(pc, NULL, WNOHANG); /* 若子进程还未退出,则父进程暂停1s */
if( pr == )
{
printf("The child process has not exited\n");
sleep();
}
}while( pr == ); /* 若发现子进程退出,打印出相应情况 */
if( pr == pc )
{
printf("Get child exit code: %d\n",pr);
}
else
{
printf("Some error occured.\n");
}
}
}