/*
 * sigaction.c.3
 */
#include <pthread.h>
#include <stdio.h>
#include "utils.h"

extern int getpid( void );

void
ctrl_c_handler( int sig )
{
	print_str("^C interrupt.");
	pthread_kill( NULL, SIGKILL );
}

void
term_handler( int sig )
{
	pthread_lock_global_np();
	printf("Caught %d %s in handler!\n", sig, sys_signame[sig] );
	pthread_unlock_global_np();
}

void
catcher( void )
{
	struct timespec ts = { 0, 50000 };

	while( 1 )
	{
		print_str("catcher");
		pthread_delay_np( &ts );
	}

	pthread_exit( (void *) 0 );
}


int main()
{
   pthread_t th;
   struct sigaction act;
   struct timespec ts = { 0, 100000 };

   act.sa_handler = term_handler;
   sigemptyset( &act.sa_mask );
   pthread_sigaction_np( SIGTERM, &act, NULL );

   act.sa_handler = ctrl_c_handler;
   sigemptyset( &act.sa_mask );
   pthread_sigaction_np( SIGINT, &act, NULL );

   pthread_create( &th, NULL, (thread_proc_t) catcher, NULL );

   /*
    * Sleep for 0.5 seconds, wake-up, and send a signal to the
	* catcher thread.  Do this until the process takes a kill -9.
    */
   while( 1 )
   {
	   pthread_delay_np( &ts );
	   pthread_kill( th, SIGTERM );
   }

   return(0);
}
