CPU SCHEDULING
PROCESS
&
Shivbhawan Varma
VLSI and Embedded System
GUJARAT TECHNOLOGICAL UNIVERSITY
AHMEDABAD
&
C-DAC, PUNE
Diagram of Process State
Process Control Block (PCB)
 Information associated with each
process.
 Process state
 Program counter
 CPU registers
 CPU scheduling information
 Memory-management information
 Accounting information
 I/O status information
Contact switching
Representation of Process Scheduling
CONTEXT SWITCH
 Save the state of the old process
 Load the saved state for the new process.
 Context-switch time is overhead; the system does
no useful work while switching.
 Time dependent on hardware support.
CPU Scheduler
 A CPU scheduler, running in the dispatcher, is
responsible for selecting of the next running process.
 Based on a particular strategy
 When does CPU scheduling happen?
 A process switches from the running state to
waiting state (e.g. I/O request)
 A process switches from the running state to
the ready state.
 A process switches from waiting state to
ready state (completion of an I/O operation)
 A process terminates
Scheduling queues
 CPU schedulers use various queues in the scheduling
process:
 Job queue: consists of all processes
 All jobs (processes), once submitted, are in the job queue.
 Some processes cannot be executed (e.g. not in memory).
 Ready queue
 All processes that are ready and waiting for execution are in the
ready queue.
 Usually, a long-term scheduler/job scheduler selects processes
from the job queue to the ready queue.
 CPU scheduler/short-term scheduler selects a process from the
ready queue for execution.
 Simple systems may not have a long-term job scheduler
Performance metrics for CPU scheduling
 CPU utilization: percentage of the time that
CPU is busy.
 Throughput: the number of processes
completed per unit time
 Turnaround time: the interval from the time
of submission of a process to the time of
completion.
 Wait time: the sum of the periods spent waiting
in the ready queue
 Response time: the time of submission to the
time the first response is produced
• fairness: It is important, but harder to define quantitatively.
Goal of CPU scheduling
 Goal:
 Maximize CPU utilization, Throughput, and fairness.
 Minimize turnaround time, waiting time, and response time.
Methods for evaluating CPU scheduling
algorithms
 Deterministic Modelling
 Take a predetermined workload
 Run the scheduling algorithm manually
 Find out the value of the performance metric that you care
about.
 Not the best for practical uses, but can give a lot of inside
about the scheduling algorithm.
 Help understand the scheduling algorithm, as well as it
strengths and weaknesses
Deterministic modeling example:
 Suppose we have processes A, B, and C,
submitted at time 0
 We want to know the response time, waiting
time, and turnaround time of process A
A B C A B C A C A C Time
response time = 0
+ +wait time
turnaround time
Gantt chart: visualize how processes execute.
Deterministic modeling example
 Suppose we have processes A, B, and C,
submitted at time 0
 We want to know the response time, waiting
time, and turnaround time of process B
A B C A B C A C A C Time
response time
+wait time
turnaround time
Deterministic modeling example
 Suppose we have processes A, B, and C,
submitted at time 0
 We want to know the response time, waiting
time, and turnaround time of process C
A B C A B C A C A C Time
response time
+ ++wait time
turnaround time
Scheduling Policies
 FIFO (first in, first out)
 Round robin
 SJF (shortest job first)
 Priority Scheduling
 Multilevel feedback queues
 Many more...
FIFO
 FIFO: assigns the CPU based on the order of
requests
 Non-preemptive: A process keeps running on a CPU
until it is blocked or terminated
 Also known as FCFS (first come, first serve)
- Simple
- Short jobs can get stuck behind long jobs
- Turnaround time is not ideal
Round Robin
 Round Robin (RR) periodically releases the
CPU from long-running jobs
 Based on timer interrupts so short jobs can get a fair share
of CPU time
 Pre-emptive: a process can be forced to leave its running
state and replaced by another running process
 Time slice: interval between timer interrupts
More on Round Robin
 If time slice is too long
 Scheduling degrades to FIFO
 If time slice is too short
 Throughput suffers
 Context switching cost dominates
FIFO vs. Round Robin
 With zero-cost context switch, is RR always better
than FIFO?
FIFO vs. Round Robin
 Suppose we have three jobs of equal length
A B C A B C TimeA B C
turnaround time of A
turnaround time of B
turnaround time of C
Round Robin
A B TimeC
turnaround time of A
turnaround time of B
turnaround time of C
FIFO
FIFO vs. Round Robin
 Round Robin
