123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <signal.h>
- #include <errno.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/wait.h>
- #define MS 1000000
- #define SIG_TIMER SIGRTMIN
- void sig_TIMER_handler(int signo, siginfo_t *info, void *context)
- {
- printf("timer went off and i don't know what to do\n");
- }
- int main(int argc, char *argv[]){
- time_t cur_time;
- struct tm *cur_t;
- struct sigevent sev;
- struct itimerspec its;
- timer_t timerid;
- sigset_t sigset;
- struct sigaction sa;
- sev.sigev_notify = SIGEV_SIGNAL;
- sev.sigev_signo = SIG_TIMER;
- sev.sigev_value.sival_ptr = &timerid;
- if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
- sa.sa_flags = SA_RESTART | SA_SIGINFO;
- sigemptyset(&sigset);
-
- sa.sa_mask = sigset;
- sa.sa_sigaction = sig_TIMER_handler;
- if (sigaction(SIG_TIMER, &sa, NULL) < 0) {
- perror("sigaction: SIG_TIMER");
- exit(1);
- }
- sigaddset(&sigset, SIG_TIMER);
-
- if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0) {
- perror("signals_enable: sigprocmask");
- exit(1);
- }
- its.it_interval.tv_sec = 0;
- its.it_interval.tv_nsec = 0;
- its.it_value.tv_sec = 0;
- its.it_value.tv_nsec = 10 * MS;
- if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
- pause();
- printf("So far so good\n");
- if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
- pause();
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- printf("[%d:%d:%d]: Everything went well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
-
- return 0;
- }
|