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

Cpu Scheduling

Uploaded by

14mervekaya01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Cpu Scheduling

Uploaded by

14mervekaya01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CENG 302

OPERATING SYSTEMS

Dr. Mansur Alp TOÇOĞLU


In this chapter,
• Basic concepts
• Planning Criteria
• Planning Algorithms
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-First (SJF) Scheduling
• Priority Scheduling
• Round-Robin Scheduling
topics will be discussed.

2
BASIC CONCEPTS

• CPU scheduling is the basis of multiprogramming


operating systems.
• The CPU makes the computer more efficient by
switching between processes.
• In each time interval, a process is intended to
execute.
• In single processor systems, only one process can be
executed at a time t.
• When there are multiple processes, they will wait for
the CPU to finish its work.
3
BASIC CONCEPTS (C O N T IN U E S …)

• The CPU is enabled to execute other processes


during the waiting periods of the processes (IO-
waiting queue or interrupt-ready queue).
• There are many processes in memory (Where
are these processes?).
• When a process goes to wait in any way, the
CPU switches to another process.

4
BASIC CONCEPTS (C O N T IN U E S …)

• According to the CPU planning algorithm,


the next process,
• is taken from the ready queue, where it waits its turn
• and is sent to the CPU by a program called Dispatcher.
• The process runs on the CPU for as long as the
process scheduling algorithm allows (either until it
finishes or a certain amount of time has passed).
• Either it ends and is removed from memory
• or it is placed back to a proper queue for the next
study.
5
CPU - I/O BURST CYCLE
• CPU-Burst is the time interval required by the CPU
to execute a process until the I/O wait arrives.
• In other words, it is the time spent by the CPU for
a single process at a time.
• Burst time = execution time.
• Execution of a process:
• It starts with a CPU burst, followed by an I/O burst,
followed by another CPU burst, and then another I/O
burst.
• The final CPU burst terminates the program by
requesting termination from the system.
6
CPU SCHEDULER
• When the CPU goes into idle state, the operating
system must select a process from the ready
queue to execute.
• The selection process is performed by the short-
term scheduler or CPU scheduler.
• The ready queue does not have to be first-in,
first-out (FIFO).
• Ready queue may be implemented as FIFO or
Priority Queue.

7
CPU SCHEDULER (C O N T IN U E S …)

• All processes
waiting in the ready
queue have the
status of being
ready by the CPU.
• Process Control
Block (PCB) is kept
in the records in
the queue.

8
PREEMPTIVE AND NON-PREEMPTIVE
CONCEPT

• CPU scheduling algorithms that establish the


scheduling relationship between the ready queue and
the CPU can basically be examined in 2 groups:
• Preemptive algorithms: A running process can be
removed from the CPU and another desired process
(priority) can be executed in the CPU.
• Non-preemptive algorithms: After the process starts
running on the CPU;
• The process uses the CPU until it completes or stops.
• It runs until it is blocked by an I/O request in its own code or
until it exits the CPU of its own will.

9
PREEMPTIVE AND NON-PREEMPTIVE
CONCEPT (C O N T IN U E S …)

• Windows 3.1 used non-preemptive scheduling.


• All other Windows versions used preemptive
scheduling.
• The Mac OS X operating system also uses preemptive
scheduling.
• A race condition occurs when preemptive
planning shares data.
• If a process is interrupted while making changes to the
data and another process is started and accesses the
same data, race condition occurs.

10
CPU PLANNING

• CPU scheduling decision is made under 4 cases:


1. When a process goes from running state to waiting state (I/O
request),
2. When a process transitions from a running state to a ready state
(interrupt),
3. When a process transitions from a waiting state to a ready state (I/O
completion),
4. When a process terminates.
• In cases 1 and 4, there is no other option in terms of planning and a
new process is selected from the ready queue and started to run.
• In cases 2 and 3, there are options to switch to different states. (It can be
taken directly back to the CPU from the ready queue, or a process with
a different priority can be placed in the CPU.)
11
PROCESS STATES DURUMLARI
–REMINDER

12
DISPATCHER

• The dispatcher used in operating system design is the name of the


