Subj : NPTL, linuxthreads, and posix periodic timers (time shift) To : comp.programming.threads From : =?ISO-8859-15?Q?st=E9phane_SARAGAGLIA?= Date : Tue Jan 04 2005 03:03 pm Hi all, I am encountering troubles using posix timers with NPTL (debian sarge, 2.6.8-1-686 kernel, libc-2.3.2.so, gcc 3.3.4). I have already posted this message on comp.os.linux.development.system and comp.os.linux.development.applications without success. I think this list is more suitable, sorry for those who read all these lists... By testing accuracy of timers, I am using a piece of code wich prints the time when a timer signal is received : -------------------------- SOURCE BEGIN ----------------------- #define POSIX_SOURCE 1 #include #include #include #include #include static volatile sig_atomic_t timer_tick = 0; void sigalarm(int signo) { struct timeval time0; struct tm *ma_date; gettimeofday(&time0, NULL); ma_date = localtime( &time0.tv_sec ); printf("%02d:%02d:%02d.%03d ", ma_date->tm_hour, ma_date->tm_min, ma_date->tm_sec, (unsigned)(time0.tv_usec/1000)); } int main(int argc, char *argv[]) { clock_t clock = CLOCK_REALTIME; int signum = SIGRTMAX; int microsec = 100000;usec int status; struct timeval time0, time1; double dt; timer_t timer_id; struct itimerspec ts; struct sigevent se; struct sigaction act; sigset_t sigmask; /* Command-line arguments: */ if (argc >= 2) microsec = atol(argv[1]); /* Set up signal handler: */ sigfillset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = sigalarm; sigaction(signum, &act, NULL); /* Set up timer: */ memset(&se, 0, sizeof(se)); se.sigev_notify = SIGEV_SIGNAL; se.sigev_signo = signum; se.sigev_value.sival_int = 0; if (timer_create(clock, &se, &timer_id) < 0) { perror("timer_create"); return EXIT_FAILURE; } /* Start timer: */ ts.it_interval.tv_sec = microsec / 1000000; ts.it_interval.tv_nsec = (microsec % 1000000) * 1000; ts.it_value = ts.it_interval; if (timer_settime(timer_id, 0, &ts, NULL) < 0) { perror("timer_settime"); return EXIT_FAILURE; } while (1){ sleep(10); } timer_delete(timer_id); return EXIT_SUCCESS; } -------------------------- SOURCE END ----------------------- When linuxthreads is selected with (setenv LD_ASSUME_KERNEL 2.4.27), the display I obtain is (DISPLAY1) : -------------------------- DISPLAY1 BEGIN ----------------------- 1) date :17:25:10.662 2) date :17:25:10.762 3) date :17:25:10.863 <-- .001s slow from prev tick : error accepted 4) date :17:25:10.963 5) date :17:25:11.063 6) date :17:25:11.163 7) date :17:25:11.263 8) date :17:25:11.363 9) date :17:25:11.462 <-- .001s fast from prev tick : error accepted 10) date :17:25:11.562 -------------------------- DISPLAY1 END ----------------------- Error from 1) to 10) is something like .001 seconds aswell : error accepted. I have performed several tests with 2.4 kernel, and error from start tick seems to always be consistent whatever the timer duration : something equivalent to .001s. When NPTL is selected with (setenv LD_ASSUME_KERNEL "" (default)), the display I obtain is (DISPLAY2) : -------------------------- DISPLAY2 BEGIN ----------------------- 1) date :17:28:14.203 2) date :17:28:14.304 <-- .001s slow from prev tick : error accepted 3) date :17:28:14.405 <-- .001s slow from prev tick : error accepted 4) date :17:28:14.506 ... 5) date :17:28:14.607 6) date :17:28:14.708 7) date :17:28:14.809 8) date :17:28:14.910 9) date :17:28:15.011 10) date :17:28:15.112 -------------------------- DISPLAY2 END ----------------------- Error from 1) to 10) is something like .009, and will be more and more consequential with time : error not accepted. I would like to use NPTL, but the results displayed by DISPLAY2 are alarming me... I have tried using 2.6.7 and 2.6.9 kernels, and I obtain the same results... Why such a difference between 2.6 and 2.4 kernel ? Is there something wrong with my version of NPTL ? Are posix timers not suitable to my needs ? Is there something wrong with my source ? Is there a problem on librt.so library ? Regards .