counter=0", the time slice of the process is updated by the clock interrupt; 3. When the device driver executes a long and repetitive task, the scheduler is called directly; 4 , when the process returns to user mode from interruption, exception and system call."/> counter=0", the time slice of the process is updated by the clock interrupt; 3. When the device driver executes a long and repetitive task, the scheduler is called directly; 4 , when the process returns to user mode from interruption, exception and system call.">

Home>Article>Operation and Maintenance> Under what circumstances does Linux process scheduling occur?

Under what circumstances does Linux process scheduling occur?

WBOY
WBOY Original
2022-07-27 15:32:32 2676browse

What happens in process scheduling in Linux: 1. At the moment when the process state changes, the process terminates, the process sleeps, and the process needs to call functions such as "sleep()" or "exit()" to perform state transitions. These functions The scheduler will be actively called for process scheduling; 2. When "current->counter=0" of the current process, the time slice of the process is updated by the clock interrupt; 3. When the device driver executes a long and repetitive task , directly calling the scheduler; 4. When the process returns to user mode from interrupts, exceptions and system calls.

Under what circumstances does Linux process scheduling occur?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Under what circumstances does Linux process scheduling occur?

The Linux scheduler is a function called Schedule(), which determines whether to switch processes. The so-called scheduling timing refers to the circumstances under which the scheduler is executed.

Linux process scheduling uses preemptive multitasking, so the suspension and continued running of processes do not require cooperation between each other.

There are several main situations:

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

The process needs to call functions such as sleep() or exit() for state transition. These functions will actively call the scheduler for process scheduling;

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

Since the time slice of the process is updated by the clock interrupt, this situation and timing 4 is the same.

  • Device Driver

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

  • When the process returns to user mode from interrupts, exceptions and system calls;

As mentioned above, no matter Whether it is returning from an interrupt, exception or system call, ret_from_sys_call() will eventually be called. This function will detect the scheduling flag and, if necessary, call the scheduler.

Extended knowledge

In Linux, processes cannot run longer than the time slice assigned to them. They use preemptive multitasking. , so processes can suspend and continue running without any cooperation between them.

In a multitasking system such as Linux, multiple programs may compete to use the same resource. In this case, we believe that performing short-term bursts of work and pausing to wait for input is better than a program that continuously occupies the processor for calculations or constantly polls the system to see if any input has arrived. We call programs that perform well as nice programs, and in a sense, this niceness can be calculated. The operating system determines the priority of a process based on its nice value. The nice value of a process defaults to 0 and will continue to change based on the performance of the program. Programs that run continuously for a long time generally have a lower priority.

Why do we need to call the scheduler when returning from a system call?

This is of course based on efficiency considerations. Returning from a system call means leaving the kernel state and returning to the user state, and the state transition takes a certain amount of time. Therefore, before returning to the user state, the system has completed all the things that should be processed in the kernel state.

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

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

schedule(): process scheduling function, which completes process selection (scheduling);

do_timer(): temporarily called the clock function, this function is in the clock interrupt service program Called, it is the main component of the clock interrupt service routine. The frequency at which this function is called is the frequency of the clock interrupt, which is 100 times per second (referred to as 100 Hz or 100Hz);

ret_from_sys_call(): system call 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, etc.

Recommended learning:Linux video tutorial

The above is the detailed content of Under what circumstances does Linux process scheduling occur?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn