How does linux process scheduling happen?

11-17-2023

Designed for programming, automatic code writing robot, free to open. This article introduces the knowledge about how linux process scheduling happens. In the operation of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

What happens in the process scheduling of linux: 1. At the moment of process state transition, the process terminates and sleeps, and the process calls functions such as sleep () or exit () for state transition, and these functions will actively call the scheduler for process scheduling; 2. when the current-> counter of the current process is 0, the time slice of the process is updated by clock interrupt; 3. When the device driver performs a long and repetitive task, directly call the scheduler; 4. When the process returns to user mode from interrupts, exceptions and system calls.

Operating environment of this tutorial: linux7.3 system and Dell G3 computer.

When does the process scheduling of linux happen? The Scheduler of Linux is a function called schedule (), which decides whether to switch processes. The so-called scheduling opportunity is under what circumstances the scheduler is executed.

Linux process scheduling adopts preemptive multi-tasking, so there is no need for cooperation between processes to suspend and continue running.

There are mainly the following situations:

The time of process state transition: process termination, process sleep;

Processes need to call functions such as sleep () or exit () for state transition, and these functions will actively call the scheduler for process scheduling;

When the time slice of the current process runs out (current-> counter = 0);

Because the time slice of the process is updated by clock interrupt, this situation is the same as opportunity 4.

device driver

When the device driver performs a long and repetitive task, the scheduler is directly called. In each iteration, the driver checks the value of need_resched, and if necessary, calls the scheduler schedule () to give up the CPU actively.

When the process returns to user mode from interrupt, exception and system call;

As mentioned above, ret_from_sys_call () will be called whether it is returned from an interrupt, an exception or a system call, and this function will detect the scheduling flag, and if necessary, call the scheduler.

Extended knowledge

In Linux, the running time of processes can't exceed the time slice allocated to them. They use preemptive multitasking, so the suspension and continuation of processes don't need cooperation with each other.

In a multitasking system such as linux, multiple programs may compete for the same resource. In this case, we think that it is better to perform short-term sudden work and suspend running to wait for input than to continuously occupy the processor for calculation or constantly poll the system to see if any input has arrived. We call a good program a nice program, and in a sense, this nice program can be calculated. The operating system determines the priority of a process according to its nice value. The nice value of a process defaults to 0 and will change constantly according to the performance of this program. Programs that run continuously for a long time generally have a lower priority.

Why call the scheduler when returning from a system call?

This is of course from the perspective of efficiency. Returning from the system call means leaving the kernel mode and returning to the user mode, and the state transition takes some time. Therefore, before returning to the user mode, the system should finish all the things that should be handled in the kernel mode.

Let's take a brief look at what the kernel has to do when each clock interrupt occurs. First, we have a general understanding of this most frequent scheduling opportunity, and then we will discuss the specific working process of the scheduler in detail.

When each timer interrupt occurs, three functions work together to complete the process selection and switching. They are: schedule (), do_timer () and ret_form_sys_call ().

Schedule (): process scheduling function, which is used to complete process selection (scheduling);

Do_timer (): Call it a clock function for the time being. This function is called in the clock interrupt service program and is the main component of the clock interrupt service program. The frequency of this function is the frequency of clock interrupt, that is, 100 times per second (100 Hz or 100 Hz for short);

Ret_from_sys_call (): The system calls the return function.

When a system call or interrupt is completed, this function is called to handle some finishing work, such as signal processing, core tasks and so on.

That's all for how linux process scheduling happened. Thank you for reading. If you want to know more about the industry, you can pay attention to the Yisu Cloud website, and Xiaobian will output more high-quality practical articles for everyone!

Ask AI for details

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us