/* ECP: FILEname=fig13_14.c */ /* 1*/ #include /* 2*/ #include /* 3*/ #include /* 4*/ main( void ) /* 5*/ { /* 6*/ pid_t Pid; /* 7*/ long int i; /* 8*/ if( ( Pid = fork( ) ) == -1 ) /* 9*/ { /*10*/ perror( "" ); /*11*/ exit( -1 ); /*12*/ } /*13*/ if( Pid == 0 ) /*14*/ printf( "I am the child; my parent is %d.", getppid( ) ); /*15*/ else /*16*/ printf( "I am the parent; my child is %d.", Pid ); /*17*/ /* Both Processes Execute Their Own Copies Of this */ /*18*/ printf( " My process id is %d.\n", getpid( ) ); /*19*/ fflush( stdout ); /*20*/ for( i = 0; i < 3000000; i++ ) /*21*/ if( i % 1000000 == 0 ) /*22*/ { /*23*/ printf( "%s\n", Pid ? "Parent" : "Child" ); /*24*/ fflush( stdout ); /*25*/ } /*26*/ return 0; /*27*/ }