(process management) 05. Process scheduling algorithm


Process scheduling is that the process in thread state obtains the use right of CPU, and the process changes from ready state to running state.

Process scheduling can be divided into:

The preemptive system will schedule according to the priority of processes, and processes can jump in line
The non preemptive system is scheduled according to the first come first serve method, and the processes cannot jump in the queue

There are many process scheduling algorithms. The commonly used scheduling algorithms are as follows:

1. First come first serve (FCFS)

The first process is scheduled first. This algorithm is fair, but if a long job is executed, the subsequent short job will be shelved for a long time, resulting in short job hunger.

2. Short job first (SJF)

In order to avoid the starvation of short jobs, give priority to the execution of short jobs, and then execute long jobs. This will lead to another problem, that is, if there are too many short jobs, it is easy to cause that long jobs will not be executed and long jobs will be hungry.

3. Time slice Round serve (RS)

Time slice rotation is polling, which defines the length of a time slice, and then evenly allocates time slices to each process. Once the time slice is used up, the job will change from running state to ready state and wait to be rescheduled. If there are a lot of homework, it is easy to grow. The homework needs to rotate for a long time to complete.

4. Multilevel feedback queue

The algorithm sets different queues, which can be classified into high, medium and low priority queues. The higher the priority, the shorter the allocated time slice. On the contrary, the lower the priority, the longer the allocated time slice. First, advanced jobs will enter high priority; If it is not completed, it will be pushed into the medium priority queue; If the execution is not finished, press it into the low priority queue. Only when the process of the previous queue has been executed can the process of the current queue be executed.

This algorithm can not effectively avoid the starvation of long jobs, because only when there is no process in the high priority queue, the next level queue will be executed. If the upper level queue has processes all the time, the processes of the next level queue will be hungry.

5. High response ratio priority

This algorithm is based on the short job first scheduling algorithm and a weight mechanism superimposed with time accumulation.

The system will determine the order of process execution according to the priority, but at the same time, in order to ensure that those processes with long waiting time can also be executed, the weight of the process will be updated in combination with the waiting time of the process. The longer the waiting time, the higher the weight.

This algorithm can not only give priority to short jobs, but also ensure that long jobs will not be hungry for a long time. It is a compromise algorithm.