+ Shorter response time
+ Fair sharing of CPU
- Not all jobs are preemptable
- Not good for jobs of the same length
- More precisely, not good in terms of the turnaround time.
Shortest Job First (SJF)
 SJF runs whatever job puts the least demand on
the CPU, also known as STCF (shortest time to
completion first)
+ Probably optimal in terms of turn-around time .
+ Great for short jobs
+ Small degradation for long jobs
SJF Illustrated
A B TimeC
turnaround time of A
turnaround time of B
turnaround time of C
Shortest Job First
response time of A = 0
response time of B
response time of C
wait time of A = 0
wait time of B
wait time of C
Drawbacks of Shortest Job First
- Starvation: constant arrivals of short jobs can
keep long ones from running
- There is no way to know the completion time of
jobs (most of the time)
 Some solutions
 Ask the user, who may not know any better
 If a user cheats, the job is killed
Priority Scheduling (Multilevel
Queues)
 Priority scheduling: The process with the
highest priority runs first
 Priority 0:
 Priority 1:
 Priority 2:
 Assume that low numbers represent high priority
A
B
C
A B TimeC
Priority Scheduling
Multilevel Feedback Queues
 Multilevel feedback queues use multiple
queues with different priorities
 Round robin at each priority level
 Run highest priority jobs first
 Once those finish, run next highest priority, etc
 Jobs start in the highest priority queue
 If time slice expires, drop the job by one level
 If time slice does not expire, push the job up by one level
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
A B C
time = 0
Time
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
B C
time = 1
A
A Time
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
C
time = 2
A B
A B Time
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
C
time = 3
A B
A B C Time
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
C
time = 3
A B
A B C Time
suppose process A is blocked on an I/O
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
C
time = 3
A
B
A B C Time
suppose process A is blocked on an I/O
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
C
time = 3
A
B
A B C Time
suppose process A is blocked on an I/O
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
time = 5
BA B C Time
C
A
suppose process A is returned from an I/O
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
time = 6
BA B C Time
C
A
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
time = 8
BA B C TimeA C
C
Multilevel Feedback Queues
 Priority 0 (time slice = 1):
 Priority 1 (time slice = 2):
 Priority 2 (time slice = 4):
time = 9
BA B C TimeA C C
Thank you..

