SlideShare a Scribd company logo
1 of 64
Download to read offline
Chapter 5: CPU Scheduling
CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Algorithm Evaluation
Basic Concepts
• Maximum CPU
utilization obtained :
multiprogramming
• CPU burst followed by
I/O burst
• CPU burst distribution is
of main concern
Histogram of CPU-burst Times
CPU Scheduler
 Whenever the CPU becomes idle, the operating
system must select one of the processes in the
ready queue to be executed.
 The selection process is carried out by the CPU
scheduler.
 The ready queue may be ordered in various
ways
 (Is ready Queue always follow FIFO Rule?? ).
Diagram of Process State
CPU Scheduler
 CPU scheduling decisions may take place when a
process:
1. Switches from running state to waiting state
2. Switches from running state to ready state
3. Switches from waiting state to ready state
4. When a process terminates
 For situations 1 and 4, there is no choice in terms of
scheduling. A new process (if one exists in the ready
queue) must be selected for execution.
 There is a choice, however, for situations 2 and 3.
Non-preemptive Scheduling
• When scheduling takes place only under
circumstances 1 and 4.
• Under non-preemptive scheduling, once the CPU
has been allocated to a process, the process keeps
the CPU until it releases the CPU either by
terminating or by switching to the waiting state.
• It is efficient on hardware platforms.
Preemptive scheduling
 When scheduling takes place only under circumstances
2 and 3
 Preemptive scheduling can result in race conditions
when data are shared among several processes.
 Problems in Preemptive scheduling:
 Consider the case of two processes that share data. While one process is
updating the data, it is preempted so that the second process can run.
The second process then tries to read the data, which is in an
inconsistent state.
 Consider preemption while in kernel mode
 Consider interrupts occurring during crucial OS activities
Dispatcher
• Dispatcher module gives control of the
CPU to the process selected by the CPU
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
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – number of processes that complete
their execution per time unit (e.g., 5 per second)
• Turnaround time – amount of time to execute a
particular process
• Waiting time – total amount of time a process has
been waiting in the ready queue
• 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)
Common Terms used in Scheduling
• Arrival Time (AT)-(Submission time)
• Burst time (BT) -(CPU service time)
Optimization Criteria for Scheduling
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
Scheduling Algorithm
• First –come, First-serve (FCFS)
• Shortest-Job-First Scheduling (SJF)
• Round-Robin Scheduling (RR)
• Priority Scheduling
• Multilevel Queue Scheduling
Scheduling methods
• Non-preemptive:
– FCFS –First come first served
– SJF –shortest job first
– Priority scheduling
• Pre-emptive:
– SJFand SRTF
– Priority scheduling
– Round robin
– Multilevel queue
– Multilevel feed back queue
First- Come, First-Served (FCFS) Scheduling
• Consider the following three processes and their burst time
ProcessBurst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
• We use Gantt Chart to illustrate a particular schedule
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
P P P
1 2 3
0 24 30
27
FCFS Scheduling (Cont.)
• Suppose that the processes arrive in the order:
P2 , P3 , P1
• 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
– Consider one CPU-bound and many I/O-bound processes
P1
0 3 6 30
P2
P3
Q:FCFS
Calculate TAT, WT, RT?
Q:FCFS
Calculate TAT, WT, RT?
Shortest-Job-First (SJF)
• Associate with each process the length of its
next CPU burst
– Use these lengths to schedule the process with the
shortest time
• SJF is optimal – gives minimum average waiting
time for a given set of processes
– How do we know what is the length of the next CPU
request
– Could ask the user
• what if the user lies?
Example of SJF
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• SJF scheduling chart
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
P3
0 3 24
P4
P1
16
9
P2
 Consider the following four processes and their burst time
Q: SJF
Determining Length of Next CPU Burst
• Can only estimate (predict) the length – in
most cases should be similar to the previous
CPU burst
– Pick the process with shortest predicted next CPU
burst
• Can be done by using the length of previous
CPU bursts, using exponential averaging
• Commonly, α set to ½
:
Define
4.
1
0
,
3.
burst
CPU
next
the
for
value
predicted
2.
burst
CPU
of
length
actual
1.







 1
