Thread: Trouble with fork() & alarm()...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Trouble with fork() & alarm()...

    Hi all,

    After countless hours going over my code I have no idea why it doesn't want to work...

    I'm trying to use fork() to print two messages for 5 seconds each using alarm() to swap the active process to the other.
    When I run my code it prints the [Parent] message alone for 5 seconds, however it continues to run simultaneously when the [Child] wakes from its initial sleep.
    After 5 seconds of simultaneous processing the [Parent] continues to run alone indefinately; the [Child] never returns...
    This leads me to believe that the [Parent] never enters the handler function but I can't work out why...

    Code:
    /*  */
    
    
    
    // Pre-processor Directives /////////////////////////////////////
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <wait.h>
    
    /////////////////////////////////////////////////////////////////
    
    
    
    // Function Prototypes //////////////////////////////////////////
    
    void handler( int );
    
    /////////////////////////////////////////////////////////////////
    
    
    
    // Main Function Implementation /////////////////////////////////
    
    int i, j = 1;
    
    int main( void )
    {
    	if( fork() != 0 )
    	{
    		while( 1 )
    		{
    			signal( SIGALRM, handler );
    			if( i == 1 )
    			{
    				alarm( 5 );
    				i--;
    			}
    			printf( "[PARENT] Serving HTTP Session 2\n" );fflush( stdout );
    		}
    	}
    	else
    	{
    		sleep( 5 ); /* Allow the parent to get ready. */
    		while( 1 )
    		{
    			signal( SIGALRM, handler );
    			if( j == 1 )
    			{
    				alarm( 5 );
    				j--;
    			}
    			printf( "[CHILD] Serving HTTP Session 1\n" );fflush( stdout );
    		}
    	}
    	exit( EXIT_SUCCESS );
    }
    
    /////////////////////////////////////////////////////////////////
    
    
    
    // Sub-function Implementation //////////////////////////////////
    
    void handler( int SIG )
    {
    	if( i = 0 )
    		i++;
    	if( j = 0 )
    		j++;
    	pause(); /* Pause the process until the other process sends it's alarm signal. */
    	signal( SIGALRM, handler );
    }
    
    /////////////////////////////////////////////////////////////////
    Any thoughts?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int i, j = 1;
    You don't initialize i.

    Code:
    void handler( int SIG )
    {
        if( i = 0 )
            i++;
        if( j = 0 )
            j++;
    You are missing a "=" in both conditions.

    Bye, Andreas

  3. #3
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Thanks! You fail to see these OBVIOUS things after too long! ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alarm()
    By TaiL in forum C Programming
    Replies: 1
    Last Post: 07-16-2009, 09:55 AM
  2. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  3. Fork - Pipe trouble
    By Markyboy in forum C Programming
    Replies: 4
    Last Post: 07-06-2008, 11:09 PM
  4. Alarm
    By kky2k in forum C Programming
    Replies: 10
    Last Post: 05-29-2007, 11:58 PM
  5. alarm
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 09:35 AM