ِ‫ن‬ ٰ‫م‬ْ‫ح‬َّ‫الر‬ ِ‫هللا‬ ِ‫م‬ْ‫س‬ِ‫ب‬ِ‫ْم‬‫ي‬ ِ‫ح‬َّ‫الر‬
GROUP MEMBERS
2611
2625
2608
2609
1. Muhammad Waqas
4. Waqar Iqbal
2. Sheroz Aftab
5. Shahzaib Ali
3. Hammad Ali
2645
Topi
c
CPU Scheduling
Basic Concepts
Maximum CPU utilization obtained with
multiprogramming
CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and I/O
wait
CPU burst distribution
ALTERNATING SEQUENCE OF CPU AND I/O BURSTS
Load store
Add store
Read from file
Store increment
index write to
file
Load store
Add store
Read from file
Wait for I/O
Cpu burst
I/O burst
Cpu
burst
Wait for I/O I/O burst
Cpu
burst
Wait for I/O I/O burst
Histogram of CPU-burst Times
CPU SCHEDULER
 Selects from among the processes in ready queue, and allocates the CPU to
one of them
 Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is non-preemptive
All other scheduling is preemptive
 Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
DISPATCHER
Scheduler: selects one process to run on CPU
Dispatcher: allocates CPU to the selected process, which involves:
1. Switching context
2. switching to user mode
3. jumping to the proper location (in the selected process) and restarting it
Dispatcher should be fast enough.
 Dispatch latency – time it takes for the dispatcher to stop one process and start
another running.
Types of Scheduler
1. Non-preemptive
2. Preemptive
Non-preemptive
• Once a process is allocated the CPU, it does not leave unless:
1. it has to wait, e.g., for I/O request or for a child to terminate
2. it terminates
Preemptive
OS can force (preempt) a process from CPU at anytime
Say, to allocate CPU to another higher-priority process
SCHEDULING CRITERIA
CPU utilization – keep the CPU as busy as possible
• Maximize
Throughput – No of processes that complete their execution per time unit
• Maximize
Turnaround time – amount of time to execute a particular process (time from submission to termination) i.e
(waiting in memory + waiting time in ready queue + executing time in CPU + doing IO)
• Minimize
Waiting time – amount of time a process has been waiting in the ready queue (sum of time waiting in ready
queue)
• Minimize
Response time – amount of time it takes from when a request was submitted until the first response is
produced, not output (for time-sharing environment)
• Minimize
TYPES OF SCHEDULING ALGORITHM
1. First Come, First Served (FCFS)
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
 Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
Process Burst
Time
P1 24
P2 3
P3 3
P1 P2 P3
0 24 27
30
 Suppose that the processes arrive in the order : P2 , P3 , P1
(P1:24,P2:3,P3:3)
The Gantt chart for the schedule is:
 Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
P2 P3 P1
0 3 6
30
2. SHORTEST-JOB-FIRST SCHEDULING
Associate with each process the length of its next CP burst.
Use these lengths to schedule the process with the shortest time
Two schemes:
• Non-preemptive – once CPU given to the process it cannot be preempted
until completes its CPU burst
• 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 optimal – gives minimum average waiting time for a given set of
processes
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 - 4
P1 P3 P2 P4
0 3 7 8 12 16
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
Average waiting time = (9 + 1 + 0 +2)/4 - 3
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
3. PRIORITY SCHEDULING
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest
integer ≡ highest priority)
• Preemptive
• Non-preemptive
 SJF is priority scheduling where priority is the inverse of predicted next
CPU burst time
Problem ≡ Starvation – low priority processes may never execute
Solution ≡ Aging – as time progresses increase the priority of the
process
EXAMPLE OF PRIORITY SCHEDULING
Example of Non-Preemptive SJF
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Average waiting time = 8.2 M - sec
P1 P3 P2 P4
0 1 6 16 18 19
4. ROUND-ROBIN SCHEDULING
Each process gets a small unit of CPU time (time quantum), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added
to the end of the ready queue.
 If there are n processes in the ready queue and the time quantum is q, then
each process gets 1/n of the CPU time in chunks of at most q time units at
once. No process waits more than (n-1)q time units.
 Performance
• q large ⇒ FIFO
• q small ⇒ q must be large with respect to context switch, otherwise overhead
is too high
EXAMPLE OF ROUND-ROBIN SCHEDULING
Process Burst
Time
P1 24
P2 3
P3 3
 Quantum time 4millisec
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26
30
 Typically, higher average turnaround than SJF, but better
