What's the algorithm behind sleep()?

后端 未结 8 1920
灰色年华
灰色年华 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:18

    There's a kernel data structure called the sleep queue. It's a priority queue. Whenever a process is added to the sleep queue, the expiration time of the most-soon-to-be-awakened process is calculated, and a timer is set. At that time, the expired job is taken off the queue and the process resumes execution.

    (amusing trivia: in older unix implementations, there was a queue for processes for which fork() had been called, but for which the child process had not been created. It was of course called the fork queue.)

    HTH!

提交回复
热议问题