#include #include #include #include #include int main() { 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. Let me look around.\n" ); #if 0 sleep( 30 ); #endif /* execute a new program in the process space */ execlp( "/bin/ls", "ls", "-l", NULL ); /* any code here will never be executed */ } else /* parent (pid > 0) */ { printf( "PARENT: created process id %d\n", pid ); #if 0 sleep( 30 ); #endif /* wait until child terminates */ wait( NULL ); printf( "PARENT: child process terminated" ); } return 0; }