#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <error.h>
int main(int argc ,char **argv)
{
pid_t pid;
pid_t pid_c;
int status;
pid=fork();
if(pid==0)
{
printf("child process will sleep 3 seconds\n");
sleep(3);
printf("child PID[%d] will exit with status 3.\n",getpid());
exit(3);
}
else if(pid>0)
{
do
{
pid_c=waitpid(pid,&status,WNOHANG);
if(pid_c==0)
{
printf("NO child process exit\n");
sleep(1);
}
}while(pid_c==0);
if(WIFEXITED(status))
{
printf("child process %d will exit normally\n",pid_c);
printf("return code is %d\n",WEXITSTATUS(status));
}
else
{
printf("child process %d will exit normally\n",pid_c);
}
}
else
{
printf("error");
exit(1);
}
return 0;
}