Created
April 21, 2013 08:36
-
-
Save Arkham/5428923 to your computer and use it in GitHub Desktop.
POSIX compliant sleep implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "../apue.h" | |
static void | |
my_alrm(int signo) | |
{ | |
/* void */ | |
} | |
unsigned int | |
my_sleep(unsigned int nsecs) | |
{ | |
struct sigaction newact, oldact; | |
sigset_t newmask, oldmask, suspmask; | |
unsigned int unslept; | |
/* set up new handler, save old information */ | |
newact.sa_handler = my_alrm; | |
sigemptyset(&newact.sa_mask); | |
newact.sa_flags = 0; | |
sigaction(SIGALRM, &newact, &oldact); | |
/* block SIGALRM and save current signal mask */ | |
sigemptyset(&newmask); | |
sigaddset(&newmask, SIGALRM); | |
sigprocmask(SIG_BLOCK, &newmask, &oldmask); | |
alarm(nsecs); | |
suspmask = oldmask; | |
sigdelset(&suspmask, SIGALRM); /* make sure SIGALRM isn't blocked */ | |
sigsuspend(&suspmask); /* sleep waiting for any signal */ | |
unslept = alarm(0); /* stop alarm and read seconds left */ | |
sigaction(SIGALRM, &oldact, NULL); | |
sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
return(unslept); | |
} | |
int | |
main(void) | |
{ | |
printf("Sleep 2 seconds\n"); | |
my_sleep(2); | |
printf("Sleep 1 second\n"); | |
my_sleep(1); | |
printf("Sleep 1 second\n"); | |
my_sleep(1); | |
printf("Sleep 1 second\n"); | |
my_sleep(1); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment