Operating System Notes-Chapter 8 Scheduling

1. Background

1.1 Context switch

  • Switch the current task of the cpu from one process/thread to another
  • Save the execution status of the current process/thread in PCB/TCP
  • Read the context of the next process/thread

1.2 CPU scheduling

  • Pick a process/thread from the ready queue as the next process/thread that the CPU will run
  • Scheduler: select the kernel function of the thread/thread (through some scheduling strategies)
  • When to schedule
    • Switching from one state to another state (especially state changes related to operation) triggers scheduling

1.3 Conditions for the kernel to run the scheduler (satisfy one of them)

  • A process switches from a running state to a waiting state
  • A process is terminated
  • User mode process
    • Not preemptable
      • The scheduler must wait for the end of the time: to ensure that the process cannot be interrupted once it is started -> other processes must wait for the process to execute before they can execute -> may cause inefficiency
      • For example, a process in Windows has to be in an organized/waiting state for some reason. At this time, other processes must also wait for the process to execute
    • Can preempt
      • The operating system can decide to execute the process of switching
      • The scheduler is executed after the interrupt response
      • The current process is switched from running to ready, or a process is switched from waiting to ready
      • The current process can be swapped out
  • The above preemption strategy is aimed at, and there is also "whether it can be preempted" in the kernel
    • A process executes a system call. If the process itself does not cause the process to be in a waiting state (continuous running), then there will be no preemption (switching) -> the kernel is not preemptible
    • A certain system call is initiated by a certain process. Due to the occurrence of a certain event, the current kernel process needs to be switched to a higher priority process for execution. Once the system call returns, it may go to another process -> preemptive Kernel
  • Early operating systems did not support user-level preemption. The basic support now makes the system more flexible and efficient

1.4 CPU scheduling time

2. Scheduling principles

The basis for selecting the process.

2.1 Execution model: The program alternates between CPU burst and I/O

  • For a process, the operating curve of the CPU is generally wavy. This is because the CPU usage rate is high during frequent operations, and when I/O is read (slower), the CPU is in an idle state, and the occupancy rate drops. Therefore, the ideal state is to execute other processes when the CPU occupancy rate is low, so that the execution efficiency of the entire system is high
  • Each scheduling decision is about which work to give to the CPU in the next CPU burst
  • Under the time slicing mechanism, the thread may be forced to give up the CPU before ending the current CPU burst

2.2 Quantifiable evaluation indicators

  • CPU usage: the percentage of time the CPU is busy
  • Throughput: the number of processes completed in a unit of time
  • Turnaround time: the time spent by a process from initialization to completion, including all waiting time (waiting time + execution time)
  • Waiting time: the total time the process is in the ready queue (refers to the ready state process, not the time waiting for the blocked process to wake up)
  • Response time: the total time taken from the submission of a request to the first response

2.3 Vague indicators-"faster service"

  • Low latency
  • High bandwidth

There are contradictions between the evaluation standards, and a compromise must be made, that is, the intermediate state of each evaluation index needs to be selected to meet different needs.

2.4 Scheduling strategy

The CPU usage and throughput should be as large as possible, and the turnaround time, waiting time, and response time should be as small as possible

  • Reduce response time: Process the user's output in time and provide the output to the user as soon as possible
  • Reduce fluctuations in average response time: In interactive systems, predictability is more important than high variance and low average
  • Reduce throughput-two aspects
    • Reduce overhead (operating system overhead, context switching)
    • Efficient use of system resources (CPU, I/O equipment)
  • Reduce waiting time: reduce the waiting time of each process

2.5 Throughput vs. Latency

  • Low-latency scheduling increases interactive performance: if the mouse is moved, but the cursor on the screen does not move, the user may restart the computer
  • But the operating system needs to ensure that the throughput is not affected: I want to end long-time programming, so the operating system must schedule from time to time, and there are many interactive tasks in time
  • Throughput is the computing bandwidth of the operating system
  • Response time is the calculation delay of the operating system
  • For example, in Linux, the scheduling algorithms for Desktop and service are different

