0% found this document useful (0 votes)
0 views

CPU Scheduling

The document discusses CPU scheduling, which determines which process will use the CPU for execution while others wait. It covers various scheduling algorithms such as First-Come, First-Served, Shortest-Job-First, Priority Scheduling, Round Robin, and Multilevel Queue Scheduling, detailing their mechanisms and performance criteria. Additionally, it explains the roles of the dispatcher and the importance of optimizing CPU utilization, throughput, and response times.

Uploaded by

shagun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

CPU Scheduling

The document discusses CPU scheduling, which determines which process will use the CPU for execution while others wait. It covers various scheduling algorithms such as First-Come, First-Served, Shortest-Job-First, Priority Scheduling, Round Robin, and Multilevel Queue Scheduling, detailing their mechanisms and performance criteria. Additionally, it explains the roles of the dispatcher and the importance of optimizing CPU utilization, throughput, and response times.

Uploaded by

shagun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Amritsar College of Engineering & Technology, Amritsar, Punjab, INDIA

NAAC - A grade, NBA accredited courses(2009-


12, 2016-18), UGC Autonomous College

SUBJECT: OPERATING SYSTEM


SUBJECT CODE:AGCS 21404
TOPIC: CPU Scheduling

Ms. Shagun Arora


Assistant Professor
Department of Computer Science and Engineering

Ms. Shagun Arora [email protected] CSE 4th Operating System


1
What is CPU Scheduling?

CPU Scheduling is a process of


determining which process will own CPU
for execution while another process is
on hold.
The main task of CPU scheduling is to make
sure that whenever the CPU remains idle, the
OS at least select one of the processes
available in the ready queue for execution.

2 Ms. Shagun Arora [email protected] CSE 4th Operating System


3 Ms. Shagun Arora [email protected] CSE 4th Operating System
CPU burst is when the process is being executed in the
CPU.
I/O burst is when the CPU is waiting for I/O for further
execution.
After I/O burst, the process goes into the ready queue
for the next CPU burst.
4 Ms. Shagun Arora [email protected] CSE 4th Operating System
Alternating Sequence of CPU And I/O
Bursts

5 Ms. Shagun Arora [email protected] CSE 4th Operating System


6 Ms. Shagun Arora [email protected] CSE 4th Operating System
CPU Scheduler
Selects from among the processes in memory that
are ready to execute, and allocates the CPU to
one of them
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
Preemptive scheduling the CPU is allocated to
the processes for the limited time .
 Non-preemptive scheduling, the CPU is
allocated to the process till it terminates or
switches to waiting state.
Scheduling under 1 and 4 is nonpreemptive
Ms. Shagun Arora [email protected] CSE 4th Operating
7
All System
other scheduling is preemptive
CPU-scheduling decisions may take place under the following four
circumstances:

1.When a process switches from the running state to the waiting state (for
example, as the result of an I/O request or an invocation of wait() for the
termination of a child process).

2.When a process switches from the running state to the ready state (for
example, when an interrupt occurs)

3.When a process switches from the waiting state to the ready state (for
example, at completion of I/O)

4.When a process terminates

8 Ms. Shagun Arora [email protected] CSE 4th Operating System


Dispatcher
Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves following function:
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

9 Ms. Shagun Arora [email protected] CSE 4th Operating System


Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – no. of processes that complete their
execution per time unit
Turnaround time(TAT) – amount of time to
execute a particular process.{ TAT= CT-AT}
Waiting time – amount of time a process has been
waiting in the ready queue{WT=TAT-BT}
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)
10 Ms. Shagun Arora [email protected] CSE 4th Operating System
Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time

11 Ms. Shagun Arora [email protected] CSE 4th Operating System


First-Come, First-Served (FCFS)
Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order:
P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

 Waiting time for P = 0; P = 24; P = 27


1 2 3

12 Average waiting time: (0 +
Ms. Shagun Arora [email protected]
24 + 27)/3 = 17
CSE 4th
Operating System
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

 Waiting time for P = 6; P = 0 P = 3


1 2 ; 3
 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Short process behind long process
Ms. Shagun Arora [email protected] CSE 4th Operating
13 System
Shortest-Job-First (SJF)
Scheduling
Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest time
Two schemes:
nonpreemptive – 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

14
time for a given set
Ms. Shagun Arora
[email protected]
of processes
CSE 4th
Operating System
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)
P1 P3 P2 P4

0 3 7 8 12 16

 Average waiting time = (0 + 6 + 3 + 7)/4 = 4


Ms. Shagun Arora [email protected] CSE 4th Operating
15 System
Example of Preemptive SJF
ProcessArrival TimeBurst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

Average waiting time = (9 + 1 + 0 +2)/4 = 3


Ms. Shagun Arora [email protected] CSE 4th Operating
16 System
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 a priority scheduling where priority is the
predicted next CPU burst time
Problem  Starvation – low priority processes may
never execute
Solution  Aging – as time progresses increase the
Ms. Shagun Arora
17 priority of the process
[email protected] CSE 4th
Operating System
Round Robin (RR)
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,
Ms. Shagunotherwise
Arora overhead is too high
18 [email protected] CSE 4th
Operating System
Example of RR with Time Quantum
= 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround than


SJF, but better response
Ms. Shagun Arora
19 [email protected] CSE 4th
Operating System
Time Quantum and Context Switch
Time

Ms. Shagun Arora


20 [email protected] CSE 4th
Operating System
Multilevel Queue
Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
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
Ms. Shagun Arora in FCFS
21 [email protected] CSE 4th
Operating System
Multilevel Queue Scheduling

Ms. Shagun Arora


22 [email protected] CSE 4th
Operating System
The Description of the processes in the above
diagram is as follows:
System Process
The Operating system itself has its own process to run and is termed as System
Process.

Interactive Process
The Interactive Process is a process in which there should be the same kind of
interaction (basically an online game ).

Batch Processes
Batch processing is basically a technique in the Operating system that collects the
programs and data together in the form of the batch before the processing starts.

Student Process
The system process always gets the highest priority while the student processes
always get the lowest priority.

In an operating system, there are many processes, in order to obtain the result we
cannot put all processes in a queue; thus this process is solved by Multilevel queue
scheduling.
Ms. Shagun Arora
23 [email protected] CSE 4th
Operating System

You might also like