One article explains - what happens in the operating system when sleep is called?

I believe that no matter what language you use, the sleep function will be called more or less, so do you know what happens in the operating system when this function is called? Let’s briefly talk about this issue today. This is another classic case of software and hardware cooperation.

If we don't have an operating system, then the implementation of the sleep function may be busy waiting, busy wait, that is, simply consuming CPU in a for loop, but with the help of the operating system, we don't need to waste precious CPU resources.

Most operating systems provide the "sleep" system call. When we call sleep in the user mode no matter what language we use, we will eventually call this system call. After the system call, the operating system starts to run. At this time:

1, The operating system suspends the execution of the process (thread) and changes its running state, such as setting it to a waiting state

2. The operating system creates a timer for the process (thread). How does the operating system know the concept of time? It's actually very simple, assuming you don't know anything about time, but let me tell you, there is a cute puppy next to you, it will bark once a second, so that every time you hear the dog bark, you will know Time passes by another second, and at the same time write it down on paper so you know the time.

Back to us, you are equivalent to the operating system, and the puppy is like the hardware in the computer system-timer, timer, the timer will generate an interrupt signal at a fixed frequency, after the interrupt signal is sent, the operating system will take over the computer system and start processing Some time-related things, such as checking whether the time slice of the current process (thread) is exhausted, whether other waiting threads need to be woken up, and so on.

3. The operating system detects that the process (thread) timer time has expired, and turns the process (thread) from the waiting state to the executable state. Note that the process (thread) may not be executed immediately at this time, even if the process ( Thread) is already in the ready state and waits, because there may be a higher priority process in the system at this time, or the time slice of the running process has not been used up.

So we can see that, assuming that the given parameter for calling sleep is 1s, then your process will not be paused for exactly 1s and then run again. The time from calling sleep to running again must not be less than 1s, that is, it may be slightly longer Less than 1s, but certainly not less than 1s.

  Information through train: Linux kernel source code technology learning route + video tutorial kernel source code

Learning through train: Linux kernel source code memory tuning file system process management device driver/network protocol stack

The most common system call related to sleep in the Linux system is nanosleep. Suppose you have a piece of C language code like this:

#include <unistd.h>
void main() {  
  sleep(1);
}

The executable program generated after compiling is called test, then use the strace command under Linux to run the program and you will get:

$ strace test
...
nanosleep({tv_sec=1, tv_nsec=0}, 0

The strace command will display all the system calls called by the program. You can see that the program finally calls the nanosleep system call. Next, let's see what the function of the system call is. The official document:

nanosleep will suspend the execution of the current thread until the time specified by the parameter has passed, or a signal occurs, which triggers the signal handler of the thread or the signal terminates the process.

This is the internal implementation of the operating system when most user-mode languages ​​call sleep.

 

 

Guess you like

Origin blog.csdn.net/youzhangjing_/article/details/129941456