[Principle] Process scheduling algorithm

The principle of process scheduling:
  • The reason for the need for process scheduling is very simple, that is, to make full use of the CPU resources in the computer system, so that the computer system can complete the various tasks we let it do as quickly and efficiently. For this reason, the number of processes that can be stored in the memory is much larger than the number of CPUs in the computer system, so that these processes can be scheduled by the process scheduler of the operating system, making the process efficient (high throughput – throughput), timely ( Low latency – latency), fairness in CPU usage. For this reason, the scheduler can design different scheduling algorithms to select the process, which reflects the process scheduling strategy. At the same time, it needs and further completes the process switching through the context switch of the process, which reflects the process scheduling mechanism. In general, when do we need to schedule (timing of scheduling), whether scheduling can be done anywhere in the kernel execution (how to schedule), if process switching is done (context switching), if we choose the "appropriate" process to execute (scheduling) strategy/scheduling algorithm), if evaluating the rationality of the selection (indicator of process scheduling). Understanding the above details can also be said to understand process scheduling.

Process scheduling metrics:

Different process scheduling algorithms have different characteristics, so it is necessary to establish basic indicators to measure an algorithm. In general, the main factors for measuring and comparing the performance of various process scheduling algorithms are as follows:

  • CPU utilization: The CPU is a scarce resource in a computer system, so the CPU should be kept as busy as possible when there are specific tasks, so that the utilization of CPU resources is the highest.

  • Throughput: The size of the workload when the CPU is running is described by the number of processes completed per unit time, which is called throughput.

  • Turnaround time: refers to the time elapsed from the creation of the process to the end of the process, which includes process blocking due to various factors (such as waiting for I/O operations to complete), being in a ready state and queuing in the ready queue, and processing The sum of the time spent running on board.

  • Waiting time: The sum of the time the process spends waiting in the ready queue. Therefore, a simple way to measure a scheduling algorithm is to count the waiting time of the process on the ready queue.

  • Response time: Refers to the time from the event (such as the generation of a clock interrupt event) to the time the process or system responds. In interactive desktop computer systems, users expect faster response times, often at the expense of throughput.

These indicators are actually conflicting with each other. The short response time means that after the relevant event occurs, the operating system needs to quickly switch the process, so that the corresponding process can respond to the generated event as soon as possible, resulting in increased process scheduling and switching overhead. , which reduces the throughput of the system.


Timing of process scheduling:

The timing at which process scheduling occurs (also called the scheduling point) is directly related to the state change of the process. Looking back at the process state change diagram, we can see that the timing of process scheduling is directly related to the transition timing of the process in the running state <–> exit state/ready state/blocking state. In a nutshell, the timing that causes a process to schedule can be summed up in the following categories:

  • After the execution of the executing process is completed, a new ready process needs to be selected for execution.

  • The executing process calls related system calls (including system calls related to I/O operations, synchronous mutex operations, etc.), which results in the need to wait for an event to occur or wait for resources to be available, thus blocking itself into a blocking state.

  • The executing process actively calls the system call that abandons the CPU, causing its own state to be ready, and putting itself back into the ready queue.

  • A process waiting for an event to occur or resources to be available waits for a queue, which causes the process to return from a blocked state to a ready state and can participate in scheduling.

  • The time slice of the executing process has been used up, so its state is ready, and it is put back into the ready queue.

  • At the moment before returning to the user process after executing the system call, a new user process can be scheduled and selected for execution

  • The priority of a process in the ready queue becomes higher than the priority of the currently executing process, which will also trigger process scheduling.


Process scheduling method:

It should be noted here that there are two scheduling methods for processes to preempt the processor:

  • Preemptive (preemptive): Once the priority of a process in the ready queue is higher than that of the currently executing process, the operating system will immediately schedule the process to complete the process switch.

  • Non-preemptive (non-preemptive non_preemptive): Even when there is a process in the ready queue that has a higher priority than the currently executing process, the current process will still occupy the processor for execution until the process itself enters the blocking state, or When the time slice is used up, or just before returning to the user process after executing the system call, the scheduling yields the processor again.


Process scheduling strategy/algorithm:
  • Most of the scheduling methods in the early operating systems are non-preemptive, because the early applications are generally scientific computing or transaction processing, and the response time index of human-computer interaction is not a priority. In this case, the running process can hold the CPU until the process blocks or terminates. The scheduling algorithm in this way can be very simple, and is more suitable for batch scientific computing or transaction processing applications that do not care about response time or care very little. With the further expansion of the application field of computers, computers are more used in human-computer interaction applications such as multimedia. For this reason, the preemptive scheduling method can deprive a process of its execution right before it terminates or blocks, so that the CPU can be used as soon as possible. Assigned to another "more important" process, giving processes in the ready queue a chance to respond to their user's IO events. The scheduling algorithm based on these two methods is as follows:

1. First-come, first-served (FCFS) scheduling algorithm: Processes in the ready state are linked to the ready queue in sequence, and the FCFS scheduling algorithm selects the process that is currently the first to enter the ready queue according to the order in which the ready processes enter the ready queue. Execute until the process blocks or ends, and the next process selection scheduling is performed. The FCFS scheduling algorithm adopts a non-preemptive scheduling method. Once a process occupies the processor, it will continue to run until the process completes its work or cannot continue to execute due to waiting for an event, and the processor is released. If the operating system adopts this process scheduling method, a long-running and running process will cause many late and short-running processes to wait too long.

