A common process scheduling algorithm for Linux

Source: Internet
Author: User

Process scheduling: Scheduling in the operating system refers to a resource allocation.

Scheduling algorithm refers to the resource allocation algorithm according to the resource allocation policy of the system.

The operating system manages the limited resources of the system, and when there are multiple processes (or requests made by multiple processes) to use these resources, because of the limited resources, it is necessary to select processes (requests) to occupy resources according to certain principles. This is the dispatch. The goal is to control the number of resource users, and select resource user licenses to occupy resources or occupy resources.

So let's look at the common process scheduling algorithm:

1. First come first to serve:

Concept:

If the well-prepared process is in front of the ready queue, and the late-ready process is placed behind the ready queue, the first-come-first service (the Fcfs:first come-a) always dispatches the process that is currently at the top of the ready queue to the running state. It also says that it only considers the order in which the process enters the ready queue, regardless of the length of its next CPU cycle and other factors.

Essentials:

Schedule and assign processor execution in the order in which the process enters the ready queue. First come first service scheduling algorithm is a non-preemptive algorithm, first into the ready queue process, the first allocation processor operation. Once a process takes over the processor, it runs until the process completes its work or is not able to continue running because it waits for an event to occur.

(1) If the system has a fallback job queue or a ready process queue established by FIFO rules, it is a job control block JCB or the Process control block PCB joins the queue at the end of the corresponding queue.

(2) scheduled to exit the queue from the beginning of the corresponding queue scan, the relevant JCB or PCB scheduling to move out of the corresponding queue.

Flow chart:

Execution process:

Pros: useful for long jobs and CPU busy jobs

Cons: not conducive to short jobs and I/O busy jobs

2. Short Job ( process ) priority scheduling algorithm SJ (P) F

Concept: Job (process) prioritization for short execution times. Usually later short jobs do not preempt the job being executed.

Advantages:

Improvement of average turnaround time and average time to cycle, shorten the waiting time of the job than FCFS;

Improve the throughput of the system;

Disadvantages:

Very unfavorable to long work, may not be executed for a long time;

Failure to prioritize execution based on the urgency of the job;

It is difficult to accurately estimate the execution time of a job (process), which can affect scheduling performance.

Flow chart:

3. Rotation Method

Concept: Allow each process to wait in the ready queue in proportion to the time it takes to enjoy the service.

Defined:

The time slice rotation method is similar to "turns the Republicans" the thought, the condition is: each job approximate thought "simultaneously" arrives, the question condition is the back work sequentially is late one time unit, the analysis should be strictly according to the RR scheduling algorithm realization thought: The system puts all ready processes in the first-in first-out principle into a queue. The new process is added to the end of the ready queue. Whenever a process scheduler is executed, the process scheduler always selects the ready queue's first process, allowing it to run a time slice on the CPU. When the process runs out of time slices to it, the scheduler stops the process and puts it at the end of the ready queue.

Flow chart:

4. Multilevel Feedback queue algorithm

Concept:

Set up multiple ready queues, assigning different priorities, such as decreasing progressively, with queue 1 having the highest priority. The length of the execution time slice of each queue is also different, the lower the priority level, the longer the time slice, such as doubling.

After the new process enters the memory, first puts into the queue 1 the end, presses the FCFS algorithm to dispatch, if by the queue 11 time slices failed to execute, then reduces the input to the end of the queue 2, similarly presses the FCFS algorithm to dispatch; So down to the last queue, the "time slice rotation" algorithm is dispatched until the completion.

Processes in lower-priority queues are dispatched only if the higher-priority queue is empty. If a process executes when a new process enters a higher-priority queue, the new process is preempted and the preempted process is put to the end of the original queue.

Multilevel feedback queue scheduling algorithm, also known as feedback loop queue or multi-queue strategy, the main idea is to divide the ready process into two or more stages, the system establishes two or more ready process queues, the higher priority queue is generally allocated to a shorter time slice. Processor scheduling selects the processor-owning process from the advanced-ready process queue first, and is selected from the lower-level ready process queue only if it is not available.

Advantages:

Take care of short processes to improve system throughput and shorten average turnaround time.

Take care of I/O processes for better I/O device utilization and reduced response time.

It is not necessary to estimate the execution time of the process and adjust dynamically.

Flow chart:

Comparison of several algorithms:


So what is the process scheduling algorithm that I use under my Linux?

Let's take a look at our Linux kernel version:

Our kernel version is 2.6.

So let's check out the Linux 2.6 process scheduling algorithm

In Linux 2.6, there are four kinds of scheduling algorithms about IO, summarized below:

