Chapter 6 :CPU Scheduling
Article Directory
- Chapter 6 :CPU Scheduling
- CPU-bound and I/O-bound
- CPU Scheduler
- Circumstances that scheduling may take place
- Preemptive vs. Non-preemptive
- Dispatcher
- Scheduling Criteria
- Test
- Scheduling Algorithms
- FCFS
- SJF
- How do we know the Next CPU Burst?
- SJF Problems
- Priority Scheduling
- Aging
- Round Robin (RR)
- Multilevel Queue
- Multilevel Feedback Queue
- Multiple-Processor Scheduling
- Thread Scheduling
Basic Concepts: Maximum CPU utilization obtained with multiprogramming
CPU-bound and I/O-bound
A CPU-bound process might have a few very long CPU bursts.
An I/O-bound process typically has many short CPU bursts
CPU Scheduler
The CPU scheduler selects a process from the ready queue, and allocates the CPU to it.
Circumstances that scheduling may take place
- A process switches from the running state to the wait state (e.g., doing for I/O)
- process switches from the running state to the ready state (e.g., an interrupt occurs)
- A process switches from the wait state to the ready state (e.g., I/O completion)
- A process terminates
Preemptive vs. Non-preemptive
Non-preemptive scheduling: scheduling occurs when a process voluntarily enters the wait state (case 1) or terminates (case 4). Simple, but very inefficient
Preemptive scheduling: scheduling occurs in all possible cases. Mutual exclusion may be violated. Mutual exclusion may be violated.
Real-time OS must implement preemptible scheduling
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
◆switching context
◆switching to user mode
◆jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running. The time for context switching depends on the hardware
Scheduling Criteria
◆CPU utilization : Normally 40% is lightly loaded and 90% or higher is heavily loaded
Throughput: The number of processes completed per time unit, Higher throughput means more jobs get done. It is more objective to the same process set
◆Turnaround time :The time period between job submission to completion ,From a user’s point of view, turnaround time is more important than CPU utilization and throughput
◆Waiting time : the sum of the periods that a process spends waiting in the ready queue
◆Response time: The time from the submission of a request (in an interactive system) to the first response is called response time
Test
Answer: ABDE
Judging the type of process is to observe the length of the CPU burst. Long CPU burst has a long CPU time. There are many Short CPU bursts for frequent IO. The length of the IO burst depends on the device, not because it is IO bound burst. Very long
Jitter jitter, the smaller the jitter, the more predictable
Scheduling Algorithms
◆First-Come, First-Served (FCFS)
◆Shortest-Job-First (SJF)
◆Priority
◆Round-Robin
◆Multilevel Queue
◆Multilevel Feedback Queue
FCFS
The process that requests the CPU first is allocated the CPU first.
Using a queue.
FCFS is not preemptive
Convoy effect :short process behind long process, CPU utilization may be low. 护航效应
SJF
When a process must be selected from the ready queue, the process with the smallest next CPU burst is selected. Thus, the processes in the ready queue are sorted in CPU burst length.
SJF can be non-preemptive or preemptive!!!
◆nonpreemptive
◆preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF).
SJF is provably optimal(最优的) – gives minimum average waiting time for a given set of processes
How do we know the Next CPU Burst?
Without a good answer to this question, SJF cannot be used for CPU scheduling.
We try to predict the next CPU burst by using the length of previous CPU bursts, using exponential averaging.
how to do:
SJF Problems
some long jobs may not have a chance to run at all. This is starvation.
Priority Scheduling
Priority may be determined internally or externally:
◆ internal priority:
determined by time limits, memory requirement, # of files, and so on. (Determined by internal nature)
◆ external priority:
not controlled by the OS (eg, importance of the process) (artificially set)
The scheduler always picks the process (in ready queue) with the highest priority to run
- Priority scheduling can be non-preemptive or preemptive. if the newly arrived process has a higher priority than the running one, the latter is preempted
Indefinite block (or starvation) may occur: a low priority process may never have a chance to run–>solution: Aging
Aging
Aging: gradually increases the priority of processes that wait in the system for a long time.
Round Robin (RR)
All processes in the ready queue is a FIFO list. When the CPU is free, the scheduler picks the first and lets it run for one time quantum.
- If time quantum is too large, RR reduces to FCFS
- If time quantum is too small, RR becomes processor sharing
- Context switching may affect RR’s performance .Shorter time quantum means more context switches
- Turnaround time also depends on the size of time quantum. In general, 80% of the CPU bursts should be shorter than the time quantum
- Typically, higher average turnaround than SJF, but better response.
Multilevel Queue
Each process is assigned permanently to one queue based on some properties of the process .
Each queue has its own scheduling algorithm,
- foreground – RR
- background – FCFS
Fixed priority scheduling: Possibility of starvation.
Time slice: each queue gets a certain amount of CPU time which it can schedule amongst its processes( i.e., 80% to foreground in RR, 20% to background in FCFS )
Multilevel Feedback Queue
Multilevel queue with feedback scheduling is similar to multilevel queue; however, it allows processes to move between queues,aging can be implemented this way
If a process uses more (resp., less) CPU time, it is moved to a queue of lower (resp., higher) priority.
That is to say : ,I/O-bound (resp., CPU-bound) processes will be in higher (resp., lower) priority queues.
More precisely, Multilevel Feedback Queue can be said to be a scheduling framework
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available.
- Homogeneous processors
- Load sharing
- Asymmetric multiprocessing
Thread Scheduling
- Local Scheduling – threads library decides which thread to put onto an available LWP
- Global Scheduling – kernel decides which kernel thread to run next
Classic Test:
**Consider a multi-level feedback queue in a single-CPU system. The first level (queue 0) is given a quantum of 8 ms, the second one a quantum of 16 ms, the third is scheduled FCFS. Assume jobs arrive all at time zero with the following job times (in ms): 4, 7, 12, 20, 25 and 30, respectively. Assume the context switch overhead is zero unless otherwise stated. **
•Show the Gantt chart for this system.
•Compute the average waiting and turnaround time.
•Suppose the context switch overhead is 1 ms. Compute the average turnaround time
Don't spray ugly characters, haha
OS Review Chapter1: Introduction
OS Review Chapter 2: Computer-System Structures
OS Review Chapter 3: Operating-System Structures
OS Review Chapter 4: Process
OS Review Chapter 5: Thread