Operating system core principles-3. Process Management (middle): Process scheduling

Source: Internet
Author: User

PS: In a multi-process concurrency environment, although there are conceptually many processes executing at the same time, in a single CPU, only one process can be in execution at any time, while other processes are in a non-executing state. So the question is, how do we determine which process is executing at any given moment and what does not? This involves an important part of process management: process scheduling, follow this article to review the process of scheduling it!

First, process scheduling basic 1.1 process scheduling definition

Process scheduling is an important part of operating system process management, and its task is to select the next process to run .

1.2 Process scheduling target

First of all, the general program tasks are divided into three kinds: CPU compute-intensive, IO-intensive and balance (compute and IO) type, for different types of programs, scheduling needs to achieve the purpose is different. For IO-intensive, the response time is the most important; for CPU-intensive, turnaround time is the most important, and for a balanced type, it is important to balance the response with a turnaround.

Therefore, the goal of the process scheduling is to achieve the minimum average response time, the maximum system throughput rate, to keep the system's various functional components are busy and provide some kind of seemingly fair mechanism .

PS: Why do you want to keep all of the system's functional parts busy? Because the CPU is very expensive, leaving it idle is a waste, so it is important to keep the CPU busy. Just like life is very precious, we only have one to keep learning to remain full of state, is not waste of life.

Second, basic scheduling algorithm 2.1 first come first service algorithm

First come first service (FCFS) algorithm is one of the most common algorithms, it is a kind of fairness in human nature. The advantage is simple and easy to implement, the disadvantage is that short work may become very slow, because there is a long way ahead of the implementation of the work, which will result in the user's interactive experience is also poor.

For example, when queuing for business, you need to handle the business only a few minutes can be done, but the person in front of you to handle the matter is very complicated to take 1 hours, then you need to wait for a long time behind him, so you think: if everyone in turn for 10 minutes of business, then how good! Then there is the time slice rotation algorithm.

2.2 Time slice rotation algorithm

Time-slice rotation is an improvement to the FCFS algorithm, whose main purpose is to improve the response time of short program, and to realize the process switching periodically . The focus of time-slice rotation is on the choice of time slices, which need to consider many factors: if the running process is long, the time slices need to be shorter, and the number of processes is small, the time slice can be longer. Therefore, the choice of time slice is a comprehensive consideration, weigh the interests of all parties, make an appropriate compromise.

However, the system response time of the time slice rotation is not always shorter than the FCFS response time. Time-slice rotation is a kind of same big pot practice, but the real life is to walk "some people first rich, first rich drive after the rich" route. For example, if there are 30 tasks, one of which takes only 1 seconds to execute, while the other 29 tasks take 30 seconds to execute, and if for some reason this task of 1 seconds is rotated behind a further 29 tasks, it will need to wait 29 seconds to execute (assuming the time slice is 1 seconds). As a result, the response time and interaction experience of this task becomes very poor. Therefore, the short task priority algorithm is presented.

2.3 Short Task Priority algorithm

The core of the short-task-first algorithm is that all tasks are not the same, but have priority distinctions. Specifically, shorter tasks have a higher priority than long tasks , and we always prioritize high-priority tasks to run first .

The short task priority algorithm is divided into two types: one is non-preemptive and the other is preemptive. Non-preemptive when a task that is already running on the CPU ends or blocks, select the process that executes the shortest execution time from the candidate task. The preemption , however, is that each additional process requires a check of all processes, including processes running on the CPU, and who is running short .

Because the short task first always runs the program which needs the shortest execution time, the average response time of the system is optimal in the above algorithms, which is also the advantage of the short task priority algorithm. But the short-task-first algorithm also has drawbacks: one is that it can cause long tasks to not get CPU time resulting in "muscle starvation". How long does it take to know how much each process is going to run? Therefore, in order to solve the first disadvantage, the priority scheduling algorithm is proposed. And the second disadvantage can take some heuristic method to estimate, many AI algorithms can do this now.

2.4 Priority scheduling algorithm

Priority scheduling algorithms give each process a priority, and each time a process switch is required, a process with the highest priority is called for scheduling . This way, if the long process is given a high priority, the process will no longer be "hungry". In fact, the short-task priority algorithm itself is a priority scheduling, but it gives a shorter process a higher priority.

The advantage of this algorithm is that it can give important processes high priority to ensure that important tasks can get CPU time, the disadvantage is two: one is low priority process may be "hungry", second, response time is not guaranteed. The first disadvantage can be resolved by dynamically adjusting the priority of a task, such as if a process waits too long, its priority will be higher than the priority of other processes, resulting in CPU time. The second disadvantage can be resolved by setting a process priority to the highest, but even if the priority is set to highest, the response time is not guaranteed if everyone sets their process priority to the highest.

2.5 Hybrid Scheduling algorithm

Before the algorithm has some shortcomings, then can have an algorithm to mix their advantages, discard their shortcomings, this is called the hybrid scheduling algorithm. The hybrid scheduling algorithm divides all processes into different large classes, each of which is a priority. If the two processes are in different large classes, the processes in the high-priority class will take precedence, and if they are in the same large class, they will be executed using a time-slice rotation algorithm. The hybrid scheduling algorithm is as follows:

2.5 Process Scheduling Process

Third, the priority of scheduling anomalies upside down 3.1 what is priority upside down

Priority inversion refers to a low-priority task that holds a shared resource that is required by a high-priority task. Such high-priority tasks are blocked by resource scarcity until the low-priority task frees resources. This actually causes the two tasks to be prioritized upside down.

3.2 Priority upside down representation

(1) Low-priority processes that do not hold resources impede the implementation of high-priority processes that require resources;

(2) Priority processes holding resources impede the implementation of high-priority processes that require resources;

3.3 Priority upside-down prevention measures

(1) For the first form, the method of interrupt prohibition can be used, the core of which is to protect the critical section by prohibiting interruption.

(2) For the second form, the low-priority process cannot hold the resources required by a high-priority process, which can be achieved by the priority upper bound and precedence inheritance.

Priority upside down problem, will learn in detail later, then detailed talk about. Of course, if you want to know the funeral, please see tell .

  

Resources

Heng Hengming, "The philosophical principles of the operating system", mechanical industry Press

Zhou Xurong

Source: http://edisonchou.cnblogs.com

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

Operating system core principles-3. Process Management (middle): 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.