training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

tellwait.c (1366B)


      1 #include	<signal.h>
      2 #include	"ourhdr.h"
      3 
      4 static volatile sig_atomic_t	sigflag;
      5 								/* set nonzero by signal handler */
      6 static sigset_t			newmask, oldmask, zeromask;
      7 
      8 static void
      9 sig_usr(int signo)	/* one signal handler for SIGUSR1 and SIGUSR2 */
     10 {
     11 	sigflag = 1;
     12 	return;
     13 }
     14 
     15 void
     16 TELL_WAIT()
     17 {
     18 	if (signal(SIGUSR1, sig_usr) == SIG_ERR)
     19 		err_sys("signal(SIGINT) error");
     20 	if (signal(SIGUSR2, sig_usr) == SIG_ERR)
     21 		err_sys("signal(SIGQUIT) error");
     22 
     23 	sigemptyset(&zeromask);
     24 
     25 	sigemptyset(&newmask);
     26 	sigaddset(&newmask, SIGUSR1);
     27 	sigaddset(&newmask, SIGUSR2);
     28 		/* block SIGUSR1 and SIGUSR2, and save current signal mask */
     29 	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
     30 		err_sys("SIG_BLOCK error");
     31 }
     32 
     33 void
     34 TELL_PARENT(pid_t pid)
     35 {
     36 	kill(pid, SIGUSR2);		/* tell parent we're done */
     37 }
     38 
     39 void
     40 WAIT_PARENT(void)
     41 {
     42 	while (sigflag == 0)
     43 		sigsuspend(&zeromask);	/* and wait for parent */
     44 
     45 	sigflag = 0;
     46 			/* reset signal mask to original value */
     47 	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
     48 		err_sys("SIG_SETMASK error");
     49 }
     50 void
     51 TELL_CHILD(pid_t pid)
     52 {
     53 	kill(pid, SIGUSR1);			/* tell child we're done */
     54 }
     55 
     56 void
     57 WAIT_CHILD(void)
     58 {
     59 	while (sigflag == 0)
     60 		sigsuspend(&zeromask);	/* and wait for child */
     61 
     62 	sigflag = 0;
     63 			/* reset signal mask to original value */
     64 	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
     65 		err_sys("SIG_SETMASK error");
     66 }