#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main()
{
pid_t pid;
int id, ecode;
pid = getpid();
printf( "[%d] main process before fork() call.\n", pid);
id=fork();
if( id>0 )
{
/* parent process */
printf( "[%d] main process after fork() call, created child process with id=%d.\n", pid, id );
wait( &ecode ); /* % 0xFF for ignoring effect of possible SIGCHLD signal at the end of child */
printf( "[%d] child[%d] process finished with exit code %d.\n", pid, id, ecode % 0xFF);
}
else if( id==0 )
{
/* child process */
pid = getpid();
printf( "[%d] inside child process of parent[%d].\n", pid, getppid() );
exit( 3 ); /* just sample value */
}
else
{
/* fork creation error */
fprintf( stderr, "[%d] error: fork() failed.\n", pid );
}
return 0;
}