In the normal operation of the computer, there are a lot of processes in the background, but each other "no quarrel, no contention", thanks to the scheduling algorithm in the operating system .
Usually. The execution of most processes can be easily divided into two steps :
The first step, the need to run the process from the external memory (for example, the computer's hard disk) selected, sent to the memory "waiting for the purpose", ready to let the CPU to run;
In the second step, the CPU chooses one of several processes in which memory "is intended". Start running.
Simply put, scheduling is the way to choose .
"Scheduling is the basis of multi-channel program operating system, is the core of operating system design." in the "two-step walk" above. The selection in the first step. This schedule is called "Job scheduling", and in the second step, this schedule is called "process scheduling". "
In the computer development process, gradually formed a variety of options, and some of the options apply to the "first step" stage, some applicable to the "second step" stage, and some of the two are applicable.
There are many scheduling algorithms in the operating system, and some of them are suitable for job scheduling. Some scheduling algorithms are suitable for process scheduling, and some scheduling algorithms are suitable for both. 】
There are six ways to compare classics, in order to describe these six ways carefully. Introduce a situation-"small island Fetch Water".
In other words, a group of people came to a small island. Decided to live on a small island for a while. There is a need for water during life, but there is only one water source on the island, but fortunately there is a steady flow.
A group of people flocked to fetch water, not only disorderly, but also cause crisis. So we deliberated on the way. Set up a system to ensure the order of water intake. very fast. Several programs of order came forward, each with advantages and disadvantages, so decided to start the trial.
On the first day, start the pilot program one: who first to fetch water first. The rest of the queue . So. Each time the water source is lined with long queues, people quietly wait for their turn to chance. very fast. The problem arises. Some people use buckets to take water to bathe, some people use a cup to fetch water just to drink, because first come first get system. Water drinkers had to wait for a few 10 minutes to hit a glass and wait until the people in front of them had finished their turn. Gradually, we found that this method seems fairer, but too rigid, should be improved.
Very well, this approach presents such a first-come-first-served mindset that can be integrated into other solutions .
"Such a scheduling algorithm is called" first come first service scheduling algorithm "(FCFS), the algorithm is characterized by simple algorithm. But the efficiency is low, the average waiting time is too long. It is advantageous to the long operation, but it is unfavorable to the short work, but it is often used in other scheduling algorithms.
】
The next day, in order to make up for the loophole in program one. We decided to try the plan two: let the need for water less first dozen, need more back row .
This makes everyone very enthusiastic about "take less", due to a need for less, you can line up in front, no one has arrived. at noon suddenly found that one of the team has been waiting for the whole morning, because he used buckets to fetch water, need more. Should be in the back, not easy to look at the front of fewer people. There's a bunch of water with a cup. Because of the need to be able to "jump in the queue", the result to dark did not hit the water.
So, everyone slowly into the cup to draw water, and the cup is smaller, and even a spoon to take the alternative.
"This scheduling algorithm is called" Short job Priority scheduling algorithm "(SJF), although the average waiting time is greatly shortened, but the long operation is unfavorable. Even some long jobs are never executed ("hunger").
In addition, when the user artificially shorten the execution time, the algorithm does not necessarily really do short job first. 】
On The third day, everyone starts running scenario three: queue up to fetch water in order of urgency . A document was developed to describe the urgency of the narrative of every day, with priority being given to describe the urgency of the need for water.
Every time you come to fetch water, you first report what you are doing. Prioritize to determine priorities. However, it is found that such a priority document is difficult to develop, among them is a "water wash", the urgency of this provision is very low (the reason is: The island is surrounded by the waters. Wash hands with seawater), so a child wants to wash his hands. No matter how to wait, it is not your turn to fetch water. And those who meet the highest priority water intake. Always repeating to fetch water, high priority seems to be the prerogative of these people, which leads to complaints. in the afternoon. has started to improve. Use the Adjust priority policy. Just take a water once. The priority falls from one level to the point that theoretically gives those who are not in a hurry to take water, and this day is barely over.
"This scheduling algorithm is called" Priority scheduling algorithm. " The same algorithm will cause "hunger" phenomenon, so that some jobs will never run. in order to solve the problem, the algorithm can be divided into "static priority" and "dynamic Priority", by dynamically adjusting the priority to solve the "hunger" phenomenon (this action is called "aging"). "
Day fourth, operational Scenario IV: Integration of programme I and programme two, that is, at the same time. Need to hit first, need the same amount, first-come first hit . since programme IV balances programme I and programme two, it has been run stably, with cups of water due to the need for less, no need to wait too long; Need more, but only to go to the early row of a good team, the same can hit the water fundamentally overcome some people will never hit the water situation.
" This scheduling algorithm is called "high response ratio priority scheduling algorithm", the algorithm is mainly used for job scheduling, response ratio = (wait time + run time)/run time. facilitates short work. Also took into account the long work, overcame the "hunger" phenomenon. "
On the fifth day, everyone thought the fourth day was a very stable one. Maybe we can find a better solution, and then run the program five: everyone together. The average water is recycled from one person to another, all the way to meet the needs of all people . This approach is considered very fair. Treat each person equally. One day down, everyone has no grievances. But the people who divide the water do not do it. because it is too difficult to do the work of water. Do not know the amount of water at a time is more appropriate- one time too little, you need to cycle very many times, too much trouble, too many times, it is better to let them directly to the water to get water more convenient .
"This scheduling algorithm is called" time-slice rotation scheduling algorithm ", in this algorithm, the size of the time slice of the system performance is very large. The time slice is too small, the CPU frequently switches, the overhead is too big, the time slice is too big, the algorithm degenerate into first come first service scheduling algorithm. "
On the sixth day, the runner disappeared one day, maybe they went on vacation. Maybe they're going to find a way.
On the seventh day, they came back high and strong. Start running Scenario Six: let the people who come to fetch water in accordance with the ranks, and still arrange for a person to cycle the average split.
But this time the water is improved-the first round of each person to a cup, the need for a cup of people to get a glass of water comfortable to leave. There is still a need to remain in the team because it is not enough, and the second round has two cups for each person. The same, contented people leave comfortably. The people who still need to stay ... And so on
In this way, the number of the team is getting smaller and less, and no matter what the complaint is, more crucially. The plan stipulates that the first person to drink a cup of water, lose is to stay, he will depend on their own needs to decide, and not into endless waiting. in this way, it takes into account a lot of aspects, the people who need less to take a glass of water immediately on the line to meet. Shorten the average waiting time, and do not need to report the water is to do, at the same time also avoided the never hit the situation.
This scheduling algorithm is called "Multilevel feedback queue scheduling algorithm", which is the synthesis and development priority of time-slice rotation scheduling algorithm and priority scheduling algorithm. Take into account the short job first and wait a short time and long operation part of the operation to avoid "hunger" phenomenon.
】
The above is the operating system six classic scheduling algorithm ( first come first service scheduling algorithm, short job priority scheduling algorithm, priority scheduling algorithm, high response ratio priority scheduling algorithm, time slice rotation scheduling algorithm, multilevel feedback queue scheduling algorithm ), each has advantages and disadvantages, Different operating systems choose different process scheduling algorithms depending on their side weight. Turn. Different scheduling algorithms are applicable to different requirements of the operating system.
* Mind collisions, don't forget to share with friends! the future will launch the "Computer Thinking Story" series, Thank you for your attention!
*
Storytelling of computer thinking--A typical scheduling algorithm of operating system