#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t childpid,a;
int status;
childpid = fork();
printf("childpid = %d\n",childpid);
if ( childpid < 0 )
{
perror( "fork()" );
exit( EXIT_FAILURE );
}
else
if ( childpid == 0 )
{
puts( "In child process" );
//sleep( 3 );//让子进程睡眠3秒,看看父进程的行为
printf("\tchild pid = %d\n", getpid());
printf("\tchild ppid = %d\n", getppid());
exit(EXIT_SUCCESS);
}
else
{
a = waitpid( childpid, &status, 0 );
printf ("a = %d\n",a);
puts( "in parent" );
printf( "\tparent pid = %d\n", getpid() );
printf( "\tparent ppid = %d\n", getppid() );
printf( "\tchild process exited with status %d \n", status );
}
exit(EXIT_SUCCESS);
}