2.6 The goal of fairness

  • Ensure that processes can be served by the CPU fairly (waiting time, execution time
  • In order to solve the problem that each process in the multi-process system can get the CPU service in time
  • Fairness usually increases the average response time

3. Scheduling algorithm of general-purpose computer

FCFS(first come, first served)

  • If the process is blocked during execution, the next one in the queue will get the CPU
  • The execution time of the previous process is long, which will cause the subsequent process to wait a long time to process -> the response time of the entire system is slow
  • Advantages: simple
  • Disadvantages:
    • The average waiting time fluctuates greatly
    • Tasks that take less time may be ranked behind tasks that take longer
    • May cause overlap processing between I/O and CPU: CPU-intensive processes will cause I/O devices to be idle, and I/O-intensive processes are also waiting

Short process priority (short job priority) short remaining time priority SPN (SJF) SRT : Optimization of basic scheduling

Put short execution time first -> short process/short task first

  • Enlist tasks according to the predicted completion time
  • Can be preemptible or non-preemptable
    • Preemptible is also called SRT (shortest remaining time): when the executing process takes longer than the execution time of the newly added process, a switch occurs
    • Not preemptible: SPN
  • Get the optimal average waiting time

problem:

  • May cause hunger
    • A continuous stream of short tasks will starve long tasks
    • When short tasks are available, the CPU time of any long task will increase the average waiting time
  • Need to predict the future
    • How to estimate the duration of the next CPU burst: simple solution -> ask the user -> kill the process if the user cheats -> if the user does not know?
    • Use history to predict the next moment of execution through formulas

HRRN (highest response ratio next)

  • Consider the waiting time more than the previous algorithm
  • Improve on the basis of SPN scheduling
  • Not preemptable
  • Watch how long the process has been waiting
  • Prevent indefinite postponement
  • official:

R = (w + s)/s
w: waiting time waiting time; s: service time execution time; the larger the R, the longer the waiting time, select the process with the highest R value

  • Still need to estimate the execution time of the program

Round Robin

  • Distribute processors in discrete units called quantum (or time slice)
  • When the time slice ends, switch to the next prepared process
  • For example, if the time slice is set to 20, the process p1 takes 49, p2 takes 20, and p3 takes 50. In the first round of execution, p1 has 29 left, p2 ends, and p3 has 30 left. Continue to execute according to the time slice
  • The main feature is fairness, and the average waiting time is relatively large

Evaluation

  • Cost: additional context switching
  • The time slice size is different, the average waiting time is not necessarily the same
  • If the time quantum is too large: the waiting time is too long; the extreme case degenerates into FCFS
  • If the time quantum is too small: response is fast; throughput is affected due to a large amount of context switching overhead
  • Goal: choose an appropriate time quantum; rule of thumb: maintain context switching overhead within 1%

Optimization Strategy:

  • The ready queue is divided into independent queues (eg foreground (interactive), background (batch processing))
  • Each queue has its own scheduling strategy (eg foreground-RR, background-FCFS)
  • Scheduling must be done between queues
    • Fixed priority: Process the foreground first and then process the background; it may lead to starvation
    • Time slicing: Each queue can get a total CPU time that can schedule other processes; eg 80% for the foreground using RR; 20% for the background using FCFS

Multilevel Feedback Queues

  • A process can move in different queues: dynamically adjust according to the characteristics of the process
  • For example: n-level priority-priority scheduling in all levels, RR in each level
    • The time quantum size increases as the priority level increases
    • If the task is not completed in the current quantum of time, it will drop to the next priority
  • advantage:
    • The priority of CPU-intensive tasks drops quickly
    • I/O-intensive tasks stay at high priority (interactive processes get faster response)

Fair Share Scheduling

FFS controls user access to system resources

  • Some user groups are more important than others
  • Ensure that unimportant groups cannot monopolize resources
  • Unused resources are allocated in proportion to the resources allocated by each group
  • Groups that do not meet the resource usage target get higher priority
  • Example: Linux

Evaluation of various scheduling algorithms:

  • Deterministic model: Determine a workload, and then calculate the performance of each algorithm
  • Queue model: a mathematical method used to handle random workloads
  • Implementation/simulation: build a system that allows the algorithm to run on actual data; the most flexible/general

to sum up

  • FCFS: Unfair, poor average waiting time
  • SPN/SRT:
    • Unfair, but the average waiting time is the smallest
    • Need to accurately predict the calculation time
    • May cause hunger
  • HRRN:
    • Improved based on SPN scheduling
    • Not preemptable
  • RR: Fair, but the average waiting time is poor
  • MLFQ: similar to SPN
  • FFS: Fairness is the first element

4. Real-time scheduling

Real-time system

  • Real-time systems are generally used in industrial control, such as the operating environment of trains, machine tools, and embedded factories, and the program needs to be completed in a prescribed time.
  • Definition: an operating system whose correctness depends on its time and function
  • Performance indicators: timeliness of time constraints (deadlines); speed and average performance are relatively unimportant
  • Main feature: predictability of time constraints

Hard real-time system : important tasks need to be completed within the guaranteed time, and
soft real-time system : important processes need to have a higher priority, try to complete them, not necessary

Task (unit of work): one calculation, one file reading, one information transfer, etc.
Attributes: resources needed to make progress; timing parameters

Insert picture description here

Schedulability

Set up a real-time system that meets deadline requirements

  • Determine the order of execution of real-time tasks
  • Two types of scheduling algorithms
    • Static priority scheduling
    • Dynamic priority scheduling

RM (Rate Monotonic) rate monotonic scheduling-static priority scheduling algorithm

  • Best static priority scheduling
  • Prioritize through cycles
  • The shorter the cycle, the higher the priority
  • Task with the shortest execution cycle

EDF (Earliest Deadline First) earliest deadline scheduling-dynamic priority scheduling algorithm

  • Best dynamic priority scheduling
  • The earlier the deadline, the higher the priority
  • Perform the earliest task of Deadline

5. Multi-processor scheduling

Multiprocessor CPU scheduling is more complicated

  • Multiple identical single processors form a multiprocessor
  • Advantages: load sharing

Symmetric Multiprocessor (SMP)

  • Each processor runs its own scheduler
  • Need to be synchronized in the scheduler

6. Priority inversion

The phenomenon that low-priority tasks affect high-priority tasks

For example: a>b>c, when the execution reaches c, a needs to wait for the execution of c to be executed, and the priority of c is lower than b, b can preempt c, then it will cause the execution of c to switch to b and finally to a. The operating system thinks the current system is unstable and restarts

Solution:

  • Priority inheritance: The priority of low-priority tasks inheriting high-priority tasks depends on their shared resources. (Improving the priority of c is equivalent to a, ensuring that b cannot preempt c
  • Priority ceiling: Perform resource statistics from the beginning, and prioritize resources. The priority of the resource is the same as the priority of the highest priority task among all tasks that can lock the resource
    • Unless the priority is higher than the priority upper limit of all locked resources in the system, the task will be blocked when it tries to execute the critical section
    • The task holding the highest priority cap semaphore lock will inherit the priority of the task blocked by the lock

Guess you like

Origin blog.csdn.net/MaoziYa/article/details/105575044