Course Content#
Process Scheduling#
- Process scheduling is a kernel subsystem that users cannot manage
- The main task of process scheduling is to decide which ready state process runs first
- A ready process is a non-blocking process that meets the running conditions and does not need to block and wait
- A blocking process is a process that is sleeping [not using CPU] and needs to be awakened by the kernel
Three-State Model#
- Blocking, Ready, Running
-
- Blocking does not occupy system resources; Ready needs to wait for kernel scheduling; Running blocks when there is an IO request or waiting for an event
👉 Five-State Model
-
- Adds New and Terminated states
From Single Task to Multi-Task#
- DOS is a single-task operating system, running only one process at a time
- Processes run in an interleaved manner, similar to multi-tasking
- Multi-processor systems can achieve true parallelism
- Concurrency VS Parallelism
- Concurrency: Many tasks are in progress within a time period
- Parallelism: The number of CPUs determines how many tasks can run simultaneously
- The core of scheduling: which process to run and for how long
Time Slice#
Strongly influences the system's global behavior and performance
- Too long
- 👍: Increases system throughput and global performance
- ×: Reduces concurrent execution; users feel noticeable latency [decreased responsiveness, too many applications can also lead to longer scheduling intervals]
- Too short
- 👍: Improves interactive performance
- ×: A lot of time spent on scheduling; the performance improvement brought by temporal locality is greatly reduced
- Temporal locality: Caching, memory [cache is built up over time]
- Spatial locality: Preparing data from nearby spaces in advance
- Implies learning and prediction
- Therefore, the length of the time slice is difficult to determine; the solution is to simply not use time slices, replaced by a Completely Fair scheduling algorithm
- The system adjusts based on the ready queue and priority strategy
- The allocation of scheduling time and priority is essentially to better serve users and is not unfair
- See below for details—Completely Fair Scheduler
Processor-Bound and IO-Bound Processes#
Divided based on the characteristics of CPU usage, depending on whether the processor or IO is used more
- Processor-Bound
- [Processes that consistently consume the available time slice]
- Require a large amount of CPU resources and will consume all CPU allocated by the scheduler
- For example: while(1); can easily reach 100% CPU usage
- Requirements for time slice
- Expect longer time slices to maximize cache hits and complete tasks quickly [will not waste resources]
- IO-Bound
- [Processes that spend most of their time in a blocking state or waiting for resources]
- Often block on file IO operations: files, network, keyboard, mouse [like listening to music, typing]; or do nothing except request the kernel to perform I/O operations [like cp]
- Requirements for time slice
- Need shorter time slices; the quicker they are scheduled, the more seamless the experience feels
- Processes will only run for a very short time and then block on kernel resources; will use interrupts
Scheduler Classification#
Cooperative Scheduler#
[Non-intervention type, low practicality]
- Processes will run until they finish themselves
- The operating system does not intervene at all
Preemptive Scheduler#
[Time slice type]
- The kernel allocates a time slice to the process; when the time slice is used up, the process is suspended [not using any resources], and other processes are executed
- If there are no other ready processes, the kernel reallocates a time slice to the already executed process
- The scheduler arranges the order and duration of process execution
- [PS]
- New state enters the ready queue, terminated state exits the ready queue [Five-State Model]
- Considers priority, all processes have a chance to run
Completely Fair Scheduler#
[Non-time slice type]
CFS: Completely Fair Scheduler
- For N processes, each process is allocated 1 / N of the processor time
- Priority and weight
- The higher the priority, the smaller the value, the greater the weight
- Default priority is 0, weight is 1
- Target Latency: Fixed period for scheduling
- Minimum Granularity: Avoids too small target latency leading to excessively short run times for each process
👉 Fair Quota
Tips#
- I am a CPU: This world is slow! Dead!—— WeChat Official Account
- An interesting article to understand the speed gap between CPU and hard disk, network