3

I have a very simple code running on linux, as shown below:

struct timespec SysTime_Test;
#define BILLION  1000000000L
SysTime_Test.tv_sec = 0;
SysTime_Test.tv_nsec = 0;
clock_settime(CLOCK_REALTIME,&SysTime_Test);
while(1) {
    printf("%d ",clock_gettime(CLOCK_REALTIME,&SysTime_Test));
    printf("%llu, %llu\n",SysTime_Test.tv_sec,  SysTime_Test.tv_nsec);
    fflush(stdout);
}

And I am expecting to start with:

0 0, 0

instead I got:

0 97508642543791, 13184898790915571716
0 152071907084879, 13184898790915571716
0 184202557433736, 13184898790915571716
0 214744069882703, 13184898790915571716
            ............
0 688307164049923, 13184898790915571716
0 715983933311791, 13184898790915571716
0 743664997540956, 13184898790915571716
0 770706111642868, 13184898790915571716
0 798223967114747, 13184898790915571716
0 825746117553923, 13184898790915571716
0 853263973025802, 13184898790915571716
0 881898519994901, 13184898790915571716
0 909098547886802, 13184898790915571716
0 936779612115967, 13184898790915571716
0 964138553797857, 13184898790915571716
0 992455273186978, 13184898790915571716
0 1019973128658857, 13184898790915571716
0 1048130934257989, 13184898790915571716
              .......... 
0 1318713873968989, 13184898790915571716
0 1346549557020846, 13184898790915571716
0 1374707362619978, 13184898790915571716
0 1402225218091857, 13184898790915571716
0 1430700851270967, 13184898790915571716
               ..........
0 2643470766924857, 13184898790915571716
0 2670988622396736, 13184898790915571716
0 2698828600415890, 13184898790915571716
0 2726187542097780, 13184898790915571716
0 2754027520116934, 13184898790915571716
0 2781704289378802, 13184898790915571716
0 2809544267397956, 13184898790915571716
0 2836903209079846, 13184898790915571716    (Big jump)
0 1434656352942746714, 13184898790915571716
0 1434694848734629725, 13184898790915571716
            ..................
0 1435769544041619659, 13184898790915571716
0 1435796907278268846, 13184898790915571716
0 1435824266219950736, 13184898790915571716

I have tried with different clock ID and I still got almost the same thing, with a big jump in between, what have I done wrong?

Leonardo Alves Machado
  • 2,481
  • 7
  • 31
  • 43
Henry
  • 141
  • 8
  • 1
    And what did `clock_settime` return?! – Antti Haapala Oct 06 '17 at 10:49
  • Also, is this the **whole** program? Did you include `stdio.h`? Did you get warnings about wrong arguments to `printf` did you compile the program correctly? – Antti Haapala Oct 06 '17 at 10:54
  • I.e. this *isn't* a [mcve] – Antti Haapala Oct 06 '17 at 10:54
  • no warning from printf, i did include stdio.h, and clock_settime returns 0 – Henry Oct 06 '17 at 10:57
  • then make this into complete code! also please compile with `-Wall -Wextra -Werror` – Antti Haapala Oct 06 '17 at 11:00
  • Note that `tv_nsec` never changes. That should be a clue that you're doing something wrong... – Andrew Henle Oct 06 '17 at 11:12
  • As Antti Haapala's answer says, suspect your problem is that `%llu` should be `%lu` for your platform. Also suggest you use compiler options that would warn you about this sort of problem next time. Also suggest reading [this question](https://stackoverflow.com/questions/2125845) or [this question](https://stackoverflow.com/questions/46569942) for additional suggestions. (Sadly there is no platform-independent size modifier for `time_t`.) – Steve Summit Oct 06 '17 at 12:02
  • Also the reason for not starting at `0 0` is probably that your call to `clock_settime` failed, probably because you weren't running with permission to set the time. Check `clock_settime`'s return value. – Steve Summit Oct 06 '17 at 12:08

2 Answers2

2

Check function results.

Code had unexpected output, yet lacks a simple check to see if setting the time worked @Steve Summit. I'd expect this as an early debug step.

// clock_settime(CLOCK_REALTIME,&SysTime_Test);
if (clock_settime(CLOCK_REALTIME,&SysTime_Test)) {
  puts("Fail Set");
}

Use correct *printf() specifiers

Member tv_nsec is a long. Print with "%ld". @Antti Haapala

printf("%ld", SysTime_Test.tv_nsec);

tv_sec is a time_t. On Linux systems that must be some integer type @Andrew Henle. Cast to the widest (or at least a wide) type and print.

printf("%jd", (intmax_t) SysTime_Test.tv_sec);
 // or
printf("%lld", (long long) SysTime_Test.tv_sec);

Save time

Turn on all compiler warnings to detect issues quicker.

Be clear

"clock_gettime returns some very large value" --> not really. The function returned 0 every time as expected and printed. It was the values in SysTime_Test the appear unexpected certainly because they were not printed properly.

chux - Reinstate Monica
  • 113,725
  • 11
  • 107
  • 213
1

Again, the question lacks a Minimal, Complete, and Verifiable example, but from the behaviour it can be inferred that the asker is using a 32-bit platform, and not using the proper format character, therefore leading to undefined behaviour.

Both tv_sec and tv_nsec are 64 bits on my computer, but the typedefs are aliases for signed long ints, therefore they should be printed using %ld! On your computer these would perhaps be pushed on stack / registers as 32-bit values, but two 64 bit values be retrieved in printf: first one would consist of the tv_nsec / tv_sec and the second one of the bytes of the pointer to the format string and the stored instruction pointer / function return address.

Even better solution, as we're on POSIX, is to add a cast to both, for example:

printf("%lld", (long long)SysTime_Test.tv_sec);
printf("%lld", (long long)SysTime_Test.tv_nsec);
Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
  • 3
    Pedantically, per [the POSIX standard](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html), `tv_sec` is a `time_t`. Only `tv_nsec` is a `[signed] long [int]`. `time_t` merely must be ["an integer type"](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html). Still, that's more than enough to break things. UB is UB. – Andrew Henle Oct 06 '17 at 11:11
  • @AndrewHenle yeah I meant that the typedefs define both as `long int`s. – Antti Haapala Oct 06 '17 at 13:53