Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!yale!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.unix.questions Subject: Re: getting the exit value of an exec'd program Message-ID: <13601@smoke.BRL.MIL> Date: 16 Aug 90 14:40:38 GMT References: <1990Aug15.223952.1175@NCoast.ORG> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 41 In article <1990Aug15.223952.1175@NCoast.ORG> atul@NCoast.ORG (Atul Parulekar) writes: >May be the answer is in the manual, but I have not been able to find it. >My problem is that if I run a program using fork and execvp, how do I get >the exit value of the exec'd program into the main program. Via wait(). The general procedure for running a subprocess is, in outline: switch ( (pid = fork()) ) { case -1: Punt( "unable to fork" ); /*NOTREACHED*/ case 0: /* child */ args[0] = "command"; execvp( args[0], args ); /* WARNING: uses $PATH */ _exit( 127 ); /*NOTREACHED*/ default: /* parent */ while ( (w = wait( &status )) != pid ) if ( w == -1 && errno != EINTR ) break; if ( w == -1 ) { Punt( "child disappeared" ); /*NOTREACHED*/ } else if ( (status & 0xFF) != 0 ) { /* (status & 0x7F) is the signal number */ /* (status & 0x80) != 0 iff core dumped */ Punt( "child terminated abnormally" ); /*NOTREACHED*/ } else status = status >> 8 & 0xFF; /* exit status */ }