Process and CPU scheduler

  • 1.
    CPU SCHEDULING PROCESS & Shivbhawan Varma VLSIand Embedded System GUJARAT TECHNOLOGICAL UNIVERSITY AHMEDABAD & C-DAC, PUNE
  • 2.
  • 3.
    Process Control Block(PCB)  Information associated with each process.  Process state  Program counter  CPU registers  CPU scheduling information  Memory-management information  Accounting information  I/O status information
  • 4.
  • 5.
  • 6.
    CONTEXT SWITCH  Savethe state of the old process  Load the saved state for the new process.  Context-switch time is overhead; the system does no useful work while switching.  Time dependent on hardware support.
  • 7.
    CPU Scheduler  ACPU scheduler, running in the dispatcher, is responsible for selecting of the next running process.  Based on a particular strategy  When does CPU scheduling happen?  A process switches from the running state to waiting state (e.g. I/O request)  A process switches from the running state to the ready state.  A process switches from waiting state to ready state (completion of an I/O operation)  A process terminates
  • 8.
    Scheduling queues  CPUschedulers use various queues in the scheduling process:  Job queue: consists of all processes  All jobs (processes), once submitted, are in the job queue.  Some processes cannot be executed (e.g. not in memory).  Ready queue  All processes that are ready and waiting for execution are in the ready queue.  Usually, a long-term scheduler/job scheduler selects processes from the job queue to the ready queue.  CPU scheduler/short-term scheduler selects a process from the ready queue for execution.  Simple systems may not have a long-term job scheduler
  • 9.
    Performance metrics forCPU scheduling  CPU utilization: percentage of the time that CPU is busy.  Throughput: the number of processes completed per unit time  Turnaround time: the interval from the time of submission of a process to the time of completion.  Wait time: the sum of the periods spent waiting in the ready queue  Response time: the time of submission to the time the first response is produced
  • 10.
    • fairness: Itis important, but harder to define quantitatively.
  • 11.
    Goal of CPUscheduling  Goal:  Maximize CPU utilization, Throughput, and fairness.  Minimize turnaround time, waiting time, and response time.
  • 12.
    Methods for evaluatingCPU scheduling algorithms  Deterministic Modelling  Take a predetermined workload  Run the scheduling algorithm manually  Find out the value of the performance metric that you care about.  Not the best for practical uses, but can give a lot of inside about the scheduling algorithm.  Help understand the scheduling algorithm, as well as it strengths and weaknesses
  • 13.
    Deterministic modeling example: Suppose we have processes A, B, and C, submitted at time 0  We want to know the response time, waiting time, and turnaround time of process A A B C A B C A C A C Time response time = 0 + +wait time turnaround time Gantt chart: visualize how processes execute.
  • 14.
    Deterministic modeling example Suppose we have processes A, B, and C, submitted at time 0  We want to know the response time, waiting time, and turnaround time of process B A B C A B C A C A C Time response time +wait time turnaround time
  • 15.
    Deterministic modeling example Suppose we have processes A, B, and C, submitted at time 0  We want to know the response time, waiting time, and turnaround time of process C A B C A B C A C A C Time response time + ++wait time turnaround time
  • 16.
    Scheduling Policies  FIFO(first in, first out)  Round robin  SJF (shortest job first)  Priority Scheduling  Multilevel feedback queues  Many more...
  • 17.
    FIFO  FIFO: assignsthe CPU based on the order of requests  Non-preemptive: A process keeps running on a CPU until it is blocked or terminated  Also known as FCFS (first come, first serve) - Simple - Short jobs can get stuck behind long jobs - Turnaround time is not ideal
  • 18.
    Round Robin  RoundRobin (RR) periodically releases the CPU from long-running jobs  Based on timer interrupts so short jobs can get a fair share of CPU time  Pre-emptive: a process can be forced to leave its running state and replaced by another running process  Time slice: interval between timer interrupts
  • 19.
    More on RoundRobin  If time slice is too long  Scheduling degrades to FIFO  If time slice is too short  Throughput suffers  Context switching cost dominates
  • 20.
    FIFO vs. RoundRobin  With zero-cost context switch, is RR always better than FIFO?
  • 21.
    FIFO vs. RoundRobin  Suppose we have three jobs of equal length A B C A B C TimeA B C turnaround time of A turnaround time of B turnaround time of C Round Robin A B TimeC turnaround time of A turnaround time of B turnaround time of C FIFO
  • 22.
    FIFO vs. RoundRobin  Round Robin + Shorter response time + Fair sharing of CPU - Not all jobs are preemptable - Not good for jobs of the same length - More precisely, not good in terms of the turnaround time.
  • 23.
    Shortest Job First(SJF)  SJF runs whatever job puts the least demand on the CPU, also known as STCF (shortest time to completion first) + Probably optimal in terms of turn-around time . + Great for short jobs + Small degradation for long jobs
  • 24.
    SJF Illustrated A BTimeC turnaround time of A turnaround time of B turnaround time of C Shortest Job First response time of A = 0 response time of B response time of C wait time of A = 0 wait time of B wait time of C
  • 25.
    Drawbacks of ShortestJob First - Starvation: constant arrivals of short jobs can keep long ones from running - There is no way to know the completion time of jobs (most of the time)  Some solutions  Ask the user, who may not know any better  If a user cheats, the job is killed
  • 26.
    Priority Scheduling (Multilevel Queues) Priority scheduling: The process with the highest priority runs first  Priority 0:  Priority 1:  Priority 2:  Assume that low numbers represent high priority A B C A B TimeC Priority Scheduling
  • 27.
    Multilevel Feedback Queues Multilevel feedback queues use multiple queues with different priorities  Round robin at each priority level  Run highest priority jobs first  Once those finish, run next highest priority, etc  Jobs start in the highest priority queue  If time slice expires, drop the job by one level  If time slice does not expire, push the job up by one level
  • 28.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): A B C time = 0 Time
  • 29.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): B C time = 1 A A Time
  • 30.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): C time = 2 A B A B Time
  • 31.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): C time = 3 A B A B C Time
  • 32.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): C time = 3 A B A B C Time suppose process A is blocked on an I/O
  • 33.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): C time = 3 A B A B C Time suppose process A is blocked on an I/O
  • 34.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): C time = 3 A B A B C Time suppose process A is blocked on an I/O
  • 35.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): time = 5 BA B C Time C A suppose process A is returned from an I/O
  • 36.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): time = 6 BA B C Time C A
  • 37.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): time = 8 BA B C TimeA C C
  • 38.
    Multilevel Feedback Queues Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4): time = 9 BA B C TimeA C C
  • 39.