Sunteți pe pagina 1din 47

APPLIED OPERATING SYSTEM

(AOS)

BEIT-V SEMESTER

Compiled By
Asst. Prof. Madan Kadariya
HOD, Department of Information Technology
Engineering
Nepal College of Information Technology
Balkumari Lalitpur
CHAPTER 2
PROCESS/THREAD MANAGEMENT

Section 2.3. Process Scheduling


OUTLINE
2.3.1 Concepts
2.3.2 Scheduling Criteria

Applied Operating System (AOS)


2.3.3 Scheduling Algorithms
(FCFS, RR, SJF, STRF, priority)
2.3.4 Thread Scheduling

3
SCHEDULING
Basis of multiprogrammed OS
The run of a process: CPU Burst

Applied Operating System (AOS)


The wait for an I/O: I/O Burst

A process with more CPU Burst is called CPU Bound.

A process with more I/O Burst is called I/O Bound.

CPUI/O Burst Cycle Process execution consists of a cycle

of CPU execution and I/O wait.


Both start and end by CPU burst.

Switches between CPU burst and I/O burst multiple times


4
before termination
Applied Operating System (AOS)
5
CPU-I/O BURST CYCLE
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:

Applied Operating System (AOS)


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

6
DISPATCHER
Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this involves:
switching context
switching to user mode

Applied Operating System (AOS)


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

7
SCHEDULING CRITERIA
CPU Utilization:
Average time during which CPU is busy executing user programs and/or
system programs.
The more the better

Applied Operating System (AOS)


Throughput:
Average amount of work completed per unit time
The higher the better
Turnaround Time(TAT):
Time elapsed from time of submission of task to the task it is completed
Total time from time waited to get into the memory, from memory to ready
queue, CPU time and I/O time
Hence,

TAT = process finish time process arrival time


8
Or
TAT = Waiting Time + Burst Time
SCHEDULING CRITERIA
Waiting Time(WT):
Amount of time a process has been waiting in the ready queue
Hence, WT = TAT Processing Time

Applied Operating System (AOS)


Minimum is better.

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)
Minimum is better.
9
OPTIMIZATION CRITERIA
Max CPU utilization
Max throughput

Applied Operating System (AOS)


Min turnaround time

Min waiting time

Min response time

10
SCHEDULING ALGORITHMS GOALS
All Systems:
Fairness:
Fair share of CPU to every process
Policy Enforcement:

Applied Operating System (AOS)


Stated policy is implemented properly
Balance:
Keeping all resources equally busy
Batch System:
Throughput:
Maximize jobs per unit time
Turnaround Time:
Minimization of time between submission and termination
CPU Utilization:
Keep CPU busy all the time
11
SCHEDULING ALGORITHMS GOALS
Interactive System:
Response Time:
Quick response to request

Applied Operating System (AOS)


Proportionality:
Meet users expectation
Real Time System:
Meeting Deadlines:
Avoiding losing data
Predictability:
Avoid quality degradation

12
ALGORITHMS USED IN DIFFERENT
SYSTEMS
First-Come-First-Serve (FCFS)
Shortest Job First (SJF)

Applied Operating System (AOS)


Shortest Remaining Time Next (STRF)
Round Robin (RR)
Priority (Preemptive and Non-preemptive)
Highest Response Ratio Next (HRRN)
Multilevel Queue

13
SCHEDULING ALGORITHMS (TYPES)
Non-Preemptive Algorithm Preemptive Algorithm
Once the CPU is allocated to a CPU can be taken away before the
process, it cannot be taken away from completion

Applied Operating System (AOS)


that process until termination
No preference for higher priority job CPU switches to higher preference
job from lower preference job from
middle
Fair treatment to all the processes Not fair, CPU either switches because
of time constraints or due to higher
priority process
Cheap to implement Costlier to implement 14

E.g.: FCFS algorithm E.g.: Round Robin (RR) algorithm


FIRST-COME-FIRST-SERVED (FCFS)
Allocates the CPU in the order the processes arrive.
Non-preemptive scheduling algorithm

E.g. Process Burst Time

Applied Operating System (AOS)


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 15
FCFS
Suppose that the processes arrive in the order
P2 , P3 , P1 .
The Gantt chart for the schedule is:
P2 P3 P1

Applied Operating System (AOS)


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.
In example 1:
Short processes coming later than long process have to wait more for

CPU. This effect is called Convoy effect 16


FCFS
Advantage:
Simple and easy to implement

Applied Operating System (AOS)


Suitable for batch system
Disadvantage:
AWT is not minimal
Not suitable for time sharing system

17
SHORTEST JOB FIRST (SJF)
Allocate the CPU to the process with least CPU burst
time
If two processes have same CPU burst, then FCFS is

Applied Operating System (AOS)


