clock_gettime with CLOCK_MONOTONIC shows non-monotonic behaviour (14.04,18.04) [on hold]












0















I have an issue using the clock_gettime() system-call with theCLOCK_MONOTONIC clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:



uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty


I am running the following code which creates a new pthread. This newly created pthread enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...) system call:



#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>

timespec tik;
timespec tok;

void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}

int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){

}
return 0;
}


Compiling this code with g++ typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*):



10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed


A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec) actually becomes negative which doesn't make sense since tok is always set after tik. Does anyone have an idea what the issue could be here?



Thanks!



Addition 1: I just tested the same code on 18.04 and the same issue occurred.










share|improve this question















put on hold as off-topic by karel, Charles Green, Zanna, Eric Carvalho, Pilot6 Jan 30 at 18:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – karel, Charles Green, Zanna, Eric Carvalho, Pilot6

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

    – user3629249
    Jan 16 at 21:15











  • in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

    – user3629249
    Jan 16 at 21:20











  • the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

    – user3629249
    Jan 16 at 21:22











  • the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

    – user3629249
    Jan 16 at 21:26











  • regarding: timespec tok; and similar statements: those statements should start with: struct timespec

    – user3629249
    Jan 16 at 21:28
















0















I have an issue using the clock_gettime() system-call with theCLOCK_MONOTONIC clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:



uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty


I am running the following code which creates a new pthread. This newly created pthread enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...) system call:



#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>

timespec tik;
timespec tok;

void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}

int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){

}
return 0;
}


Compiling this code with g++ typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*):



10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed


A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec) actually becomes negative which doesn't make sense since tok is always set after tik. Does anyone have an idea what the issue could be here?



Thanks!



Addition 1: I just tested the same code on 18.04 and the same issue occurred.










share|improve this question















put on hold as off-topic by karel, Charles Green, Zanna, Eric Carvalho, Pilot6 Jan 30 at 18:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – karel, Charles Green, Zanna, Eric Carvalho, Pilot6

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

    – user3629249
    Jan 16 at 21:15











  • in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

    – user3629249
    Jan 16 at 21:20











  • the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

    – user3629249
    Jan 16 at 21:22











  • the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

    – user3629249
    Jan 16 at 21:26











  • regarding: timespec tok; and similar statements: those statements should start with: struct timespec

    – user3629249
    Jan 16 at 21:28














0












0








0








I have an issue using the clock_gettime() system-call with theCLOCK_MONOTONIC clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:



uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty


I am running the following code which creates a new pthread. This newly created pthread enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...) system call:



#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>

timespec tik;
timespec tok;

void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}

int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){

}
return 0;
}


Compiling this code with g++ typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*):



10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed


A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec) actually becomes negative which doesn't make sense since tok is always set after tik. Does anyone have an idea what the issue could be here?



Thanks!



Addition 1: I just tested the same code on 18.04 and the same issue occurred.










share|improve this question
















I have an issue using the clock_gettime() system-call with theCLOCK_MONOTONIC clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:



uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty


I am running the following code which creates a new pthread. This newly created pthread enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...) system call:



#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>

timespec tik;
timespec tok;

void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}

int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){

}
return 0;
}


Compiling this code with g++ typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*):



10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed


A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec) actually becomes negative which doesn't make sense since tok is always set after tik. Does anyone have an idea what the issue could be here?



Thanks!



Addition 1: I just tested the same code on 18.04 and the same issue occurred.







c clock realtime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 20:13







Mantabit

















asked Jan 15 at 19:30









MantabitMantabit

63




63




put on hold as off-topic by karel, Charles Green, Zanna, Eric Carvalho, Pilot6 Jan 30 at 18:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – karel, Charles Green, Zanna, Eric Carvalho, Pilot6

If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by karel, Charles Green, Zanna, Eric Carvalho, Pilot6 Jan 30 at 18:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – karel, Charles Green, Zanna, Eric Carvalho, Pilot6

