How does sleep() work?

后端 未结 3 1938
猫巷女王i
猫巷女王i 2020-12-14 17:57

This might be a stupid question, but how do sleep(), wait(), pause(), functions work?

3条回答
  •  萌比男神i
    2020-12-14 18:13

    Modern operating systems are multitasking, which means it appears to run multiple programs simultaneously. In fact, your computer only (traditionally, at least) only has one CPU, so it can only execute one instruction from one program at the same time.

    The way the OS makes it appear that multiple stuff (you're browsing the web, listening to music and downloading files) is happening at once is by executing each task for a very short time (let's say 10 ms). This fast switching makes it appear that stuff is happening simultaneously when everything is in fact happening sequentially. (with obvious differences for multi-core system).

    As for the answer to the question: with sleep or wait or synchronous IO, the program is basically telling the OS to execute other tasks, and do not run me again until: X ms has elapsed, the event has been signaled, or the data is ready.

提交回复
热议问题