used to break the tie
Two schemes;
Non Preemptive :
Once CPU given to the process it cannot be preempted until
completes its CPU burst
Preemptive (also called Shortest Remaining First -SRTF):
If a new process arrives with CPU burst length less than remaining
time of current executing process, preempt.
18
SJF (NON-PREEMPTIVE)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4

Applied Operating System (AOS)


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 19


SJF (PREEMPTIVE)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4

Applied Operating System (AOS)


P3 4.0 1
P4 5.0 4
SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16
20
Average waiting time = (9 + 1 + 0 +2)/4 = 3
SJF
Advantages:
Optimal gives minimum average waiting time for a given

Applied Operating System (AOS)


set of processes.
Disadvantages:
Difficultto know the length of next CPU burst time
Big jobs are waiting for CPU, which may result in aging
problem

21
QUESTION:
Make Gantt Chart and Find:
Average Turnaround Time (ATAT)
Average Waiting Time (AWT)
Case1: FCFS

Applied Operating System (AOS)


Case2: SJF (preemptive)
Case3: SJF (non preemptive)

Process Arrival Time(ms) CPU Burst (ms)

P0 0 10
P1 1 6
P2 3 2
22
P3 5 4
PRIORITY SCHEDULING
Gives preferential treatment to important jobs.
Allows the programs with the highest priority to be

processed first

Applied Operating System (AOS)


If two or more jobs with equal priority, then implements

FCFS algorithm
The priority determination is affected by:
Resource requirement
Processing time
Total spent time on the system

23
PRIORITY SCHEDULING
Types:
PreemptivePriority Scheduling

Applied Operating System (AOS)


Non-preemptive Priority Scheduling

24
PRIORITY SCHEDULING
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority
(smallest integer)

Applied Operating System (AOS)


Preemptive
Non-preemptive
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 priority of a
waiting process 25
PREEMPTIVE PRIORITY SCHEDULING
Example:
Process Burst Time Priority Arrival
Time

Applied Operating System (AOS)


P1 10 3 0
P2 5 2 1
P3 2 1 2
Note:
Gantt Priority 1 means highest priority & 3 means lowest priority
Chart P1 P2 P3 P2 P1
0 1 2 4 8 17
AWT = {0+(8-1)+0+(4-2)+0}/3
= 9/3 = 3 ms. 26
PREEMPTIVE PRIORITY (PROBLEMS)
Indefinite blocking of low priority processes by high
priority processes called Starvation

Applied Operating System (AOS)


Completion of a process within finite time is not
guaranteed
Solution:
Aging: Increasing priority of process that has waited for
long time. Older process gets high priority and hence can
complete in finite time.

27
NON-PREEMPTIVE PRIORITY
Example:
Process Arrival Burst Time Priority
Time (AT)

Applied Operating System (AOS)


P0 0 10 5
P1 1 6 4
P2 3 2 2
P3 5 4 0
Gantt
P0 P3 P2 P1
Chart
0 10 14 16 22
AWT = {0+(10-5)+(14-3)+(16-1)}/4
= 31/4 = 7.75 ms. 28
ROUND ROBIN(RR) SCHEDULING
Preemptive FCFS algorithm
Each process gets a small unit of CPU time called time quantum (10-100
milliseconds).
After this time has elapsed, the process is preempted and added to the end of the
ready queue.

Applied Operating System (AOS)


Let,
n = number of processes,
q = time quantum, then
no process runs more than one time slice (i.e. q)
no process waits more than (n-1)*q time units
If a running process releases control due to some I/O, another process is scheduled
to run
Note:
q must be large w.r.t. context switch, otherwise overhead is too high. 29
RR ALGORITHM
Example: Time Quantum = 20 ms
Process Burst Time
P1 53
P2 17

Applied Operating System (AOS)


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.


Calculate: AWT and ATAT yourself 30
TIME QUANTUM AND CONTEXT SWITCH TIME

Applied Operating System (AOS)


31
TURNAROUND TIME VARIES WITH THE TIME QUANTUM

Applied Operating System (AOS)


Fig: Illustration of turnaround time varying with the time
quantum
32
HIGHEST RESPONSE RATIO NEXT (HRRN)
Chooses next process with the greatest ratio (R)
Attractive because it accounts for the age of the process
While shorter jobs are favored, aging without service

Applied Operating System (AOS)


increases the ratio so that a longer process will
eventually get past competing shorter jobs

33
MULTILEVEL QUEUE SCHEDULING
Ready queue is partitioned into separate queues:
foreground(interactive)
background (batch)

Each queue has its own scheduling algorithm,

Applied Operating System (AOS)

foreground RR
background FCFS
Scheduling must be done between the queues.
Fixedpriority 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
34
Applied Operating System (AOS)
35
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

Applied Operating System (AOS)


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
36
EXAMPLE OF MULTILEVEL FEEDBACK
QUEUE
Three queues:
Q0 RR with time quantum 8 milliseconds
Q1 RR time quantum 16 milliseconds