If this question can be reworded to fit the rules in the help center, please edit the question.













  • the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

    – user3629249
    Jan 16 at 21:15











  • in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

    – user3629249
    Jan 16 at 21:20











  • the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

    – user3629249
    Jan 16 at 21:22











  • the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

    – user3629249
    Jan 16 at 21:26











  • regarding: timespec tok; and similar statements: those statements should start with: struct timespec

    – user3629249
    Jan 16 at 21:28



















  • the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

    – user3629249
    Jan 16 at 21:15











  • in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

    – user3629249
    Jan 16 at 21:20











  • the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

    – user3629249
    Jan 16 at 21:22











  • the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

    – user3629249
    Jan 16 at 21:26











  • regarding: timespec tok; and similar statements: those statements should start with: struct timespec

    – user3629249
    Jan 16 at 21:28

















the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

– user3629249
Jan 16 at 21:15





the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using: setitimer() as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'

– user3629249
Jan 16 at 21:15













in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

– user3629249
Jan 16 at 21:20





in the thread function, the statement: return NULL; would be better written as: pthread_exit( NULL );

– user3629249
Jan 16 at 21:20













the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

– user3629249
Jan 16 at 21:22





the the main() function, rather than a forever loop, suggest pthread_join( loopthread, NULL );

– user3629249
Jan 16 at 21:22













the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

– user3629249
Jan 16 at 21:26





the posted code is missing the statement: #include <stdbool.h> which defines bool, true, false

– user3629249
Jan 16 at 21:26













regarding: timespec tok; and similar statements: those statements should start with: struct timespec

– user3629249
Jan 16 at 21:28





regarding: timespec tok; and similar statements: those statements should start with: struct timespec

– user3629249
Jan 16 at 21:28










1 Answer
1






active

oldest

votes


















0














Ok, the solution to this is pretty trivial, one simply needs to replace



tok.tv_nsec-tik.tv_nsec


with



(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)


since the struct timespec which is set by clock_gettime(...) has two members, one to store the nanoseconds (tv_nsec) and one to store the seconds (tv_sec).






share|improve this answer
























  • overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

    – user3629249
    Jan 16 at 21:17











  • Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

    – Mantabit
    Jan 16 at 21:34


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Ok, the solution to this is pretty trivial, one simply needs to replace



tok.tv_nsec-tik.tv_nsec


with



(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)


since the struct timespec which is set by clock_gettime(...) has two members, one to store the nanoseconds (tv_nsec) and one to store the seconds (tv_sec).






share|improve this answer
























  • overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

    – user3629249
    Jan 16 at 21:17











  • Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

    – Mantabit
    Jan 16 at 21:34
















0














Ok, the solution to this is pretty trivial, one simply needs to replace



tok.tv_nsec-tik.tv_nsec


with



(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)


since the struct timespec which is set by clock_gettime(...) has two members, one to store the nanoseconds (tv_nsec) and one to store the seconds (tv_sec).






share|improve this answer
























  • overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

    – user3629249
    Jan 16 at 21:17











  • Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

    – Mantabit
    Jan 16 at 21:34














0












0








0







Ok, the solution to this is pretty trivial, one simply needs to replace



tok.tv_nsec-tik.tv_nsec


with



(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)


since the struct timespec which is set by clock_gettime(...) has two members, one to store the nanoseconds (tv_nsec) and one to store the seconds (tv_sec).






share|improve this answer













Ok, the solution to this is pretty trivial, one simply needs to replace



tok.tv_nsec-tik.tv_nsec


with



(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)


since the struct timespec which is set by clock_gettime(...) has two members, one to store the nanoseconds (tv_nsec) and one to store the seconds (tv_sec).







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 15 at 21:05









MantabitMantabit

63




63













  • overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

    – user3629249
    Jan 16 at 21:17











  • Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

    – Mantabit
    Jan 16 at 21:34



















  • overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

    – user3629249
    Jan 16 at 21:17











  • Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

    – Mantabit
    Jan 16 at 21:34

















overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

– user3629249
Jan 16 at 21:17





overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.

– user3629249
Jan 16 at 21:17













Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

– Mantabit
Jan 16 at 21:34





Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?

– Mantabit
Jan 16 at 21:34



Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