Like this?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
void handler( int );
int main( void )
{
signal( SIGUSR1, handler );
pid_t pid = fork();
if( pid != 0 )
{
while( 1 )
{
printf( "[PARENT] Serving HTTP Session 2\n" );
fflush( stdout );
sleep(5);
kill(pid,SIGUSR1);
pause();
}
}
else
{
pause(); // wait for first signal from parent
pid = getppid(); // this is child, get the parent
while( 1 )
{
printf( "[CHILD] Serving HTTP Session 1\n" );
fflush( stdout );
sleep(5);
kill(pid,SIGUSR1);
pause();
}
}
exit( EXIT_SUCCESS );
}
/////////////////////////////////////////////////////////////////
// Sub-function Implementation //////////////////////////////////
void handler( int SIG )
{
printf( "[HANDLER] Signal received in process %d\n", (int)getpid() );
fflush( stdout );
signal( SIGUSR1, handler );
}