response
Thank You
Any Questions
??

CPU Scheduling

  • 1.
    ِ‫ن‬ ٰ‫م‬ْ‫ح‬َّ‫الر‬ ِ‫هللا‬ِ‫م‬ْ‫س‬ِ‫ب‬ِ‫ْم‬‫ي‬ ِ‫ح‬َّ‫الر‬
  • 2.
    GROUP MEMBERS 2611 2625 2608 2609 1. MuhammadWaqas 4. Waqar Iqbal 2. Sheroz Aftab 5. Shahzaib Ali 3. Hammad Ali 2645
  • 3.
    Topi c CPU Scheduling Basic Concepts MaximumCPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait CPU burst distribution
  • 4.
    ALTERNATING SEQUENCE OFCPU AND I/O BURSTS Load store Add store Read from file Store increment index write to file Load store Add store Read from file Wait for I/O Cpu burst I/O burst Cpu burst Wait for I/O I/O burst Cpu burst Wait for I/O I/O burst Histogram of CPU-burst Times
  • 5.
    CPU SCHEDULER  Selectsfrom among the processes in ready queue, and allocates the CPU to one of them  Queue may be ordered in various ways CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is non-preemptive All other scheduling is preemptive  Consider access to shared data Consider preemption while in kernel mode Consider interrupts occurring during crucial OS activities
  • 6.
    DISPATCHER Scheduler: selects oneprocess to run on CPU Dispatcher: allocates CPU to the selected process, which involves: 1. Switching context 2. switching to user mode 3. jumping to the proper location (in the selected process) and restarting it Dispatcher should be fast enough.  Dispatch latency – time it takes for the dispatcher to stop one process and start another running. Types of Scheduler 1. Non-preemptive 2. Preemptive
  • 7.
    Non-preemptive • Once aprocess is allocated the CPU, it does not leave unless: 1. it has to wait, e.g., for I/O request or for a child to terminate 2. it terminates Preemptive OS can force (preempt) a process from CPU at anytime Say, to allocate CPU to another higher-priority process
  • 8.
    SCHEDULING CRITERIA CPU utilization– keep the CPU as busy as possible • Maximize Throughput – No of processes that complete their execution per time unit • Maximize Turnaround time – amount of time to execute a particular process (time from submission to termination) i.e (waiting in memory + waiting time in ready queue + executing time in CPU + doing IO) • Minimize Waiting time – amount of time a process has been waiting in the ready queue (sum of time waiting in ready queue) • Minimize Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) • Minimize
  • 9.
    TYPES OF SCHEDULINGALGORITHM 1. First Come, First Served (FCFS) Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:  Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 Process Burst Time P1 24 P2 3 P3 3 P1 P2 P3 0 24 27 30
  • 10.
     Suppose thatthe processes arrive in the order : P2 , P3 , P1 (P1:24,P2:3,P3:3) The Gantt chart for the schedule is:  Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process P2 P3 P1 0 3 6 30
  • 11.
    2. SHORTEST-JOB-FIRST SCHEDULING Associatewith each process the length of its next CP burst. Use these lengths to schedule the process with the shortest time Two schemes: • Non-preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst • 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 optimal – gives minimum average waiting time for a given set of processes
  • 12.
    Example of Non-PreemptiveSJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 - 4 P1 P3 P2 P4 0 3 7 8 12 16
  • 13.
    Example of PreemptiveSJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (preemptive) Average waiting time = (9 + 1 + 0 +2)/4 - 3 P1 P2 P3 P2 P4 P1 0 2 4 5 7 11 16
  • 14.
    3. PRIORITY SCHEDULING Apriority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority) • Preemptive • Non-preemptive  SJF is priority scheduling where priority is the inverse of predicted next CPU burst time Problem ≡ Starvation – low priority processes may never execute Solution ≡ Aging – as time progresses increase the priority of the process
  • 15.
    EXAMPLE OF PRIORITYSCHEDULING Example of Non-Preemptive SJF Process Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 M - sec P1 P3 P2 P4 0 1 6 16 18 19
  • 16.
    4. ROUND-ROBIN SCHEDULING Eachprocess gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.  If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.  Performance • q large ⇒ FIFO • q small ⇒ q must be large with respect to context switch, otherwise overhead is too high
  • 17.
    EXAMPLE OF ROUND-ROBINSCHEDULING Process Burst Time P1 24 P2 3 P3 3  Quantum time 4millisec The Gantt chart is: P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30  Typically, higher average turnaround than SJF, but better response
  • 18.