n
th
n n
t
  .
1
1 n
n
n t 


 



Prediction of the Length of the Next CPU Burst
Shortest-remaining-time-first
• Preemptive version of SJF is called shortest-remaining-time-
first
• Example illustrating the concepts of varying arrival times and
preemption.
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart
• Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 =
6.5 msec
P4
0 1 26
P1
P2
10
P3
P1
5 17
Q: SJRF
Q: SJRF
• Consider three processes, all arriving at time zero, with
total execution time (CPU and I/O) of 10, 20 and 30 units,
respectively.
• Each process spends the first 20% of execution time doing
I/O, the next 70% of time doing computation, and the last
10% of time doing 1/0 again.
• The operating system uses a SJF scheduling algorithm and
schedules a new process either when the running process
gets blocked on I/O or when the running process finishes its
compute burst.
• Assume that all I/O operations can be overlapped as much
as possible. For what percentage of time does the CPU
remain idle?
??
Consider the following table of arrival time and burst time for
three processes P0, P1 and P2.
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The shortest job first scheduling algorithm is used. Scheduling
is carried out only at arrival or completion of processes. What
is the average waiting time for the three processes?
Answer 5ms
??
• Consider the following set of processes, with the arrival
times and the CPU-burst times given in milliseconds
Process Arrival Time Burst Time
P1 0 5
P2 1 3
P3 2 3
P4 4 1
• What is the average turnaround time for these
processes with the preemptive shortest remaining
processing time first (SRPT) algorithm ?
Answer 5.5
*??*
Consider the following table (Assume Non Preemptive scheduling):
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. Give the Gantt Chart & Calculate average turnaround time using
i) FCFS scheduling
ii) SJF scheduling
b. Give the Gantt Chart & Compute average turnaround time if the
CPU is left idle for the first 1 unit
and then SJF scheduling is used. (Remember that processes P1 and P2
are waiting during this idle time, so their waiting time may increase.)
Round Robin (RR)
• Designed for time-sharing systems
• Each process gets a small unit of CPU time (time
quantum q).
• 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.
• Timer (hardware support) interrupts every quantum
to schedule next process
Round Robin (RR)
• *Process may use less than a full time slice*
• Performance
– q large  FIFO
– q small  q must be large with respect to context
switch, otherwise overhead is too high
Round Robin (RR) algorithm
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:
• The average waiting time under the RR policy is often longer
• Typically, higher average turnaround than SJF, but better
response
• q should be large compared to context switch time
• q is usually 10ms to 100ms, context switch < 10 usec
P P P
1 1 1
0 18 30
26
14
4 7 10 22
P2
P3
P1
P1
P1
 Consider the following three processes and their burst time
Time Quantum and Context Switch Time
• The performance of the RR algorithm depends on the size of
the time quantum. If the time quantum is extremely small
(say, 1 millisecond), RR can result in a large number of
context switches.
Turn Around Time in RR
• Increase in quantum time -less frequent
executions of switching –lesser turn around time
• Average turn around time (TT) does not always
improve with increase in quantum time
• TT can be improved if most processes can finish
their jobs in one quantum
• With 3 processes of 10 ms each, and time slice =
1 ms, average turn around time = 29 ms
(excluding switching time)
• If time slice = 10, average turn around time = 20
ms (plus switching time)
Turnaround Time Varies With The Time Quantum
• The average turnaround time of a set of processes does not
necessarily improve as the time-quantum size increases. In
general, the average turnaround time can be improved if most
processes finish their next CPU burst in a single time quantum.
Q:RR
Solve for q=1
*??*
Consider the set of processes where processes are arrived in the order P1, P2,
P3, P4 all at time to.
Process Burst Time (milliseconds) Priority
P1 20 2
P2 2 1
P3 4 2
P4 2 3
a. Find the total number of context switches present in Non Preemptive
Priority & RR scheduling (Time Slice=2). Diagram Required.
b. When the behavior of RR scheduling become equivalent to FCFS. Explain. In
above mentioned case find out the time quantum when RR & FCFS will be
equivalent.
??
q=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
– Nonpreemptive
• 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
ProcessAarri Burst TimeTPriority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Priority scheduling Gantt Chart
• Average waiting time = 8.2 msec
1
0 1 19
P1
P2
16
P4
P3
6 18
P
 Consider the following five processes and their burst time
Combining Priority Scheduling and RR
ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
• Priority scheduling Gantt Chart
• Average waiting time = 8.2 msec
 System executes the highest priority process; processes with the same
priority will be run using round-robin.
 Consider the following five processes and their burst time
??
We wish to schedule three processes PI, P2 and P3 on a
uniprocessor system. The priorities, CPU time requirements and
arrival times of the processes are as shown below.
Process Priority CPU time required Arrival time (hh:mm:ss)
• PI 10(highest) 20 sec 00:00:05
• P2 9 10 sec 00:00:03
• P3 8 (lowest) 15 sec 00:00:00
We have a choice of preemptive or non-preemptive scheduling. In
preemptive scheduling, a late-arriving higher priority process can
preempt a currently running process with lower priority. In non-
preemptive scheduling, a late-arriving higher priority process
must wait for the currently executing process to complete before
it can be scheduled on the processor. What are the turnaround
times (time from arrival till completion) of P2 using preemptive
and non-preemptive scheduling respectively.
??
*??*
Multilevel Queue
• Ready queue is partitioned into separate queues, eg:
– foreground (interactive)
– background (batch)
• Process permanently in a given queue
• Each queue has its own scheduling algorithm:
– foreground – RR
– background – FCFS
• Scheduling must be done between the queues:
– Fixed priority scheduling; (i.e., serve all from foreground
then from background). 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
Separate Queue For Each Priority
Multilevel Queue Scheduling
Multilevel Feedback Queue
• A process can move between the various
queues; aging can be implemented this way
• Multilevel-feedback-queue scheduler defined by
the following parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a
process
– method used to determine when to demote a
process
– method used to determine which queue a process
will enter when that process needs service
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served
FCFS
• When it gains CPU, job receives 8
milliseconds
• If it does not finish in 8 milliseconds,
job is moved to queue Q1
– At Q1 job is again served FCFS and receives
16 additional milliseconds
• If it still does not complete, it is
preempted and moved to queue Q2
Logical Questions
What scheduling policy will you use for each of
the following cases? Explain your reasons for
choosing them.
a. The processes arrive at large time intervals:
b. The system’s efficiency is measured by the
percentage of jobs completed.
c. All the processes take almost equal amounts
of time to complete.
a. FCFS
b. STF
c. FCFS or RR
• In which of the following operations, the
scheduler is not called into play?
a. Process requests for I/O.
b. Process finishes execution.
c. Process finishes its time allotted.
d. All of the above
e. None of the above
Answer D
• What are the factors that need to be
considered to determine the degree of
multiprogramming in a system?
Ans: The two factors that need to be considered
are:
1. The overheads in context switching may
become excessive.
2. With excessive multiprogramming the
response times may become unacceptable.
• What happens if the time allocated in a
Round Robin Scheduling is very large? And
what happens if the time allocated is very
low?
Ans: It results in a FCFS scheduling. If time is too
low, the processor through put is reduced. More
time is spent on context switching
• What is the difference between the idle and
blocked state of a process?
Ans: In idle state, the process is waiting for the
processor to become free so that it can execute.
In blocked state, the process has been put out
from the running state by the processor due to
some I/O.
• Put the following in the chronological order
in the context of the birth of a process
executes: Ready, suspended, execute,
terminate, create.
Ans: Create, Ready, Execute, Suspended,
Terminate
• A Shortest Job First algorithm may lead to
starvation where a process with large execution
time is made to wait for indefinitely long times.
Suggest a modification to the SJF that
overcomes this problem.
Ans: A clock value (arrival time) is stored for each
process. This helps to determine the priority of a
process as a function of execution time and the
clock value.
• If the waiting time for a process is p and
there are n processes in the memory then the
CPU utilization is given by,
a. p/n
b. p^n (p raised to n)
c. 1-p^n
d. n-(p^n)
Ans: p^n
*??*
• Suppose a new process in a system arrives at
an average of six processes per minute and
each such process requires an average of 8
seconds of service time. Estimate the fraction
of time the CPU is busy in a system with a
single processor.
• Given that there are on an average 6 processes
per minute. So the arrival rate = 6 process/min.
i.e. every 10 seconds a new process arrives on an
average. or we can say that every process stays
for 10 seconds with the CPU Service time = 8 sec.
• Hence the fraction of time CPU is busy = service
time / staying time
=8 / 10 =0.8
So the CPU is busy for 80% of the time.
End of Chapter 5

