Operating System Concepts_Chapter 5_CPU Scheduling

Overview

With a uniprocessor system, the CPU can only execute one process and the other processes have to wait until the CPU is free. The goal of multiprogramming is to have processes executing at all times, maximizing CPU utilization.
CPU scheduling makes a process wait (usually waiting for some IO request to complete) to take the CPU usage right from the process to another process, and so on.


CPU-IO interval cycle

A process has two properties: CPU execution , and I/O wait cycles , which are the basis for the successful scheduling of CPUs. The process execution starts from the CPU section (CPU burst) , then goes to the I/O section (IO burst) , and the cycle goes back and forth, and the last operation is also in the CPU section (the execution is terminated by the system request).
The length of the CPU interval Through a large number of tests, it is found that a process usually has a large number of short CPU intervals and a small number of long CPU intervals. This distribution helps to choose an appropriate CPU scheduling algorithm.


CPU scheduler

When the CPU is idle, the operating system selects a process from the ready queue to execute, this operation is usually performed by the short-term scheduler or the CPU scheduler.
The ready queue can have various implementations (FIFO, priority queue, tree, unordered linked list...) , but conceptually, all queues in the ready queue are queued, and the record in the queue is usually a PCB.

CPU scheduling decisions typically occur in the following four contexts:

  1. A process switches from running state to waiting state (such as IO request, system call wait() )
  2. A process switches from running state to ready state (eg interrupted)
  3. A process switches from waiting state to ready state (such as IO request completion)
  4. when a process terminates

For cases 1 and 4, there is no choice but scheduling. A new process (if any) must be elected to execute, and the scheduling scheme in this case is called nonpreemptive or coorperative .
For other cases (including but not limited to the 2nd and 3rd), a choice can be made, in which case the scheduling scheme is preemptive .


dispatcher

The dispatcher (dispatcher) is a module, which is another part related to the CPU scheduling function, and has the following functions:

  • switch context
  • switch to user mode
  • Jump to the appropriate location in the user program to restart the program

The dispatcher is called on every switch (so it should be as fast as possible), the time it takes for the dispatcher to stop one process and start another is the dispatch latency


Scheduling Guidelines

Different CPU scheduling algorithms may apply to different processes. In order to compare the scheduling algorithms of various CPUs, the following guidelines are proposed:

  • CPU usage : CPU should be as busy as possible, for real systems, CPU usage is generally 40%~90% (need to be maximized)
  • Throughput : The number of completed processes in a time unit (needs to be maximized)
  • Turnaround Time : Time period from process submission to process completion (needs to be minimized)
  • Waiting time : the sum of the time spent waiting in the ready queue (needs to be minimized)
  • Response time : the time from submitting the request to generating the first response (needs to be minimized)

These scheduling criteria cannot all be satisfied. When one criterion is satisfied, another criterion may be lost, such as:

  • CPU utilization and response time: The CPU is idle during context switching, so when the number of context switches is reduced, the CPU utilization increases, but this may lead to an increase in the response time of a process.
  • Average turnaround time and maximum wait time: Executing the shortest task first can reduce turnaround time, but the wait time for such a long task increases
  • I/O device utilization and CPU utilization: Maximizing CPU utilization can be achieved by reducing context switching. The utilization of I/O devices is maximized by scheduling tasks that are ready for I/O operations as much as possible, which can lead to context switches.

Scheduling Algorithm

Before explaining the scheduling algorithm, we need to review the ready queue again : the ready queue contains the processes in the system that are ready and ready to run in memory. The queue is usually implemented as a linked list, with the head node pointing to pointers to the first and last PCB blocks, and each PCB block includes a pointer field to the next PCB.


First come first serve algorithm

First come, first served (FCFS) algorithm , that is, a simple FIFO queue, the process that requests the CPU first is allocated to the CPU, but the average waiting time is long, and there are many IO constraints due to waiting for a CPU-constrained process The process cannot be executed and has a convoy effect

The algorithm is non-preemptive


Shortest Job First Scheduling

Shortest job-first scheduling (shortest-job-first(sjf)) associates each process with the next CPU interval segment. When the CPU is idle, it will be assigned to the process with the shortest next CPU interval . If the two process intervals are the same , the FCFS scheduling is used.
This algorithm is optimal , but a difficulty lies in how to know the length of the next CPU interval. Therefore, this algorithm is often used for long-term scheduling, and the next CPU of a process is judged by the long-term exponential average of a CPU interval of a process. The length of the interval. (As a result, the algorithm cannot be used for short-term CPU scheduling)

The algorithm may be preemptive or non-preemptive, preemptive sjf (also known as shortest-remainint-time-first scheduling) preempts the currently running process, nonpreemptive sjf allows the current process to finish its CPU interval.

Exponential mean: θn+1 = atn + (1-a)θnt is the current information, θ is the historical information, a controls the weights, a common value is 1/2
write picture description here


priority scheduling

In the priority scheduling algorithm , each process has a priority associated with it, the task with the highest priority is assigned to the CPU, and tasks with the same priority are scheduled according to FCFS.

sjf is a special case of the priority scheduling algorithm. Its priority is the inverse of the next (predicted) CPU interval, the larger the CPU interval, the lower the priority.

The priority is usually a number in a fixed range, such as 0~7, 0~4095,

Depending on the system, it is possible to use high number high priority and low number high priority.

There are two ways to define priorities, external definition/internal definition

  • Internally defined priority (internally defined priority) uses some measurement data to calculate process priority, such as time limit, memory requirements, number of open files, how many IO requests, etc.
  • External priorities are defined by criteria outside the operating system, including process importance, cost, political factors, etc.

Like the preemption and non-preemption in the sjf algorithm, the priority scheduling algorithm also has the problem of preemption and non-preemption. The preemptive priority scheduling algorithm preempts the CPU, and the non-preemptive priority scheduling algorithm adds a new process to the head of the ready queue.

A major problem with this algorithm: indefinite blocking/starvation , because the priority scheduling algorithm causes low priority processes to wait indefinitely for the CPU. One of the solutions is aging , which is to gradually increase the priority of processes that have been waiting in the system for a long time.


Rotary scheduling

The round-robin RR scheduling algorithm is specially designed for time- sharing systems . It is similar to FCFS, but adds preemption to switch processes.

The implementation idea is: define a small time unit/time slice (10-100ms), each process is allocated a CPU of no more than one time unit, and after the time is up, the process will be added to the tail of the ready queue through an interrupt ( if the process does not complete the task)

In terms of performance, if the time slice is large, its performance is close to FCFS. If the time slice is small, the algorithm will cause a large number of context switches , so the time slice must be large enough relative to the time of context switching.

Now let's assume that the time slice time of the round-robin scheduling is 10ms, there are 10 IO-constrained processes, and each 1ms CPU calculation executes 10ms of IO requests.

The question is: in a time slice, a process takes 1ms to complete the CPU calculation, will the process wait for 9ms to complete this time slice, or will it directly context switch to the next process?


Algorithm evaluation (to be completed)

  • Analytical Evaluation
    • Deterministic Model Approach
  • queuing model
  • simulation
  • accomplish

Guess you like

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