Skip to content

Instantly share code, notes, and snippets.

@satoyuichi
Created May 4, 2012 08:56
Show Gist options
  • Save satoyuichi/2593422 to your computer and use it in GitHub Desktop.
Save satoyuichi/2593422 to your computer and use it in GitHub Desktop.
Fork test
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char** argv );
int main( int argc, char** argv )
{
int pid;
printf( "Start main function.(%d)\n", __LINE__ );
pid = fork();
printf( "Forked process id is %d.(%d)\n", pid, __LINE__ );
switch( pid ) {
case 0:
printf( "I am child process.(%d)\n", __LINE__ );
exit( 0 );
break;
case -1:
printf( "Fork is failed.(%d)\n", __LINE__ );
exit( 0 );
break;
default:
printf( "I am parent process.(%d)\n", __LINE__ );
wait( NULL );
break;
}
printf( "End main function.(%d)\n", __LINE__ );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment