StudyingUnixInterface/10/signal/sleep.c

39 lines
719 B
C
Raw Permalink Normal View History

2021-01-11 10:00:36 +00:00
#include <unistd.h>
#include <signal.h>
static void sig_alrm(int signo) {
}
unsigned int sleep(unsigned int seconds) {
struct sigaction newact, oldact;
sigset_t newmask, oldmask, suspmask;
unsigned int unslept;
newact.sa_handler = sig_alrm;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGALRM, &newact, &oldact);
sigemptyset(&newmask);
sigaddset(&newmask, SIGALRM);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
alarm(seconds);
suspmask = oldmask;
sigdelset(&suspmask, SIGALRM);
sigsuspend(&suspmask);
unslept = alarm(0);
sigaction(SIGALRM, &oldact, NULL);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return unslept;
}
int main(void) {
return sleep(5);
}