Overview of "Linux" process scheduling

Source: Internet
Author: User

                                          

1 can run queue

( based on real-time process scheduling )

The most important data structure-type running queue in the scheduler (Runqueue) . A running queue is a linked list of running processes on a given processor, one for each processor.

Each operational process uniquely belongs to a single, running queue. In addition, the operational queue also includes scheduling information for each processor. Therefore, the operational queue is also the most important data structure for each processor.

In order to avoid deadlocks. The code to lock multiple execution queues must always obtain these locks in the same order : from the low-to-high order of the executable queue addresses.

Note: A deadlock is a phenomenon in which two or more processes are running, because they are competing for resources or because they are communicating with each other. If there is no external force. They will not be able to go forward. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes.

Runqueue is a two-way loop queue, once the scheduling time is triggered, the kernel once again calculates the current queue of all process execution weights. and select the highest weight of the process as the current process into execution.

Its drawbacks:

1 ) scheduling timing trigger, another calculation runqueue The execution weights for each process in the o (n) Span style= "Color:rgb (51,51,51); Font-family: Song body; Font-size:10.5pt ".  

2 ) Runqueue managing real-time and non-real-time processes at the same time (normal processes), the kernel calculates execution weights through process attributes such as real-time or non-real-time, real-time process priorities, user processes, or kernel threading-related factors Count , low flexibility, and not easy to understand and maintain.

2 Priority series Group

each execution queue has two priority series groups, one active and one expired. is a struct of the Prio_array type. A data structure that provides an O (1) level algorithm for the complexity of the order of precedence groups. Priority groups enable each priority of an executable processor to include a corresponding queue, which includes the list of executable processes on the corresponding priority level.

Each priority group also includes a series of struct List_head queue, and each linked list corresponds to a given priority. Each linked list includes all the executable processes that have the corresponding priority on the processor queue.

3 counting time slices again

in the whole process of the time slice ( The time the CPU allocates to each program ) when all is used up, an explicit method is used to compute the time slice of each process again.

the new Linux The Scheduler reduces the dependency on the loop. Instead, it maintains two priority series groups for each processor : both active arrays and obsolete arrays. Processes on the running queue in the active array also have time slices remaining, while processes on the running queue in the expired array run out of time slices.

When the time slice of a process is exhausted. It will be moved to an expired array, but before this. The time slice has given it another calculation.

4 Schedule ()

Select the next process and switch to it to run by Schedule () function is implemented. The function is called directly when the kernel code wants to hibernate, and in addition, it is assumed that any process will be preempted. Then the function is also called to run. the schedule () function runs independently of each processor.

5 Calculating priority and time slices

The nice value is named as a static priority since it was specified by the user from the start. Can not be changed.

The dynamic priority is calculated from a function relationship between static precedence and process interactivity. Effective_prio () function to return the dynamic priority of a process.

This function takes a nice value as a base, plus a reward or penalty for process interactivity between 5 and +5 .

by some judgment to get an accurate reflection of the process is exactly I/O consumption type or processor consumption type.

The most obvious criterion is the length of time the process sleeps. Assuming that a process is dormant for most of its time, it is I/O -consumption. Assume that a process runs longer than the time it sleeps. Then it's processor-expendable.

There is one more aspect. It's relatively easy to calculate the time slice again. It can only be based on the static priority level. When a process is created, the new child process and the parent process divide the remaining process time slices of the parent process.

This allocation is fairly fair and prevents users from constantly acquiring time slices by creating new processes. Task_timeslice () function to return a new time slice for a given task. The calculation of the time slice only needs to scale the priority proportionally. It is possible to make it conform to the range of values of the time slice.

The Scheduler also provides a second mechanism to support interactive processes : Suppose a process is highly interactive. Then when the time slice is exhausted, it is placed in the active array instead of the expired array.

6 Sleep and wake up

Hibernate ( blocked ) process is in a special, non-operational state. The process marks itself as a dormant state. Move yourself out of the runtime queue, put in the wait queue, and then call Schedule () to Select and run a different process.

The process of waking is the opposite : The process is set to a running state and then moved from the waiting queue to the running queue.

There are two related process states for hibernation : Task_interruptible and the task_uninterruptible . Hibernation is processed by waiting for a queue.

The wait queue is a simple list of processes that wait for certain events to occur.

The kernel uses wake_queue_head_t to represent the waiting queue. The wait queue can be created statically through declare_waitqueue () . Can also be created dynamically by Init_waitqueue_head () . The wake-up operation is performed through the function wake_up () . It wakes up all the processes on the specified wait queue.

There is a little need to be aware of sleep. There is a false awakening.

Sometimes the process wakes up not because the condition it waits for is reached, so it needs a loop to ensure that the conditions it waits for are actually reached.

7 Load Balancing program

The load Balancing program hasKERNEL/SCHED.Cthe functions inload_balance ()to achieve. It has two methods of invocation. In theSchedule ()when running. Only the current available run queue is empty. It will be called. In addition, it is also called by the timer:every time the system spare1milliseconds to call once or in other cases every $milliseconds to call once. The load-balancing program calls to lock the current processor's operational queue and shield interrupts to prevent the running queue from being interviewed concurrently.


Overview of "Linux" process scheduling

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.