|
|
@@ -6,6 +6,7 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <err.h>
|
|
|
+#include <sys/file.h>
|
|
|
|
|
|
/*
|
|
|
* signal and timer example
|
|
|
@@ -35,65 +36,68 @@ struct entry {
|
|
|
|
|
|
static int spankme;
|
|
|
|
|
|
-void
|
|
|
+ void
|
|
|
sigHandler(int signo, siginfo_t * si, void * /* ucontext_t */ uap)
|
|
|
{
|
|
|
switch (signo) {
|
|
|
- case SIGALRM:
|
|
|
- /* timer timeout */
|
|
|
- SLIST_FOREACH(np, &head, entries) {
|
|
|
- if (np && np->code == si->si_value.sival_int) {
|
|
|
- printf("%s", np->name);
|
|
|
- fflush(stdout);
|
|
|
- break;
|
|
|
+ case SIGALRM:
|
|
|
+ /* timer timeout */
|
|
|
+ SLIST_FOREACH(np, &head, entries) {
|
|
|
+ if (np && np->code == si->si_value.sival_int) {
|
|
|
+ flock(1, LOCK_EX);
|
|
|
+ printf("%s", np->name);
|
|
|
+ fflush(stdout);
|
|
|
+ flock(1, LOCK_UN);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- if (np == NULL)
|
|
|
- printf("Timeout #%d, unknown.\n", si->si_value.sival_int);
|
|
|
- break;
|
|
|
- case SIGINFO:
|
|
|
- printf("no need to panic.\n");
|
|
|
- break;
|
|
|
- case SIGINT:
|
|
|
- /* exit flag */
|
|
|
- ++spankme;
|
|
|
- break;
|
|
|
- default:
|
|
|
- printf("sig:%di received.", signo);
|
|
|
- break;
|
|
|
+ if (np == NULL)
|
|
|
+ printf("Timeout #%d, unknown.\n", si->si_value.sival_int);
|
|
|
+ break;
|
|
|
+ case SIGINFO:
|
|
|
+ printf("no need to panic.\n");
|
|
|
+ break;
|
|
|
+ case SIGINT:
|
|
|
+ /* exit flag */
|
|
|
+ ++spankme;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ printf("sig:%d received.", signo);
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-int
|
|
|
+ int
|
|
|
main(int argc, char *argv[])
|
|
|
{
|
|
|
SLIST_INIT(&head);
|
|
|
|
|
|
/* timer pointers */
|
|
|
- size_t nb_timers=0;
|
|
|
+ size_t nb_timers = 5;
|
|
|
+
|
|
|
if(argc>1){
|
|
|
- nb_timers=atoi(argv[1]);
|
|
|
+ nb_timers = atoi(argv[1]);
|
|
|
}
|
|
|
|
|
|
if(nb_timers<1){
|
|
|
- nb_timers=5;
|
|
|
+ fprintf(stderr,"Invalid timer number.\n");
|
|
|
+ abort();
|
|
|
}
|
|
|
|
|
|
- timer_t timerid[nb_timers];
|
|
|
- char *names[nb_timers];
|
|
|
-
|
|
|
+ timer_t timerid[nb_timers];
|
|
|
+ char *names[nb_timers];
|
|
|
|
|
|
/* build text to display for each */
|
|
|
- for(size_t i=0;i<nb_timers;++i){
|
|
|
+ for(size_t i = 0; i < nb_timers; ++i) {
|
|
|
|
|
|
- names[i]=malloc(MAX_LEN_NAME);
|
|
|
- if(i==0){
|
|
|
+ names[i] = malloc(MAX_LEN_NAME);
|
|
|
+ if (i == 0) {
|
|
|
strncpy(names[i],".",MAX_LEN_NAME);
|
|
|
continue;
|
|
|
}
|
|
|
- if(MAX_LEN_NAME<=snprintf(names[i],MAX_LEN_NAME,"+%zu+",i*2+1)){
|
|
|
- warnx("string %zu shorten",i);
|
|
|
+ if (MAX_LEN_NAME <= snprintf(names[i], MAX_LEN_NAME, "+%zu+", i * 2 + 1)) {
|
|
|
+ warnx("string %zu shorten", i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -120,10 +124,9 @@ main(int argc, char *argv[])
|
|
|
continue;
|
|
|
}
|
|
|
/* 3 -> TIMER_MAX stored into timerid->oshandle */
|
|
|
- printf("Timer #%d now created at address %p.\n", timer_oshandle_np(timerid[i]),timerid[i]);
|
|
|
np = malloc(sizeof(struct entry)); /* Insert at the head. */
|
|
|
np->code = timer_oshandle_np(timerid[i]);
|
|
|
- np->name=names[i];
|
|
|
+ np->name = names[i];
|
|
|
|
|
|
SLIST_INSERT_HEAD(&head, np, entries);
|
|
|
|
|
|
@@ -137,12 +140,13 @@ main(int argc, char *argv[])
|
|
|
if (timer_settime(timerid[i], 0, &its, NULL)) {
|
|
|
warn("timer_settime failed for %zu", i);
|
|
|
}
|
|
|
+ printf("Timer %s now created at interval %ld.\n", np->name, its.it_interval.tv_sec);
|
|
|
}
|
|
|
|
|
|
printf("waiting...\n");
|
|
|
for (; !spankme;) {
|
|
|
pause();
|
|
|
- } //loop
|
|
|
+ }
|
|
|
|
|
|
printf("\nbye.\n");
|
|
|
for (size_t i = 0; i < nb_timers; ++i) {
|