Why doesnt this program terminate?
Hi!
I'm a complete c noob and I'm trying to get do a lab assignment for uni. What I don't understand at the moment is why the following code doesn't terminate, even though I use exit() ?
Code:
pid_t child_PID; /* Process-ID of child */
int main( int argc, char *argv[] )
{
int status;
child_PID = fork();
if( -1 == child_PID )
{
fprintf( stderr, "Failed to fork" );
exit( 1 );
}
if( 0 == child_PID )
{
execlp("printenv", "printenv", NULL );
}
child_PID = wait( &status);
if( -1 == child_PID )
{
perror( "wait() failed unexpectedly, exiting");
exit( 1 );
}
/* Child process finished */
if( WIFEXITED( status ) )
{
int child_status = WEXITSTATUS( status );
if( 0 != child_status )
{
fprintf( stderr, "Child (pid %ld) failed with exit code %d\n",
(long int) child_PID, child_status );
exit( 1 );
}
else
{
fprintf( stderr, "child finished normally\n" );
}
}
else
{
/* Child process interrupted by signal */
if( WIFSIGNALED( status ) )
{
int child_signal = WTERMSIG( status );
fprintf( stderr, "Child (pid %ld) was terminated by signal no. %d\n",
(long int) child_PID, child_signal );
exit( 1 );
}
}
exit( 0 );
}