1
0

fix(lab03): max value of nanosecond is 1s

Time is splited in second and nanosecond
This commit is contained in:
2026-04-24 12:42:53 +00:00
parent ca8085ce09
commit 5592c9c0fe

View File

@@ -111,18 +111,22 @@ static void* timer_thread(void* arg) {
return NULL; return NULL;
} }
static void configure_timer(int* timer_fd, long period_ns) { static void configure_timer(int* timer_fd, long period_ms) {
// https://www.man7.org/linux/man-pages/man3/itimerspec.3type.html // https://www.man7.org/linux/man-pages/man3/itimerspec.3type.html
struct itimerspec its;
static struct itimerspec its;
// Periodic interval // Periodic interval
its.it_interval.tv_sec = 0; its.it_interval.tv_sec = period_ms / 1000;
its.it_interval.tv_nsec = period_ns; its.it_interval.tv_nsec = (period_ms % 1000) * 1000000;
// Initial expiration with same value as periodic interval // Initial expiration with same value as periodic interval
its.it_value.tv_sec = 0; its.it_value.tv_sec = period_ms / 1000;
its.it_value.tv_nsec = period_ns; its.it_value.tv_nsec = (period_ms % 1000) * 1000000;
if (timerfd_settime(*timer_fd, 0, &its, NULL) == -1) { if (timerfd_settime(*timer_fd, 0, &its, NULL) == -1) {
perror("timerfd_settime failed"); perror("timerfd_settime failed");
return 1; exit(1);
} }
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@@ -136,13 +140,13 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
configure_timer(&data.timer_fd, DEFAULT_TIME_MS * 1000000); configure_timer(&data.timer_fd, DEFAULT_TIME_MS);
if (pthread_create(&thread, NULL, timer_thread, &data) != 0) { if (pthread_create(&thread, NULL, timer_thread, &data) != 0) {
fprintf(stderr, "Failed to create timer thread\n"); perror("Failed to create timer thread");
return 1; exit(1);
} }
pthread_join(thread, NULL); pthread_join(thread, NULL);