Thread: Why doesnt this program terminate?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    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 );
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as there is no loops in the code, I fail to see how it could not terminate... Does it perform "printenv" correctly?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Sorry, my bad I ran it through another program which isi the one that doesnt terminate.

    Thx anyway :-)

    //T

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what you are saying is that YOUR application works OK as it's posted here, but if the application instead of "printenv" doesn't terminate, neither does yours [naturally, as it's waiting for that application to terminate]?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Something like that.

    This is our first real programming assignment in c, and so our teachers have kindly supplied us with a small program called runsafe which terminates all child processes after a set amount of time so that we dont leave a bunch of unfinished processes every time we execute our unfinished programs:-)

    This is the program that does not terminate (until the set time). I thought it was my program that didn't terminate and that runsafe had to kill it.

    //T

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM