0% found this document useful (0 votes)
15 views28 pages

3.OS Process Scheduling

Uploaded by

MrSobbi 07
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)
15 views28 pages

3.OS Process Scheduling

Uploaded by

MrSobbi 07
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/ 28

Operating Systems

Process Scheduling
Scheduling
• When a computer is multi-programmed, it
frequently has multiple processes or threads
competing for the CPU at the same time

• If only one CPU is available, a choice has to be


made which process to run next

• The part of the operating system that makes the


choice is called the scheduler

• Algorithm it uses is called the scheduling algorithm


Process Scheduling
• Scheduling is a process of assigning priorities to
all active processes i.e., setting the order of
execution.
• Objective is to allocate all resources efficiently.

• Scheduler is a sub-program of OS which


performs the function of scheduling.

• Scheduler specifically performs two activities:


i. assigns priorities to processes
ii. allocates resources to active processes
3
Scheduling – Process Behavior

Bursts of CPU usage alternate with periods of waiting for I/O.


(a) A CPU-bound process. (b) An I/O-bound process.
When to Schedule
• New process created
• Process exits
• Process blocks on I/O
• I/O interrupt occur
• Non pre-emptive (Stop on I/O, waiting for
other process output)- No Force Control
• Pre-emptive (Time based e.g. 10 sec)-
Force Control
Criteria For Performance Evaluation
UTILIZATION The fraction of time the CPU is being used. ( ratio of in-use time / total
observation time )

THROUGHPUT The number of jobs completed in a period of time. (jobs / unit time )

SERVICE TIME The time required by a device to handle a request. (seconds)

QUEUEING/WAIT TIME Time on a queue waiting for CPU. (seconds)

RESIDENCE TIME The time spent by a request at a device.


RESIDENCE TIME = SERVICE TIME + QUEUEING TIME.

RESPONSE TIME Time used by a system to respond to a User Job. ( seconds )

THINK TIME The time spent by the user of an interactive system to figure out the next
request. (seconds)

TURNAROUND TIME average time from submission to complete a process

The goal is to optimize both the average and the amount of variation
6
Scheduling Algorithm Optimization Criteria

• Max CPU utilization


• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
Scheduling Algorithm Goals
Some goals of the scheduling algorithm under different circumstances.
Scheduling in Batch Systems

• First-come First-served (Non Pre-emptive)

• Shortest Job First (Non Pre-emptive)

• Shortest Remaining Time First (Pre-emptive)


First- Come, First-Served (FCFS) Scheduling
• Simpler scheduling policy.
• Processes are dispatched according to their arrival
time on the ready queue.
• Once the process has the CPU, it runs to completion.
• Non-preemptive discipline.
• Although it is simple to implement, but it drastically
effect the overall efficiency of the system, because
smaller processes arrived later than longer processes,
will wait for a very long time.
• In many scheduling schemes, the process with the
same priorities are dispatched according to FCFS
policy.
First- Come, First-Served (FCFS) Scheduling
T
w im
pro hic e for
co ces h the
ntr s
Process Burst Time o ge
CP l of t ts
U he
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 P1 = 0; P2 = 24; P3 = 27


• Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont’d...)

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 P1 = 6; P2 = 0; P3 = 3


• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case
First- Come, First-Served (FCFS) Scheduling
Shortest Job First (SJF)
• If we assume the run times of the jobs are
known in advanced, the non-preemptive batch
SJF algorithm picks the shortest job first.
• FCFS can be used to break ties.
• Note that this algorithm is optimal when all the
jobs are available simultaneously.

• The difficulty of this algorithm is predicting the


time usage for a job.
Shortest Job First (SJF)

For example :
(a)FIFO (b)SJF

Turnaround average: Turnaround average:


(8 + 12 + 16+ 20)/4= (4 + 8 + 12 + 20)/4 =
14 in FIFO 11 in SJF

Turnaround is average time from submission to complete a process