program that takes the next ready process according to the
CPU scheduling algorithm and sends it to the CPU.
• The dispatcher carries out the process of taking the next one of
these processes from the ready queue and sending it to the CPU.
• It is imperative that the dispatcher transitions very quickly.
• The transition time between processes is called dispatch latency.

13
SCHEDULING CRITERIA
• CPU scheduling algorithms are compared based on a number of
different criteria:
1. CPU Utilization:It is desirable that the CPU be in use as
much as possible. CPU usage rate is between 0% and 100%. In
real systems, this ratio is between 40% (normal) and 90%
(intensive).
Utilization = useful time/total time.
1. Throughput: It is the number of processes completed per
unit time (per second, per hour).
2. Turnaround time: For a process, it is sum of the time
• Time to be taken into Memory (HD to RAM),
• Waiting time in the ready queue,
• running on the CPU,
• Time to perform the I/O operation.

14
SCHEDULING CRITERIA (C O N T IN U E S …)

4. Waiting time: It is the time that a process waits in


the ready queue.
5. Response time: Waiting time of a process until it gets
the CPU for the first time.

15
SCHEDULING CRITERIA (C O N T IN U E S …)

What do we want for optimization?

• Maximum CPU Usage (utilization)


• Maximum Throughput (throughput)
• Minimum Turnaround Time
• Minimum Waiting Time
• Minimum Response Time

16
SCHEDULING ALGORITHMS

• CPU scheduling algorithms determine which of the


processes waiting in the ready queue will be
assigned to the CPU.
1. First-Come, First-Served (FCFS) Scheduling
2. Shortest-Job-First (SJF) Scheduling
3. Priority Scheduling
4. Round-Robin Scheduling

17
1.FIRST-COME, FIRST-SERVED (FCFS)

• It is the simplest CPU scheduling algorithm and works


as first-come first served (FCFS).
• The process that makes the first request to the CPU
is the process that is assigned to the CPU first.
• Non-preemptive scheduling algorithm.
• Not priority scheduling algorithm.
• It can be managed with FIFO queue structure.
• The average waiting time with the FCFS algorithm is
generally high.
• Waiting times vary greatly depending on the order in
which processes arrive in the queue.

18
1.FIRST-COME, FIRST-SERVED (FCFS)
(C O N T IN U E S … )

Process Burst Time


P1 24
P2 3
P3 3
• Arrival order of processes: P1 , P2 , P3

P1 P2 P3

0 24 27 30

• Waiting times: P1 = 0; P2 = 24; P3 = 27


• Average waiting time: (0 + 24 + 27)/3 = 17 ms

19
1.FIRST-COME, FIRST-SERVED (FCFS)
(C O N T IN U E S … )

If the order of processes changes as follows


P2 , P3 , P1

P2 P3 P1

0 3 6 30

• Waiting Times: P1 = 6; P2 = 0; P3 = 3
• Average waiting Time: (6 + 0 + 3)/3 = 3

20
1.FIRST-COME, FIRST-SERVED (FCFS)
(C O N T IN U E S … )

• In the FCFS algorithm, if the burst times of the processes


are very different, the average waiting times will vary
greatly.
• The situation where many small processes wait for a
large process to leave the CPU is called convoy effect.
• When a CPU is allocated to a process, it retains the CPU
until it terminates or receives an I/O request.
• The FCFS algorithm is not suitable for time-sharing
systems that share the CPU at certain time intervals.

21
2.SHORTEST-JOB-FIRST (SJF)

• In the Shortest-Job-First Scheduling (SJF) algorithm, the


process with the shortest processing time in the CPU
(also called shortest-next-CPU-burst) is assigned.

• Average Waiting Time: (0 + 3 + 16 + 9) / 4 = 7 ms


• If FCFS was used it would be 10.25 ms.
• Two processes with the same burst time are selected with
FCFS.
22
2.SHORTEST-JOB-FIRST (SJF)
(C O N T IN U E S … )

• The SJF algorithm can be preemptive and non-


preemptive.
• When a new process with a shorter burst time comes
to the ready queue,
• Preemptive SJF interrupts the running process,
• Non-preemptive SJF allows the currently running one to
terminate.
• Preemptive SJF,
• is called as Shortest-remaining-time-first (The one
with the shortest remaining burst time should work
first).

