2 unsigned int sleep(unsigned int seconds)
4 struct sigaction new_action, old_action;
5 sigset_t old_mask, stop_mask, sleep_mask;
6 /* set the signal handler */
7 sigemptyset(&new_action.sa_mask); /* no signal blocked */
8 new_action.sa_handler = alarm_hand; /* set handler */
9 new_action.sa_flags = 0; /* no flags */
10 sigaction(SIGALRM, &new_action, &old_action); /* install action */
11 /* block SIGALRM to avoid race conditions */
12 sigemptyset(&stop_mask); /* init mask to empty */
13 sigaddset(&stop_mask, SIGALRM); /* add SIGALRM */
14 sigprocmask(SIG_BLOCK, &stop_mask, &old_mask); /* add SIGALRM to blocked */
17 /* going to sleep enabling SIGALRM */
18 sleep_mask = old_mask; /* take mask */
19 sigdelset(&sleep_mask, SIGALRM); /* remove SIGALRM */
20 sigsuspend(&sleep_mask); /* go to sleep */
21 /* restore previous settings */
22 sigprocmask(SIG_SETMASK, &old_mask, NULL); /* reset signal mask */
23 sigaction(SIGALRM, &old_action, NULL); /* reset signal action */
24 /* return remaining time */
27 void alarm_hand(int sig)
29 return; /* just return to interrupt sigsuspend */