getting the exit value of an exec'd program

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Aug 17 00:40:38 AEST 1990


In article <1990Aug15.223952.1175 at NCoast.ORG> atul at 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 */
		}



More information about the Comp.unix.questions mailing list