More Related Content

Similar to OS Process Chapter 3.pdf

dataprocess using different technology.ppt
dataprocess using different technology.pptdataprocess using different technology.ppt
dataprocess using different technology.ppt
ssuserf6eb9b
 
Ch6
Ch6Ch6
Ch6
C.U
 
3 process scheduling
3 process scheduling3 process scheduling
3 process scheduling
ahad alam
 
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
gopi7
 
fggggggggggggggggggggggggggggggfffffffffffffffffff
fggggggggggggggggggggggggggggggffffffffffffffffffffggggggggggggggggggggggggggggggfffffffffffffffffff
fggggggggggggggggggggggggggggggfffffffffffffffffff
adugnanegero
 
Process management in os
Process management in osProcess management in os
Process management in os
Miong Lazaro
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
tech2click
 

Similar to OS Process Chapter 3.pdf (20)

ch_scheduling (1).ppt
ch_scheduling (1).pptch_scheduling (1).ppt
ch_scheduling (1).ppt
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
CPU Scheduling Lecture 5 - 6.pptx
CPU Scheduling Lecture 5 - 6.pptxCPU Scheduling Lecture 5 - 6.pptx
CPU Scheduling Lecture 5 - 6.pptx
 
CPU Scheduling Part-III.pdf
CPU Scheduling Part-III.pdfCPU Scheduling Part-III.pdf
CPU Scheduling Part-III.pdf
 
Ch6 cpu scheduling
Ch6   cpu schedulingCh6   cpu scheduling
Ch6 cpu scheduling
 
dataprocess using different technology.ppt
dataprocess using different technology.pptdataprocess using different technology.ppt
dataprocess using different technology.ppt
 
Ch6
Ch6Ch6
Ch6
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithms
 
3 process scheduling
3 process scheduling3 process scheduling
3 process scheduling
 
Ch05
Ch05Ch05
Ch05
 
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
3_process_scheduling.ppt
3_process_scheduling.ppt3_process_scheduling.ppt
3_process_scheduling.ppt
 
fggggggggggggggggggggggggggggggfffffffffffffffffff
fggggggggggggggggggggggggggggggffffffffffffffffffffggggggggggggggggggggggggggggggfffffffffffffffffff
fggggggggggggggggggggggggggggggfffffffffffffffffff
 
3_process_scheduling.ppt
3_process_scheduling.ppt3_process_scheduling.ppt
3_process_scheduling.ppt
 
Process Scheduling Algorithms for Operating Systems
Process Scheduling Algorithms for Operating SystemsProcess Scheduling Algorithms for Operating Systems
Process Scheduling Algorithms for Operating Systems
 
3_process_scheduling.ppt----------------
3_process_scheduling.ppt----------------3_process_scheduling.ppt----------------
3_process_scheduling.ppt----------------
 
Process management in os
Process management in osProcess management in os
Process management in os
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
 
Operating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling ProcessOperating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling Process
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 

