Operating System Notes--CPU Scheduling

1--Basic concept

CPU scheduling:

        Pick a process/thread from the process's ready queue as the next process/thread to be run by the CPU;

        In the figure below, when the process generates a state transition (running→end, running→waiting, etc.), the corresponding CPU scheduling will occur;

Conditions for the kernel to run the scheduler for CPU scheduling: (just satisfy one of them)

        ① A process switches from the running state to the waiting state;

        ② A process is terminated;

The scheduler is divided into preemptive and non-preemptive scheduling, while preemptive and non-preemptive can occur in user mode and kernel mode:

        ① Non-preemptive scheduling:

                        The scheduler must wait for the current event to end (other processes cannot be executed until the current process ends);

        ② Preemptive scheduling (switching the current process, preempting the CPU to execute another process) situation:

                        The scheduler executes after the interrupt is serviced;

                        The current process switches from the running state to the ready state, or a process switches from the waiting state to the ready state;

                        Currently running processes can be swapped out;

2--Scheduling principle

CPU scheduling needs to consider many factors, including:

        ① CPU usage: the percentage of time that the CPU is busy;

        ② Throughput: the number of processes completed per unit time;

        ③ Turnaround time: a process from initialization to completion, including the sum of all waiting times;

        ④ Waiting time: the total time the process is in the ready queue;

        ⑤ Response time: the total time it takes from a request being submitted to generating the first response;

CPU scheduling needs to follow the following principles:

        ① High bandwidth (throughput represents the computing bandwidth of the operating system), low latency (response time represents the computing delay of the operating system);

        ② Reduce response time;

        ③ Reduce the fluctuation of average response time;

        ④ Increase throughput: reduce system overhead and realize efficient utilization of system resources such as CPU and I/O;

        ⑤ Reduce the waiting time of each process;

        ⑥ Ensure fairness: try to ensure that the running time of each process occupying the CPU is the same as the waiting time in the ready queue;

3--Scheduling Algorithm

FCFS algorithm:

        FCFS stands for First Come, First Service algorithm (First Come, First Service), manage processes through a queue, and execute corresponding processes according to the order of entering the queue;

        The FCFS algorithm stipulates that if the currently executing thread is blocked, the right to use the CPU is handed over to the next process;

Disadvantages of FCFS algorithm:

        Due to the uncertain order of processes entering the queue, the average waiting time of processes fluctuates greatly;

        The FCFS algorithm may cause tasks that take less time to be queued after tasks that take a long time, resulting in longer waiting times;

Shortest process first algorithm:

        SPN (Shortest process next), SJF (Shortest job first), and SRT (Shortest remaining time) are all short process priority algorithms, which first sort the processes, sort and enqueue according to the predicted running time, and short processes will be given priority implement;

        The short-process-first algorithm has the optimal average waiting time, but it may cause long-term processes to fail to execute, which violates the principle of fairness; because when short processes continue to appear, short processes will be prioritized before long processes, resulting in long-term processes cannot be executed by the CPU for a long time;

        Short process priority can be divided into preemptive and non-preemptive. The preemptive algorithm will judge after each CPU time slice, the remaining execution time of the current process and the execution time of the new process. If the execution time of the new process is shorter , may seize the CPU to execute a new process;

Highest Response Ratio Algorithm:

        The HRRN (Highest Response Ratio Nest) algorithm (not preemptive) will calculate the response ratio of each process, and select the process with the highest response ratio for priority execution;

        Response ratio R = (w + s) / s; w represents the waiting time of the process, and s represents the service (running) time of the process; 

4--Real-time scheduling

Real-time scheduling systems can be divided into strong real-time scheduling systems and weak real-time scheduling systems;

        Strong real-time scheduling system: It is required to complete important tasks within the guaranteed time and must be completed;

        Weak real-time scheduling system: requires important processes to have higher priority and needs to be completed as much as possible, not necessary;

Strong real-time and weak real-time scheduling systems lead to hard deadlines and weak deadlines (deadlines):

        Hard deadlines: when deadlines are missed, the consequences can be catastrophic, and deadlines must be verified to be met in the worst case;

        Soft time limit: Ideally, the deadline should be met to the maximum, and the requirements can be reduced accordingly;

Real-time scheduling algorithm:

        ​​​​​​RM rate monotonic scheduling: the best static priority scheduling, through periodic scheduling, the shorter the cycle, the higher the priority, and the process with the shortest execution cycle;

        EDF earliest deadline scheduling: the best dynamic priority scheduling, the earlier the deadline, the higher the priority, and execute the process with the earliest deadline ;

5--Priority inversion

Priority inversion:

        High-priority tasks are blocked by low-priority tasks, high-priority tasks cannot be scheduled, and medium-priority tasks seize CPU resources;

Specific examples are as follows:

        There are three threads, namely Thread A (high priority), Thread B (medium priority), and Thread C (low priority). The execution of thread A needs to obtain the resources of thread C. At this time, thread A will be blocked by thread C. The guiding resources are satisfied; at this time, in order to satisfy the continued execution of thread A, thread C will execute and return the corresponding resources; however, when thread C is executing, because thread B has a higher priority than thread C, thread B will seize the CPU. As a result, thread C cannot be executed smoothly; when thread C cannot be executed, thread A cannot be executed smoothly, although thread A has the highest priority;

Two ways to solve priority inversion:

        Priority inheritance: Let low-priority threads inherit the priority of high-priority threads, that is, upgrade the priority of low-priority threads; in the above example, the priority of thread C can be temporarily upgraded to the priority of thread A;

        Priority ceiling: When a thread applies for a shared resource, the priority of the thread is raised to the highest priority among all threads that can access this resource. This priority is called the priority ceiling of the resource;

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/130524521