Applied Operating System (AOS)


Q2 FCFS
Scheduling
A new job enters queue Q0 which is served FCFS. When it
gains CPU, job receives 8 ms
If it does not finish in 8 ms, job is moved to queue Q1
At Q1 job is also served FCFS and receives 16 additional ms
If it still does not complete, it is preempted and moved to queue Q2

37
Applied Operating System (AOS)
38
MULTILEVEL FEEDBACK QUEUES
THREAD SCHEDULING
User-level threads and Kernel-level threads
All the scheduling algorithms in thread supporting OS are
Kernel-level scheduling not processes

Applied Operating System (AOS)


User-level threads are managed by a thread library, and the

kernel is unaware of them


For user level thread, kernel only picks a process and threads
from the process can compete for CPU
For Kernel level thread, Kernel picks a thread to be executed on
CPU

39
THREAD SCHEDULING
User Level Thread Kernel Level Thread

Process A Process B Process A Process B

Applied Operating System (AOS)


2. Runtime System picks a thread

1. Kernel Picks a Process 1. Kernel Picks a Thread

Possible: A1, A2, A3, A1, A2, A3 Possible: A1, A2, A3, A1, A2, A3
Not-Possible: A1, B1, A2, B2, A3, B3 Also Possible: A1, B1, A2, B2, A3, B3
40
Q. Consider the following set of processes, with the length of the CPU burst given in
milliseconds:
Process Burst Time Priority

P1 10 3
P2 1 1
P3 2 3

Applied Operating System (AOS)


P4 1 4
P5 5 2
The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0.
1. Draw four Gantt charts that illustrate the execution of these processes using the
following scheduling algorithms: FCFS, SJF, non preemptive priority (a smaller priority
number implies a higher priority), and RR (quantum= 1).
2. What is the turnaround time of each process for each of the scheduling algorithms in
part 1?
3. What is the waiting time of each process for each of these scheduling algorithms?
41
4. Which of the algorithms results in the minimum average waiting time (over all
processes)?
Calculate the Average waiting time and average turn around
time for the following scheduling criteria.
1. FCFS 2. SJF 3. SJF(Preemptive)
4. HRRN 5. RR( Q= 1 and 4) Answer
Criteria Avg TAT
FCFS 8.6 ms

Applied Operating System (AOS)


SJF 7.6 ms
SJF (P) 7.2 ms
HRRN 8 ms
RR(Q=1) 10.8 ms
RR(Q=4) 10 ms

42
Process Scheduling Example
0 5 10 15 20

A
First-Come-First B
Served (FCFS) C
D
E

A
Round-Robin B
(RR), q =1 C
D
E

A
Round-Robin B
(RR), q =4 C

Applied Operating System (AOS)


D
E

A
Shortest Process B
Next (SPN) C
D
E

A
Shortest Remaining B
Time(SRT) C
D
E

A
Highest Response B
Ratio Next (HRRN) C
D
E

A
Feedback B
q =1 C
D
E

A
Feedback B
i
q =2 C
D

43
E

0 5 10 15 20
Figure 9.5 A Comparison of Scheduling Policies
SUMMARY
CPU scheduling is the task of selecting a waiting process from
the ready queue and allocating the CPU to it.
The CPU is allocated to the selected process by the dispatcher.

Applied Operating System (AOS)


First-come, first-served (FCFS) scheduling is the simplest

scheduling algorithm, but it can cause short processes to wait


for very long processes.
Shortest job first (SJF) scheduling is provably optimal,

providing the shortest average waiting time.


Implementing SJF scheduling is difficult, because predicting

the length of the next CPU burst is difficult.


44
SUMMARY
The SJF algorithm is a special case of the general priority scheduling
algorithm, which simply allocates the CPU to the highest-priority
process.
Both priority and SJF scheduling may suffer from starvation.

Applied Operating System (AOS)


Aging is a technique to prevent starvation.
Round-robin (RR) scheduling is more appropriate for a time-shared
(interactive) system.
RR scheduling allocates the CPU to the first process in the ready queue
for q time units, where q is the time quantum.
After q time units, if the process has not relinquished the CPU, it is
preempted, and the process is put at the tail of the ready queue.
If the quantum is too large, RR scheduling degenerates to FCFS
scheduling; if the quantum is too small, scheduling overhead in the form45
of context-switch time becomes excessive.
SUMMARY
The FCFS algorithm is non-preemptive; the RR algorithm is
preemptive.
The SJF and priority algorithms may be either preemptive or
non-preemptive.

Applied Operating System (AOS)


Multilevel queue algorithms allow different algorithms to be
used for different classes of processes.
The most common model includes a foreground interactive

queue that uses RR scheduling and a background batch queue


that uses FCFS scheduling.
Multilevel feedback queues allow processes to move from one
queue to another. 46
End of Chapter 2.3

Applied Operating System (AOS) 47

S-ar putea să vă placă și