23
2.SHORTEST-JOB-FIRST (SJF)
(C O N T IN U E S … )

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 + (8 - 2) + (7 - 4) + (12 - 5))/4


• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
24
2.SHORTEST-JOB-FIRST (SJF)
(C O N T IN U E S … )

Process Arival Time Burst 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

25
2.SHORTEST-JOB-FIRST (SJF)
(C O N T IN U E S … )

Preemptive SJF

Average waiting time = [9+0+15+2]/4 = 26/4 = 6.5 msec


Non-preemptive SJF Average waiting time 7.75 msec.
26
3. PRIORITY SCHEDULING

• The CPU is assigned to the process with the


highest priority.
• Those with equal priority are assigned in FCFS
order.
• The shortest-job-first (SJF) algorithm is a special
case of priority planning algorithms.
– The SJF algorithm prioritizes based on the estimated
CPU-burst time.
– In the SJF algorithm, the priority increases as the CPU
burst time decreases, and the priority decreases as the
CPU burst time increases.
27
3. PRIORITY SCHEDULING
(CONTINUES…)

• Below is a gantt chart according to priority value for 5


processes. Let's assume that all processes arrive at time 0.

• Average waiting time (1+ 6 + 16 + 18) / 4 = 8,2 ms.

28
3. PRIORITY SCHEDULING
(CONTINUES…)

• Prioritization criteria can be one or more of the following:


– Time limit
– Memory requirement
– Number of opened files
– I/O burst and CPU burst rate
– Importance of the process (political, funds …)
• Priority planning can be preemptive and non-preemptive.
– In the preemptive method, when a process reaches the ready queue, if
it has priority over the currently running process, the running process
is interrupted.
– In the non-preemptive method, when a process reaches the ready
queue, the current one continues to run, even if it has a higher
priority than the currently running process.

29
3. PRIORITY SCHEDULING
(CONTINUES…)

• In the priority planning algorithm,


– The CPU can continuously run high priority processes and
– Some processes may always wait in the ready queue
(indefinite blocking, starvation).
• To prevent unlimited waiting
– Priority aging method is used.
– While low-priority processes are waiting in the queue, the
priority level is increased (e.g., increased by 1 every 15
minutes).
• By increasing the priority value, even the process
with the lowest priority is ensured to run after a
certain period of time.
30
4. ROUND ROBIN

• Round-robin (RR) planning is often used in time-sharing


systems.
• It is the FCFS algorithm with Preemption concept.
• Ready queue(circular queue) is used as FIFO.
• Processes in the ready queue are sequentially assigned to
the CPU within a certain time interval (time slice =
quantum).
• The time interval is usually chosen between «10 ms and
100 ms».
– (1 time interval = time quantum)
• A process that ends in less than this time releases the CPU.
• With round-robin scheduling, the average waiting time is
often long. 31
4. ROUND ROBIN (C O N T IN U E S …)

Process Burst Time


P1 24
Example1 RR:
P2 3
Time Quantum = 4
P3 3 Arrival Time = 0

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

• Waiting times: P1 (10-4)=6 , P2=4 , P3=7


• Average Waiting Time= 17/3=5.66

32
4. ROUND ROBIN (C O N T IN U E S …)

Process Burst Time


P1 53
P2 17 EXAMPLE2 RR:
P3 68 Time Quantum = 20
P4 24

• Gantt chart:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

• Waiting Time: P1:57+24=101; P2:20; P3:37+40+17=94; P4:57+40=97

33
4. ROUND ROBIN (C O N T IN U E S …)

• If the time slice duration (quantum) is very large, the


operation is similar to the FCFS method.
• If the time slice duration is too small, context
switching operations are performed too much.
– Context switch time becomes overhead and it is not
desirable to perform too many context switches.
• Time slice duration is usually taken as 10 times the
context switch duration.
– Modern systems quantum range between 10-100 ms
– Context switch time <10 ms
– 10% of CPU time is spent for context switch.
34
4. ROUND ROBIN (C O N T IN U E S …)

35
RESOURCES
• Textbook:
• Operating System Concepts, Ninth Edition, Abraham
Silberschatz, Peter Bear Galvin, Greg Gagne

36

You might also like