Detailed analysis of Linux Process Scheduling time

Source: Internet
Author: User

Linux schedules many processes. This involves the concept of Linux Process Scheduling timing, which is determined by the Schedule function in the Linux kernel, if you want to switch, which process to switch to, and so on.

Linux Process Scheduling time mainly includes::

1. process state transition time: Process Termination and process sleep;

2. When the time slice of the current process is used up, current-> counter = 0 );

3. Device Drivers

4. When a process is returned to the user State from interruption, exception, or system call;

Time 1: The process must call sleep) or exit functions to convert the status. These functions will call the scheduler to schedule the process;

Timing 2: because the time slice of the process is updated by clock interruption, this situation is the same as timing 4.

Timing 3: when the device driver executes a long and repetitive task, it directly calls the scheduler. In each loop, the driver checks the value of need_resched. If necessary, the scheduler schedule () is called to discard the CPU.

Time 4, as mentioned earlier, whether it is returned from an interrupt, exception, or system call, the system will eventually call ret_from_sys_call). This function is used to detect the scheduling flag. If necessary, call the scheduler. So, why do we need to call the scheduler when the system calls the response? Of course, this is from the perspective of efficiency. Returning from a system call means returning from the kernel state to the user State, while switching the State takes some time. Therefore, before returning to the user State, the system completes all the tasks in the kernel state.

We will not discuss the timing of executing the scheduling program directly, because we will describe the working process of the scheduling program later. We have discussed the clock interruption and learned the important role of the clock interruption. Next we will take a brief look at what the kernel will do when every clock interruption occurs, first, I have a general understanding of the most frequent scheduling time, and then I will discuss in detail the specific working process of the scheduling program.

When every clock interrupt timer interrupt) occurs, three functions work together to select and switch processes. They are schedule, do_timer, and ret_form_sys_call ). Let's first explain the three functions:

Schedule): A process scheduling function that completes the process selection and scheduling );

Do_timer): currently called a clock function, which is called in the clock interrupt service program and is the main component of the clock interrupt service program, the call frequency of this function is the clock Interruption Frequency, that is, 100 times per second (100Hz or 100Hz );

Ret_from_sys_call): The system calls the return function. When a system call or interruption is completed, the function is called to process some final tasks, such as signal processing and core tasks.

How are these three functions coordinated?

As we can see above, the clock interrupt is an interrupt service program, and its main component is the clock function do_timer). This function is used to update the system time and process time slice, the updated process time slice counter serves as the main basis for scheduling.

Call the ret_from_sys_call function when the clock is interrupted. We have discussed this function before. The function contains the following lines:

Cmpl $0, _ need_resched

Jne reschedule

......

Restore_all:

RESTORE_ALL


Reschedule:

Call SYMBOL_NAME (schedule)

Jmp ret_from_sys_call


The meaning of these lines is obvious: Check the need_resched flag. If this flag is not 0, go to reschedule to call the scheduler schedule) to select the process. Scheduler schedule) selects the next process in the running Queue according to the specific standards. When the return result is from the scheduler, if another scheduling flag is found to be set, the scheduler is called again until the scheduling flag is 0, restore the environment of the selected process from RESTORE_ALL when the scheduler returns, and return to the user space of the selected process for running.

The above is the most frequent scheduling time of clock interruption. The main purpose of this discussion is to give readers a general understanding of timing 4.

Finally, it should be noted that the system call return function ret_from_sys_call) is the function that is usually called from the system call, exception, and interrupt return function, but it does not have to be called, for the interrupt request signals that need to be frequently responded and processed as soon as possible, in order to reduce the system overhead, do not call ret_from_sys_call after the processing is complete.) Obviously, the user space returned from these interrupt handlers must be the interrupted process, and there is no need to re-select), and they should do as little work as possible, because the response frequency is too high.

Linux Process Scheduling is different from other UNIX process scheduling, especially in the process of "nice level" priority, which is the first process to run with priority scheduling priority, linux uses time slice rotation scheduling Round Robing), but at the same time ensures that high-priority processes run fast, long time both sooner and longer ). Standard UNIX scheduling programs use multi-level process queues. Most implementations use the second-level priority queue: a standard queue and a real-time "real time" queue. In general, if the processes in the real-time queue are not blocked, they must be executed before the processes in the standard queue, and in each queue, the "nice level" process is first executed.

In general, the Linux scheduling process performs very well in terms of interactivity. Of course, this is at the cost of sacrificing some "throughput.

  1. Differences between Linux and Unix
  2. Detailed analysis of Linux folder execution Permissions
  3. How to check Linux Security Vulnerabilities
  4. Describes how to use the Linux man command in detail.
  5. Linux bash shortcut keys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.