What is the effect of changing system time on sleeping threads?

后端 未结 2 1675
不知归路
不知归路 2021-01-12 20:15

If you take a look at the clock_gettime() function, which is available in all BSDs and is actually defined as part of the POSIX standard, you see that there is support for a

2条回答
  •  星月不相逢
    2021-01-12 20:47

    As far as I know, sleep functions are usually implemented more like a decrementing counter. You say "sleep for 10 seconds," that translates into "sleep for 1000 schedule ticks" in the scheduler, and then every time the scheduler checks on sleeping processes it decrements the amount of time left.

    In this way, the sleep time will always be an amount of real time to sleep, as opposed to sleeping until some time in the future. The reason for this is as you've suspected, if we pick a time in the future we might never get there (or might get there in an unexpected amount of time). This is consistent with what you would want to use sleep for in a program. It's not meant to do calendar-like calculations.

    You can also make a simple test, make a program sleep for 30 seconds, use the nix "time" command to time how long the function runs, and after it starts change your system clock back 5 minutes and see what happens.

提交回复
热议问题