2. Short Job First (SJF) Scheduling Algorithm: In fact, there are fewer and fewer references to jobs at present. Let's replace "job" with "process" and call it a short process priority scheduling algorithm. This algorithm selects the exact schedule in the ready queue. The process with the shortest (or estimated) running time enters execution. It can use either a preemptible scheduling method or a non-preemptive scheduling method. The preemptible short-process-first scheduling algorithm is also commonly referred to as the Shortest Remaining Time First (SRTF) scheduling algorithm. The short process priority scheduling algorithm can effectively shorten the average turnaround time of the process and improve the throughput of the system, but it is not conducive to the operation of long processes. And if the running time of the process is "estimated", it will lead to the fact that the estimated running time is not necessarily accurate, and short jobs cannot be given priority.

3. Round Robin (RR) Scheduling Algorithm: The RR scheduling algorithm is similar to the FCFS scheduling algorithm in the selection process, but differs in the timing of the scheduling. The RR scheduling algorithm defines a unit of time, called a time slice (or time quantum). A time slice is usually between 1 and 100 ms. When the running process runs out of time slice, even if the process is still running, the operating system does not let it continue to run, but selects the next process in the ready state from the ready queue for execution, and the process that is deprived of CPU usage Return to the end of the ready queue, waiting to be scheduled again. The size of the time slice can be adjusted. If the time slice is large enough for a process to complete all its work, this algorithm degenerates into the FCFS scheduling algorithm; if the time slice is set very small, the processor context switches between processes. The work is so frequent that less time is actually spent running the user program. The time slice can be set statically or dynamically adjusted according to the current system load and running conditions. The dynamic adjustment of the time slice size needs to consider many factors such as the number of ready processes, process context switching overhead, system throughput, and system response time. .

4. Highest Response Ratio First (HRRF) scheduling algorithm: The HRRF scheduling algorithm is a compromise algorithm between the first-come, first-served algorithm and the shortest process first algorithm. The first come first serve algorithm only considers the waiting time of the process and ignores the execution time of the process, while the shortest process priority scheduling algorithm only considers the execution time of the process estimated by the user and ignores the waiting time of the ready process. The HRRF scheduling algorithm takes both into consideration, considering both the process waiting time and the execution time of the process. The response ratio (Rp) is defined for this purpose:

  • Rp = (waiting time + estimated execution time) / execution time = response time / execution time -

The previous expression assumes that the sum of the wait time and the expected execution time equals the response time. The HRRF scheduling algorithm will select the process with the maximum value of Rp to execute, which not only takes care of the short process but also does not make the waiting time of the long process too long, and improves the scheduling performance. However, the HRRF scheduling algorithm needs to calculate the response ratio Rp of each process each time, which will bring a large time overhead (especially in the case of a large number of ready processes).

5. Multi-Level Feedback Queue scheduling algorithm: The execution logic flow of the multi-level feedback queue scheduling algorithm is as follows:

Set up multiple ready queues and give each queue a different priority. The first queue has the highest priority, followed by the second, and the rest of the queues have lower priorities. Only when the first to i-1 queues are empty, the operating system scheduler will schedule the processes in the i-th queue to run. The size of the execution time slice given to the processes in each queue is also different. In the queue with higher priority, the execution time slice of each process is smaller or larger (the Linux-2.4 kernel adopts this method).

When a ready process needs to be linked into the ready queue, the operating system first puts it at the end of the first queue, and queues up for scheduling according to the principle of FCFS. If it is the turn of the process to execute and it has not completed at the end of a time slice, the operating system scheduler will transfer the process to the end of the second queue, and then wait for the scheduled execution according to the principle of first come, first served. In this way, when a long process is dropped from the first queue to the last queue, in the last queue, the FCFS or RR scheduling algorithm can be used to run the process in this queue.

If the processor is serving a process in the i-th (i > 1) queue, and a new process enters the k-th (k < i) queue, the new process will preempt the processor of the running process, that is, by the scheduler Put the executing process back at the end of the i-th queue, and reassign the processor to the new process in the k-th queue.

From the MLFQ scheduling algorithm, it can be seen that long processes cannot occupy the processor for a long time, and the response time of the system will be shortened and the throughput will be good (provided that there are no frequent short processes). Therefore, the MLFQ scheduling algorithm is a comprehensive process scheduling algorithm suitable for different types of application characteristics.

6. The highest priority priority scheduling algorithm: the priority of the process is used to indicate the importance of the process and the priority of its operation. The priority of a process can be divided into two types: static priority and dynamic priority.

Static priority is determined when the process is created. Once determined, it does not change for the entire duration of the process. The static priority is generally set by the user according to factors including the type of the process, the resources used by the process, and the estimated running time of the process. Generally speaking, if the process needs more resources and the estimated running time is longer, the priority of the process is lower; on the contrary, the priority of the I/O bounded process can be set higher.

Dynamic priority refers to adjusting the priority according to the changes in the execution of the process during the running process of the process. The dynamic priority is generally determined according to factors such as the length of the process occupying the CPU time and the length of the process waiting for the CPU time. The longer the processor is occupied, the lower the priority, and the longer the waiting time, the higher the priority. Then the process scheduler will execute the ready process with the highest priority now according to the sum of static priority and dynamic priority.

In the operating system, in order to allow each process to have the opportunity to run, it is necessary to allocate a time slice to each process. When the time slice of a process is used up, the scheduler of the operating system will let the current process give up the CPU and choose another process. A process takes up CPU execution. In order to effectively support the time slice required for process scheduling, ucore designs and implements a timer function. In this way, the timer provides basic support for the scheduling mechanism based on time events.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728840&siteId=291194637