//---------------------------------------------------------------- // killdemo.cpp // // This program illustrates the use of the 'kill()' library- // function, which a process calls in order to send a signal // to another process. Here our main program forks a child- // process which installs a signal-handler, then waits until // it receives a 'SIGUSR1' signal (whereupon it exits). The // parent-process will send the child-process this signal if // a user presses the -key. // // programmer: ALLAN CRUSE // written on: 16 SEP 2004 //---------------------------------------------------------------- #include // for printf() #include // for signal(), kill() #include // for fork() #include // for exit() #include // for wait() int done = 0; // global variable void upon_signal( int intnum ) { printf( "signum=%d\n", intnum ); done = 1; } int main( void ) { int pid = fork(); if ( pid == 0 ) { // child-process signal( SIGUSR1, upon_signal ); while ( !done ); printf( "child is quitting\n\n" ); } else { // parent-process getchar(); kill( pid, SIGUSR1 ); int status = 0; wait( &status ); printf( "parent is quitting\n\n" ); } exit(0); }