Page MenuHomePhabricator
Paste P74554

timer-stress.c
ActivePublic

Authored by tstarling on Apr 2 2025, 4:35 AM.
Tags
None
Referenced Files
F58963620: timer-stress.c
Apr 2 2025, 4:50 AM
F58963598: timer-stress.c
Apr 2 2025, 4:35 AM
Subscribers
None
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void on_good_timer(union sigval sv) {
}
void on_bad_timer(union sigval sv) {
long t = time(NULL);
long delta = t - (long)sv.sival_ptr;
char message[100];
snprintf(message, sizeof(message), "Timer expired after %ld seconds\n", delta);
write(STDERR_FILENO, message, strlen(message));
abort();
}
int main(int argc, char** argv) {
timer_t id;
struct itimerspec its_long = {.it_value = {.tv_sec = 180}};
struct itimerspec its_short = {.it_value = {.tv_nsec = 1000}};
struct itimerspec its_zero = {.it_interval = {.tv_sec = 0}};
struct sigevent ev_short = {
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = on_good_timer,
};
struct sigevent ev_long = {
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = on_bad_timer,
};
for (int which = 0; ; which = !which) {
struct sigevent * ev = which ? &ev_short : &ev_long;
struct itimerspec * its = which ? &its_short : &its_long;
ev->sigev_value.sival_ptr = (void*)time(NULL);
if (timer_create(CLOCK_MONOTONIC, ev, &id)) {
perror("timer_create");
continue;
}
if (timer_settime(id, 0, its, NULL)) {
perror("timer_settime(x)");
goto cleanup;
}
if (timer_settime(id, 0, &its_zero, NULL)) {
perror("timer_settime(0)");
}
cleanup:
if (timer_delete(id)) {
perror("timer_delete");
}
}
}