Shortest Remaining Time First
(SRTF)
Scheduling in Interactive Systems
1. Round-robin scheduling
2. Priority scheduling
3. Shortest process next
4. Lottery scheduling
5. Real-Time scheduling
Round Robin Scheduling
• Each process is assigned a time interval, called
its quantum.
• If the process is running at the end of quantum ,
the CPU is preempted and given to another
process.
• If the process has blocked/finished before
quantum has elapsed, CPU switches to the other
process when process blocks.
Round Robin Example
EXAMPLE DATA:
Process Arrival
Service
Time
Time
1 0 8
2 1 4
3
Round Robin, quantum 2
= 4, no priority-based 9
preemption
4 3 5
P1 P2 P3 P4 P1 P3 P4 P3
0 4 8 12 16 20 24 25 26

Avg.Turnaround Time= ( (20-0) + (8-1) + (26-2) + (25-3) )/4 = 74/4 = 18.5

19
Time Quantum and Context Switch Time

Reference: Operating System Concepts- 9th ed. By Silberschatz et al., 2013


Round Robin Scheduling
R.R scheduling is easy and fair, the only problem is how to
choose the length of quantum

• If quantum size is too big (e.g. 500 msec) with the


required time for process switching (e.g. 5 msec),
response time suffers. Suppose 10 users hits the Enter
key simultaneously, it means the last process in the list
should wait for a long time.

• If quantum size is too small (e.g. 20 ms) throughputs


suffers. With spending 5 msec on process switching
20% of CPU time will be wasted on administrative
overhead (i.e., saving and loading registers and other
required tasks for process switching).
Priority Scheduling
• Each process is assigned a priority and the CPU is
allocated to the process with higher priority
The problem is high-priority processes may run
indefinitely and low priority processes may never
execute (starvation problem).
Priority Scheduling
Priority can be assigned to the processes:
• Statically: external to O.S
• Dynamically: Assign by the system to achieve some
system goals.
• For example I/O bound process should be given CPU
immediately. A simple algorithm for doing that is set
the priority to 1/f, where f is the fraction of the last
quantum that a process used.
• E.g. process that used only 1msec of its 50 msec
quantum would get priority 50, a process that ran 25
msec before blocking would get priority 2, and a process
that used the whole quantum would get priority 1.
Priority Scheduling
• Another way is to group the process into priority
classes and use priority scheduling among the class
but R.R within each class. If the priority class become
empty then the lower class will be run (see the next
slide)

• The problem with this method is if priorities are not


adjusted occasionally, lower priority classes may all
starve to death
Priority Classes

A scheduling algorithm with four priority classes.


Lottery scheduling
• In this algorithm each job receives some number
of lottery tickets, and on each time slice,
randomly a winning tickets will be picked.
• For example if there are 100 tickets and one
process holds 20 of them it will have 20 percents
chance to win each lottery. In long run 20
percent of CPU.
• Lottery scheduling solves the problem of
priority scheduling. Because if the priority of
everyone increases no one gets a service.
Real Time Systems Scheduling
•Hard real-time systems – required to complete a critical
task within a guaranteed amount of time.
•Soft real-time computing – requires that critical
processes receive priority over less fortunate ones.

•Real time algorithms can


Static make their scheduling decisions before the
system starts running. Static scheduling only works
when there is perfect information available in advance
about the work to be done and the deadlines that have
to be met
Dynamic make their scheduling decisions at run time

•Real Time OS will use priority, no pre-emption.


Programming Assignment 1–CPU Scheduling
11 March 2024
Design & Implement a program that simulates the Shortest Job
First (SJF) and Round Robin scheduling algorithms.
Assuming that all the jobs will arrive at the same time. Your
program must calculate the
following:
1. Average Waiting Time (AWT)
2. Average Turnaround Time (ATT)

Your program should have two functions, one for SJF and another
for RR. Give a choice to user to select.

You might also like