1) NOOP
The NoOp algorithm is completely written as no operation. The algorithm implements the most simple FIFO queue, and all IO requests operate roughly in the order of first served. The reason is "approximate", because noop on the basis of FIFO also made the merger of adjacent IO requests, not completely in accordance with the first-in-one rule to meet the IO request. The NoOp assumes that the I/O request is optimized or sequenced by the driver or device (just like the work done by a smart controller). In some SAN environments, this option may be the best choice. Noop is less concerned with IO, and all IO requests are processed in a FIFO queue, with the default being that IO is not a performance issue. This also makes the CPU less worried. Of course, for a more complex application type, using this scheduler, users will be very worried about themselves.

2) Deadline Scheduler
Based on CFQ, deadline solves the extreme situation of Io request starvation. In addition to the IO sort queue that CFQ itself has, the deadline provides a FIFO queue for read io and write Io, respectively. The maximum wait time for a read FIFO queue is 500ms, and the maximum wait time for a write FIFO queue is 5s. The IO request priority in the FIFO queue is higher than in the CFQ queue, and the read FIFO queue has a higher priority than the write FIFO queue. The priority can be expressed as follows:
FIFO (Read) > FIFO (Write) > CFQ
The deadline algorithm guarantees a minimum delay time for a given IO request, which is understood to be appropriate for DSS applications.

3) Anticipatory Scheduler
The focus of CFQ and deadline is on satisfying fragmented IO requests. For continuous IO requests, such as sequential reads, no optimizations are made. To meet the random IO and sequential IO mix scenarios, Linux also supports the anticipatory scheduling algorithm. Based on deadline, the anticipatory is set to 6ms for each read IO.
The Wait time window. If the OS receives a read IO request from an adjacent location within this 6ms, it can immediately meet
Anticipatory scheduler (AS) was once the IO scheduler of the Linux 2.6 Kernel. Anticipatory's Chinese meaning is "expected, expected", the word does reveal the characteristics of the algorithm, in short, when there is an IO occurs, if there is a process request IO operation, it will produce a default 6 millisecond guessing time, guess the next process request Io is what to do. This can result in a large delay in reading, bad for the database application, and a good performance for Web Server. This algorithm can also be easily understood to target low-speed disks, because that "guess" is actually intended to reduce the head movement time.

4) CFQ
The full write of the CFQ algorithm is completely Fair Queuing. The algorithm is characterized by the address of the IO request, rather than responding in the order of first served.
On traditional SAS disks, disk pathfinding takes most of the IO response time. The starting point for CFQ is to sort the IO addresses to meet as many IO requests as possible with as few disk rotations as possible. With the CFQ algorithm, the throughput of the SAS disk is greatly improved. However, compared to NoOp's disadvantage is that the first-come IO request is not necessarily satisfied, there may be starvation situation.
Completely Fair Queuing (CFQ, full fair queue) replaced 2.6.18 anticipatory in scheduler as the default IO Kernel for Linux Scheduler. CFQ maintains an IO queue for each process, and IO requests from each process are handled by the CFQ in a round-robin manner. That is, it is fair to each IO request. This makes the CFQ ideal for discrete-read applications (EG:OLTP DB). In the Enterprise Linux distributions I know, SuSE Linux seems to be the first to default with CFQ.

The algorithm for viewing and modifying the IO Scheduler is very simple. Suppose we want to operate on SDA as follows:
Cat/sys/block/sda/queue/scheduler
echo "CFQ" >/sys/block/sda/queue/scheduler

Summarize:
The focus of 1 CFQ and deadline is on satisfying fragmented IO requests. For continuous IO requests, such as sequential reads, no optimizations are made. To meet the random IO and sequential IO mix scenarios, Linux also supports the anticipatory scheduling algorithm. Anticipatory on the basis of deadline, for each read IO set a 6ms waiting time window. If the OS receives a read IO request from an adjacent location within this 6ms, it can be satisfied immediately.

The choice of the IO Scheduler algorithm depends on both the hardware characteristics and the application scenario.
In the traditional SAS disk, CFQ, DEADLINE, anticipatory are good choices, and for the dedicated database server, DEADLINE throughput and response time are good. However, on the emerging SSD, such as SSD, Fusion io, the simplest noop is probably the best algorithm, because the other three algorithms are optimized based on shortening the seek time, and the SSD has no so-called seek time and the IO response time is very short.

2 for database applications, the performance of anticipatory Scheduler is the worst. The Deadline in the DSS environment is a little better than the CFQ, and the CFQ is better in terms of overall performance. It's no wonder that RHEL 4 default IO Scheduler is set to CFQ. While Rhel 4 is more than Rhel 3, overall IO improvements are still not small.

A common process scheduling algorithm for Linux

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.