What's the algorithm behind sleep()?

后端 未结 8 1925
灰色年华
灰色年华 2020-12-07 16:36

Now there\'s something I always wondered: how is sleep() implemented ?

If it is all about using an API from the OS, then how is the API made ?

Does it all bo

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 17:12

    I don't know anything about Linux, but I can tell you what happens on Windows.

    Sleep() causes the process' time-slice to end immediately to return control to the OS. The OS then sets up a timer kernel object that gets signaled after the time elapses. The OS will then not give that process any more time until the kernel object gets signaled. Even then, if other processes have higher or equal priority, it may still wait a little while before letting the process continue.

    Special CPU machine code is used by the OS to do process switching. Those functions cannot be accessed by user-mode code, so they are accessed strictly by API calls into the OS.

提交回复
热议问题