OS Process Chapter 3.pdf

  • 1. Chapter 5: CPU Scheduling
  • 2. CPU Scheduling • Basic Concepts • Scheduling Criteria • Scheduling Algorithms • Algorithm Evaluation
  • 3. Basic Concepts • Maximum CPU utilization obtained : multiprogramming • CPU burst followed by I/O burst • CPU burst distribution is of main concern
  • 5. CPU Scheduler  Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed.  The selection process is carried out by the CPU scheduler.  The ready queue may be ordered in various ways  (Is ready Queue always follow FIFO Rule?? ).
  • 7. CPU Scheduler  CPU scheduling decisions may take place when a process: 1. Switches from running state to waiting state 2. Switches from running state to ready state 3. Switches from waiting state to ready state 4. When a process terminates  For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must be selected for execution.  There is a choice, however, for situations 2 and 3.
  • 8. Non-preemptive Scheduling • When scheduling takes place only under circumstances 1 and 4. • Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. • It is efficient on hardware platforms.
  • 9. Preemptive scheduling  When scheduling takes place only under circumstances 2 and 3  Preemptive scheduling can result in race conditions when data are shared among several processes.  Problems in Preemptive scheduling:  Consider the case of two processes that share data. While one process is updating the data, it is preempted so that the second process can run. The second process then tries to read the data, which is in an inconsistent state.  Consider preemption while in kernel mode  Consider interrupts occurring during crucial OS activities
  • 10. Dispatcher • Dispatcher module gives control of the CPU to the process selected by the CPU 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
  • 11. Scheduling Criteria • CPU utilization – keep the CPU as busy as possible • Throughput – number of processes that complete their execution per time unit (e.g., 5 per second) • Turnaround time – amount of time to execute a particular process • Waiting time – total amount of time a process has been waiting in the ready queue • 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)
  • 12. Common Terms used in Scheduling • Arrival Time (AT)-(Submission time) • Burst time (BT) -(CPU service time)
  • 13. Optimization Criteria for Scheduling • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time
  • 14. Scheduling Algorithm • First –come, First-serve (FCFS) • Shortest-Job-First Scheduling (SJF) • Round-Robin Scheduling (RR) • Priority Scheduling • Multilevel Queue Scheduling
  • 15. Scheduling methods • Non-preemptive: – FCFS –First come first served – SJF –shortest job first – Priority scheduling • Pre-emptive: – SJFand SRTF – Priority scheduling – Round robin – Multilevel queue – Multilevel feed back queue
  • 16. First- Come, First-Served (FCFS) Scheduling • Consider the following three processes and their burst time ProcessBurst Time P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 • We use Gantt Chart to illustrate a particular schedule • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 P P P 1 2 3 0 24 30 27
  • 17. FCFS Scheduling (Cont.) • Suppose that the processes arrive in the order: P2 , P3 , P1 • 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 – Consider one CPU-bound and many I/O-bound processes P1 0 3 6 30 P2 P3
  • 20. Shortest-Job-First (SJF) • Associate with each process the length of its next CPU burst – Use these lengths to schedule the process with the shortest time • SJF is optimal – gives minimum average waiting time for a given set of processes – How do we know what is the length of the next CPU request – Could ask the user • what if the user lies?
  • 21. Example of SJF ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 • SJF scheduling chart • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P3 0 3 24 P4 P1 16 9 P2  Consider the following four processes and their burst time
  • 23. Determining Length of Next CPU Burst • Can only estimate (predict) the length – in most cases should be similar to the previous CPU burst – Pick the process with shortest predicted next CPU burst • Can be done by using the length of previous CPU bursts, using exponential averaging • Commonly, α set to ½ : Define 4. 1 0 , 3. burst CPU next the for value predicted 2. burst CPU of length actual 1.         1 n th n n t   . 1 1 n n n t        
  • 24. Prediction of the Length of the Next CPU Burst
  • 25. Shortest-remaining-time-first • Preemptive version of SJF is called shortest-remaining-time- first • Example illustrating the concepts of varying arrival times and preemption. ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 • Preemptive SJF Gantt Chart • Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec P4 0 1 26 P1 P2 10 P3 P1 5 17
  • 27. Q: SJRF • Consider three processes, all arriving at time zero, with total execution time (CPU and I/O) of 10, 20 and 30 units, respectively. • Each process spends the first 20% of execution time doing I/O, the next 70% of time doing computation, and the last 10% of time doing 1/0 again. • The operating system uses a SJF scheduling algorithm and schedules a new process either when the running process gets blocked on I/O or when the running process finishes its compute burst. • Assume that all I/O operations can be overlapped as much as possible. For what percentage of time does the CPU remain idle?
  • 28. ?? Consider the following table of arrival time and burst time for three processes P0, P1 and P2. Process Arrival time Burst Time P0 0 ms 9 ms P1 1 ms 4 ms P2 2 ms 9 ms The shortest job first scheduling algorithm is used. Scheduling is carried out only at arrival or completion of processes. What is the average waiting time for the three processes? Answer 5ms
  • 29. ?? • Consider the following set of processes, with the arrival times and the CPU-burst times given in milliseconds Process Arrival Time Burst Time P1 0 5 P2 1 3 P3 2 3 P4 4 1 • What is the average turnaround time for these processes with the preemptive shortest remaining processing time first (SRPT) algorithm ? Answer 5.5
  • 30. *??* Consider the following table (Assume Non Preemptive scheduling): Process Arrival Time Burst Time P1 0.0 8 P2 0.4 4 P3 1.0 1 a. Give the Gantt Chart & Calculate average turnaround time using i) FCFS scheduling ii) SJF scheduling b. Give the Gantt Chart & Compute average turnaround time if the CPU is left idle for the first 1 unit and then SJF scheduling is used. (Remember that processes P1 and P2 are waiting during this idle time, so their waiting time may increase.)
  • 31. Round Robin (RR) • Designed for time-sharing systems • Each process gets a small unit of CPU time (time quantum q). • 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. • Timer (hardware support) interrupts every quantum to schedule next process
  • 32. Round Robin (RR) • *Process may use less than a full time slice* • Performance – q large  FIFO – q small  q must be large with respect to context switch, otherwise overhead is too high
  • 33. Round Robin (RR) algorithm
  • 34. Example of RR with Time Quantum = 4 Process Burst Time P1 24 P2 3 P3 3 • The Gantt chart is: • The average waiting time under the RR policy is often longer • Typically, higher average turnaround than SJF, but better response • q should be large compared to context switch time • q is usually 10ms to 100ms, context switch < 10 usec P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1  Consider the following three processes and their burst time
  • 35. Time Quantum and Context Switch Time • The performance of the RR algorithm depends on the size of the time quantum. If the time quantum is extremely small (say, 1 millisecond), RR can result in a large number of context switches.
  • 36. Turn Around Time in RR • Increase in quantum time -less frequent executions of switching –lesser turn around time • Average turn around time (TT) does not always improve with increase in quantum time • TT can be improved if most processes can finish their jobs in one quantum • With 3 processes of 10 ms each, and time slice = 1 ms, average turn around time = 29 ms (excluding switching time) • If time slice = 10, average turn around time = 20 ms (plus switching time)
  • 37. Turnaround Time Varies With The Time Quantum • The average turnaround time of a set of processes does not necessarily improve as the time-quantum size increases. In general, the average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum.
  • 39. *??* Consider the set of processes where processes are arrived in the order P1, P2, P3, P4 all at time to. Process Burst Time (milliseconds) Priority P1 20 2 P2 2 1 P3 4 2 P4 2 3 a. Find the total number of context switches present in Non Preemptive Priority & RR scheduling (Time Slice=2). Diagram Required. b. When the behavior of RR scheduling become equivalent to FCFS. Explain. In above mentioned case find out the time quantum when RR & FCFS will be equivalent.
  • 41. 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 – Nonpreemptive • 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
  • 42. Example of Priority Scheduling ProcessAarri Burst TimeTPriority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 • Priority scheduling Gantt Chart • Average waiting time = 8.2 msec 1 0 1 19 P1 P2 16 P4 P3 6 18 P  Consider the following five processes and their burst time
  • 43. Combining Priority Scheduling and RR ProcessA arri Burst TimeT Priority P1 4 3 P2 5 2 P3 8 2 P4 7 1 P5 3 3 • Priority scheduling Gantt Chart • Average waiting time = 8.2 msec  System executes the highest priority process; processes with the same priority will be run using round-robin.  Consider the following five processes and their burst time
  • 44. ??
  • 45. We wish to schedule three processes PI, P2 and P3 on a uniprocessor system. The priorities, CPU time requirements and arrival times of the processes are as shown below. Process Priority CPU time required Arrival time (hh:mm:ss) • PI 10(highest) 20 sec 00:00:05 • P2 9 10 sec 00:00:03 • P3 8 (lowest) 15 sec 00:00:00 We have a choice of preemptive or non-preemptive scheduling. In preemptive scheduling, a late-arriving higher priority process can preempt a currently running process with lower priority. In non- preemptive scheduling, a late-arriving higher priority process must wait for the currently executing process to complete before it can be scheduled on the processor. What are the turnaround times (time from arrival till completion) of P2 using preemptive and non-preemptive scheduling respectively. ??
  • 46. *??*
  • 47. Multilevel Queue • Ready queue is partitioned into separate queues, eg: – foreground (interactive) – background (batch) • Process permanently in a given queue • Each queue has its own scheduling algorithm: – foreground – RR – background – FCFS • Scheduling must be done between the queues: – Fixed priority scheduling; (i.e., serve all from foreground then from background). 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
  • 48. Separate Queue For Each Priority
  • 50. Multilevel Feedback Queue • A process can move between the various queues; aging can be implemented this way • Multilevel-feedback-queue scheduler defined by the following parameters: – number of queues – scheduling algorithms for each queue – method used to determine when to upgrade a process – method used to determine when to demote a process – method used to determine which queue a process will enter when that process needs service
  • 51. Example of Multilevel Feedback Queue • Three queues: – Q0 – RR with time quantum 8 milliseconds – Q1 – RR time quantum 16 milliseconds – Q2 – FCFS • Scheduling – A new job enters queue Q0 which is served FCFS • When it gains CPU, job receives 8 milliseconds • If it does not finish in 8 milliseconds, job is moved to queue Q1 – At Q1 job is again served FCFS and receives 16 additional milliseconds • If it still does not complete, it is preempted and moved to queue Q2
  • 53. What scheduling policy will you use for each of the following cases? Explain your reasons for choosing them. a. The processes arrive at large time intervals: b. The system’s efficiency is measured by the percentage of jobs completed. c. All the processes take almost equal amounts of time to complete.
  • 54. a. FCFS b. STF c. FCFS or RR
  • 55. • In which of the following operations, the scheduler is not called into play? a. Process requests for I/O. b. Process finishes execution. c. Process finishes its time allotted. d. All of the above e. None of the above Answer D
  • 56. • What are the factors that need to be considered to determine the degree of multiprogramming in a system? Ans: The two factors that need to be considered are: 1. The overheads in context switching may become excessive. 2. With excessive multiprogramming the response times may become unacceptable.
  • 57. • What happens if the time allocated in a Round Robin Scheduling is very large? And what happens if the time allocated is very low? Ans: It results in a FCFS scheduling. If time is too low, the processor through put is reduced. More time is spent on context switching
  • 58. • What is the difference between the idle and blocked state of a process? Ans: In idle state, the process is waiting for the processor to become free so that it can execute. In blocked state, the process has been put out from the running state by the processor due to some I/O.
  • 59. • Put the following in the chronological order in the context of the birth of a process executes: Ready, suspended, execute, terminate, create. Ans: Create, Ready, Execute, Suspended, Terminate
  • 60. • A Shortest Job First algorithm may lead to starvation where a process with large execution time is made to wait for indefinitely long times. Suggest a modification to the SJF that overcomes this problem. Ans: A clock value (arrival time) is stored for each process. This helps to determine the priority of a process as a function of execution time and the clock value.
  • 61. • If the waiting time for a process is p and there are n processes in the memory then the CPU utilization is given by, a. p/n b. p^n (p raised to n) c. 1-p^n d. n-(p^n) Ans: p^n
  • 62. *??* • Suppose a new process in a system arrives at an average of six processes per minute and each such process requires an average of 8 seconds of service time. Estimate the fraction of time the CPU is busy in a system with a single processor.
  • 63. • Given that there are on an average 6 processes per minute. So the arrival rate = 6 process/min. i.e. every 10 seconds a new process arrives on an average. or we can say that every process stays for 10 seconds with the CPU Service time = 8 sec. • Hence the fraction of time CPU is busy = service time / staying time =8 / 10 =0.8 So the CPU is busy for 80% of the time.