Process scheduling, a scheduler Confessions

I am a process scheduler.

My job is to schedule all of the processes within the computer, CPU resources are allocated for them.

1. Batch era

At first, when I create the operating system, just going to let me use FCFS order process under the scheduling algorithm, simple maintenance. But I later development, far more than his imagination.

1.1 FCFS

FCFS is the so-called " first-come, first-served (First Come First Serve)" each time into the process memory has lined up. Whenever a process on the CPU finished running or blocking, I would choose the front ranks of the process, he went with the CPU to execute.

Take for instance these processes:

According to FCFS algorithm, I would have to press A, B, C, D, E in this order to send them to CPU:

This algorithm sounds simple and fair, however, did not last long, I received a short course of complaint:. "The last time I came in front of a long process, and so a full 200 seconds before he runs out I only spent one second to run over, because waiting for him, I spend so long, so not worth it. "

I think carefully, FCFS algorithm does have this defect - short response time the process is too long, interactive user experience will be worse.

So I decided to replace the scheduling algorithm.

1.2 SPN

This time I design an algorithm called " short-priority task " (Shortest Process Next, SPN). Each selection process is expected shortest processing time. Therefore, when queuing, I will short the aforementioned process from the queue.

This time, the short course has been well taken care of and greatly reduce the average response time of the process, and I am very satisfied with the operating system.

But he quit a long process: those short course every day to jump the queue, they often lead to lack of CPU resources, resulting in a " starvation " phenomenon.

SPN cancellation algorithm is increasing.

This is a big problem. FCFS although response time is long, but in the end all processes must have access to CPU resources. SPN algorithm but not the same, if a steady stream of short process to join the queue, they will never be a long process a chance to execute - terrible.

Therefore, short-priority task algorithm needs to be improved. Is there any way to take care of both short process, but also to take care of a long process?

1.3 HRRN

After the operating system and discussion, we decided to process into consideration two properties: the latency and required service time - wait a long time, required service time is short (that is, short course) process more easily selected.

To quantify, we have developed a formula: response ratio = (waiting time + service time requirement) / required service time. In response ratio algorithm executed first. We call it " high priority response ratio " (Highest Response Ratio Next, HRRN) .

This algorithm has been unanimously praised the length of the process. Although my workload has increased (before each dispatch, I have to recalculate response than all the waiting process) but in order to have the fairness of the process, it was all worth it.

2. Concurrent era

To a new era.

With the proliferation of computers, personal growth in a large number of users, concurrent , that run multiple programs demand emerged. It beats me - only one processor, how to run multiple programs?

Fortunately CPU awoke me: "I am now since the operation speed so fast, why not play to the strengths, get a" pseudo-parallel "come out? "

"Pseudo-parallel? What do you mean"

"That looks like a parallel, serial, or in fact. Each process alternating short time using my resources, but in humans it seems, these processes as" simultaneously "run. "

I understood why.

2.1 RR

After a reminder of the CPU, I quickly worked out a new scheduling algorithm - round-robin algorithm (Round Robin, RR).

In this algorithm, each process will take turns using the CPU resources, but they start running, I will open for them timer , if the timer expires (or perform blocking operations), the process will be forced to "disembark" switch to the next process. As for the process of selecting the next thing, the direct use of FCFS enough.

The new algorithm will face new problems, now my question is, the length of time slices of how design?

Intuitively, the shorter the time slice, the more the process can be run in a fixed time, the CPU can be said that the switching process is to consume a lot of his instruction cycle time slice is too short can lead to waste a lot of CPU resources in the context switching on. Time slice is too long, short interactive command response will be slower. So specifically how to take, we will have to see the size of the interaction time (feels like did not say the same, but at least to the standard thing).

At this stage, my workload will increase greatly - do not have ten seconds before switching a program, now booing, you have to switch dozens of times a second.

2.2 VRR

Round-robin algorithm looks very fair - all the processes are the same time slice. But is this really so?

I / O-intensive processes do not think so, he said to me:! "Scheduler brother, round-robin we did not take care of this type of process ah we often did not stay to slice in half the time CPU, encountered a blocking operation, is you catch it. and we are blocking queue , often stay for a long time. and other blocking operation is over, we have at the ready queue lined up for a long time team. those processor-intensive process, using most of the processor time, lead us to lower performance, response time can not keep up. "

Taking into account the requirements of these processes, I decided to create a new secondary queue for them. When the unblocking of the process, will enter the secondary queue, a process scheduling, preference auxiliary queue process.

This is the " virtual round-robin " (Virtual Round Robin, VRR).

Later, the actual performance results, this method does better than the round-robin. I am quite proud of.

2.3 Priority Scheduling

One day, suddenly find my operating system, secretive, said: "scheduler ah, you know, I want to give the whole system to provide services, recently too many users can process, the process sometimes leads to my service with no response on. I was a little worried this will affect system stability. "

I heard, it was a big deal, system instability that time? Scheduling algorithm to be changed!

Now let the operating system services to get enough resources to run, then, simply let them have the highest priority CPU use it.

Priority scheduling algorithm thus produced.

I have to tell you to make provision - each process will be assigned a priority , to determine their own priority value according to their own circumstances, however, the priority of the user process allowed higher priority kernel processes.

When switching program, I would choose a process from the queue priority 1, if the priority queue is empty 1, Priority 2 will choose in the process, and so on.

Of course, in order to ensure low-priority process will not be hungry , I would raise the priority waiting a long time process.

Using this algorithm, I am more busy, not only requires a lot of switching process, also need to dynamically adjust the priority. Maybe this is great power comes great responsibility now.

But I know it is because of my existence, human beings can run multi-channel program on the computer - it makes me feel proud.

I hope you gain something after reading my article.

Thank you for reading, we meet again!

Disclaimer: original article without permission is prohibited reprint

Guess you like

Origin www.cnblogs.com/tobe98/p/11604483.html