Hi all,
I'm trying to write a program which will give two processes 5 seconds each to write their message to the screen.
The problem is that the pause function isn't unpausing when the second process emits it's SIGALRM...
Would somebody please be able to advise me on where I'm going wrong?
Please see my code below:
Code:/* */ // Pre-processor Directives ///////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <wait.h> #include <signal.h> ///////////////////////////////////////////////////////////////// // Function Prototypes ////////////////////////////////////////// void handler( int ); ///////////////////////////////////////////////////////////////// // Main Function Implementation ///////////////////////////////// int i = 1, 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 ) { printf( "[HANDLER] Signal recieved!\n" );fflush( stdout ); signal( SIG, handler ); pause(); /* Pause the process until the other process sends it's alarm signal. */ if( i == 0 ) i++; if( j == 0 ) j++; } /////////////////////////////////////////////////////////////////



LinkBack URL
About LinkBacks



