//------------------------------------------------------------------- // run.cpp // // This program was written in order to allow a user to see the // value of an application-program's exit-code: the application // will be executed as a child-process, and its exit-code shown // by the parent-prcess, which receives it by calling 'wait()'. // // compile using: $ make run // execute using: $ run {, ... } // // programmer: ALLAN CRUSE // written on: 03 MAY 2005 //------------------------------------------------------------------- #include // for printf(), perror() #include // for fork(), execv() #include // for exit() #include // for wait() int main( int argc, char **argv ) { if ( argc == 1 ) { fprintf( stderr, "No argument\n" ); exit(1); } int pid = fork(); // creates the child-process if ( pid == 0 ) // child-process executes application if ( execv( argv[1], argv+1 ) < 0 ) { perror( "execv" ); exit( 1 ); } // parent-process waits until child-process terminates int status = 0; wait( &status ); // parent retrieves and displays child's exit-status if ( WIFEXITED( status ) ) { printf( "process %s exited with ", argv[1] ); printf( "status=%d \n", (char)WEXITSTATUS( status ) ); } }