#include #include #include #include #include int main() { int x = 100; pid_t pid; /* process id */ /* fork a child process */ pid = fork(); /*** fork() returns twice... ***/ if ( pid < 0 ) { fprintf( stderr, "ERROR: fork() failed.\n" ); exit( 1 ); } if ( pid == 0 ) /* child */ { printf( "CHILD: I'm new.\n" ); x++; printf( "CHILD: x is %d\n", x ); exit( 0 ); } else /* parent (pid > 0) */ { printf( "PARENT: created process id %d\n", pid ); /* wait until child terminates */ wait( NULL ); printf( "PARENT: child process terminated" ); printf( "PARENT: x is %d\n", x ); } return 0; }