Sie sind auf Seite 1von 93

Operating System Algorithms

Nathan Adams
Sun Apr 16 23:28:47 CDT 2017
Chapter 1 - Introduction
Chapter 1.1 - Dedications
Chapter 1.2 - Copyright
Chapter 2 - Process Scheduling
Chapter 2.1 - First Come First Serve
Example 1
Example 2
Example 3
Example 4
Example 5
Exercise Problems
Chapter 2.2 - Shortest Job First (Non-Preemption)
Example 1
Example 2
Example 3
Example 4
Exercise Problems
Chapter 2.3 - Shortest Job First (Preemption)
Example 1
Example 2
Example 3
Example 4
Exercise Problems
Chapter 2.4 - Priority
Example 1
Example 2
Example 3
Example 4
Exercise Problems
Chapter 2.5 - Round Robin
Example 1
Example 2
Example 3
Example 4
Exercise Problems
Chapter 2.6 - Rate Monotonic
Example 1
Example 2
Exercise Problems
Chapter 2.7 - Earliest Deadline First
Example 1
Example 2
Exercise Problems
Chapter 3 - Resource Allocation Graph
Example 1
Example 2
Example 3
Exercise Problems
Problem 1
Problem 2
Chapter 4 - Disk Scheduling
Chapter 4.1 - First Come First Serve
Example 1
Example 2
Example 3
Exercise Problems
Chapter 4.2 - Shortest Seek Time First
Example 1
Example 2
Example 3
Exercise Problems
Chapter 4.3 - SCAN
Example 1
Example 2
Example 3
Exercise Problems
Chapter 4.4 - C-SCAN
Example 1
Example 2
Example 3
Exercise Problems
Chapter 4.5 - LOOK
Example 1
Example 2
Example 3
Exercise Problems
Chapter 4.6 - C-LOOK
Example 1
Example 2
Example 3
Exercise Problems
Chapter 5 - Solutions
Chatper 2.1 - FCFS
Chatper 2.2 - Short Job First (Non-Preemption)
Chatper 2.3 - Short Job First (Preemption)
Chatper 2.4 - Priority
Chatper 2.5 - Round Robin
Chapter 3 - Resource Allocation Graphs
Chapter 4.1 - FCFS
Chapter 4.2 - SSTF
Chapter 4.3 - SCAN
Chapter 4.4 - C-SCAN
Chapter 4.5 - LOOK
Chapter 4.6 - C-LOOK
Chapter 1 - Introduction
After teaching Operating Systems for several years, I have found a lack of
supporting material for the algorithms that are presented in Operating System
books.

The purpose of this book is not to replace current Operating System textbooks
or be a replacement for current Operating System Algorithm books. This book
is simply to augment them with more indepth step by step examples.

This book will not contain every algorithm known to mankind related to
operating systems - the basis for which algorithms that are present in this book
are ones that are presented in Operating System Concepts by Abraham
Silberschatz and Peter B. Galvin.

If you feel an important algorithm is missing please contact me at:

nathan [at] allostech.com

I may add it and give you a free updated ebook version.

Please also contact me if you find something incorrect or dont agree with -
Im not a robot and I do make mistakes.

Please do NOT contact me for homework/class help unless you are willing to
pay a hourly fee (down payment is required before session begins). Requests
for free help will be promptly ignored and your email may be blacklisted.

Finally - please do not pirate this book. Instead, refer students to contact me
for a free digital copy.

If you want to fund future books or continuing support of this book, please feel
free to send donations to nathan [at] allostech.com or via Bitcoin -
1NHczVQN2hUAMXLGgXnniR3CRjQ9SGgyFn

Date of book creation: Sun Apr 16 23:28:47 CDT 2017


Chapter 1.1 - Dedications
I would like to thank the following people that made this book possible:

Alex Larson - without his help with reviewing this book and helping me
with my other ventures, this book may never have come to fruition.
Dr. Ray Klump - amazing professor who gave me a chance to teach some
classes which gave me the inspiration to write this book.
Elisha Chirchir - helped me create the graphics for the disk scheduling
Dan Bewley and Eric Cedergren - some of my students who pointed out
some typos and mistakes in the book
Chapter 1.2 - Copyright
Operating System Algorithms

Copyright (c) 2017 Nathan Adams

All rights reserved. No part of this book may be reproduced in any form or by
any electronic or mechanical means, including information storage and
retrieval systems, without permission in writing from the publisher, except by
reviewers, who may quote breif passages in a review.

ISBN 978-1-545146262

Visit https://allostech.com
Chapter 2 - Process Scheduling
A modern operating system needs a way to prioritize processes due to the
simple fact that there will probably be many different processes running at
once.

There are several algorithms that an operating system may use to schedule a set
of processes. They include:

First Come First Serve (FCFS)


Shortest Job First (SJFS) Non-preemption
SJFS preemption
Priority
Round Robin

Those algorithms listed do not offer any guarantees on if or when a process


will run. This works for desktop PCs but will not work for systems which
require a guarantee that the process will have a chance to run. This is where
real time operating systems come into play. For example: your desktop is
probably running a non-real time operating system such as Windows, Linux or
Mac OS. A real time operating system is used in, for example, your car
because when you want to make sure there will be a time when the ABS
system kicks in.

A real time operating system may run off of 2 algorithms:

Rate Monotonic
Earilest Deadline First

Each of these algorithms will contain, at a minimum, a set of processes, and


related burst times. Some algorithms will contain a priority, or time quantum.
The burst time usually is a unit of milliseconds but it can really be any time
unit.
Chapter 2.1 - First Come First Serve
First Come First Serve (or FCFS) is the simplest of the algorithms. Basically
any process that comes in first is given time on the processor. Processes are
not preempted in FCFS.

Preemption is the ability for an operating system to pause a process in favor of


another process - either because of a higher priority or that the process
satisfies a condition that the operating system considers it more important.

Example 1

Process Burst Time (ms)


P1 24
P2 3
P3 3

In FCFS algorithm, we will schedule this set of processes starting from P1 to


P3 - ie

< P1, P2, P3 >

When scheduling processes, we will use a gantt chart. A gantt chart is simply a
way to layout schedules in boxes. It is commonly used in software engineering
to plan projects and see if there are any overlapping task schedules which need
more resources.

In this book I lay out the gantt charts in a verbose way so you can see the
individual steps (or the work) taken to get to the final answer. You can skip to
the last step when you do the practice exercises if you find that is easier for
you.

We will begin scheduling with Process 1 and on the left we label 0 (because it
starts at time 0) then 24 on the right. In FCFS the algorithm states that the
process will run until it is finished. Process 1 has a burst time of 24 which is
why we put down 24 on the right.

Next we want to schedule Process 2. Process 2 has a burst time of 3. We draw


a box to the right of Process 1. Process 1 ended at time 24 and since Process 2
has a burst time of 3 we are now at time 27 (24 + 3).

Finally we schedule Process 3. Process 3 also has a burst time of 3. After


Process 3 runs that will put us at a time of 30.

Usually the reason why people perform a gantt chart like this is to determine
the average wait time (or AWT). The AWT is a metric that you can use to
determine how efficient an algorithm is. FCFS is similar to the bubble sort in
the sense that it is usually one of the worst performing algorithms. But it is
important to have a base to compare from.

To calculate AWT you just need to add up the times and divide by the number
of processes. In this example you just take 0 (which is the beginning of
scheduling), 24 (which is the end of Process 1), then finally 27 (which is the
end of Process 2). We do not factor in 30 (end of Process 3) because in this
example no other processes were waiting to run after Process 3. We will add
up those numbers and divide by 3 because there are 3 processes.

The calculation is as follows:


(0 + 24 + 27) / 3 = 17ms AWT

Example 2
In this example we will use the same process schedule but changed the order in
which the processes are scheduled. This will give you the ability to see how
the order of the processes will influence the AWT.

Process Burst Time (ms)


P1 24
P2 3
P3 3

< P2, P3, P1 >

We are pushing the longer running process to the last.

Just like above we start with Process 2 - starts at time 0 and runs its full burst
ending at time 3.

Then we schedule Process 3. Process 3 has a burst time of 3 which puts us at


time 6.

Then finally Process 1. Which leads us at time 30.


To calculate the AWT we take 0 (which is the beginning), 3 (which is the time
that Process 2 ended and Process 3 began), and 6 (which is the time that
Process 3 ended).

To calculate the AWT:

( 0 + 3 + 6) / 3 = 3ms AWT

Again we divide by 3 because there were 3 processes.

Even though we used the same set of processes from the previous example, you
can see that changing the order of the processes will have an impact on the
AWT.

Example 3

Process Burst Time (ms)


P1 8
P2 5
P3 3

< P1, P2, P3 >


The AWT is:

( 0 + 8 + 13 ) / 3 = 7ms AWT

Example 4

Process Burst Time (ms)


P1 8
P2 5
P3 3

< P2, P1, P3 >


The AWT is:

( 0 + 5 + 13 ) / 3 = 6ms AWT

Example 5

Process Burst Time (ms)


P1 8
P2 5
P3 3

< P3, P2, P1 >

The AWT is:

( 0 + 3 + 8 ) / 3 = 3.667 ms AWT
Exercise Problems
Given the following process schedules, calculate the AWT.

Problem 1

Process Burst Time (ms)


P1 7
P2 4
P3 10

< P1, P2, P3 >

Problem 2

Process Burst Time (ms)


P1 7
P2 4
P3 10

< P3, P1, P2 >

Problem 3

Process Burst Time (ms)


P1 2
P2 9
P3 4

< P1, P2, P3 >


Problem 4

Process Burst Time (ms)


P1 7
P2 5
P3 3

< P1, P2, P3 >


Chapter 2.2 - Shortest Job First (Non-
Preemption)
Shortest job first non-preemption is pretty easy and straight forward. The
algorithm picks which ever process has the shortest burst time and schedules
that one.

For example if you have Process 1 with a burst time of 2, and Process 2 with a
burst time of 3 - the algorithm would schedule Process 2 first, then Process 1.
In the case of a tie the algorithms states to just use FCFS algorithm. In the
case of Process 1 and Process 2 with the burst time of 2 - using FCFS you
would schedule Process 1 then Process 2.

It is important to point out that there is another form of this algorithm which
uses preemption. Recall preemption is the idea where the operating system has
the ability to pause or stop a process in favor of another one.

Example 1

Process Burst Time (ms)


P1 6
P2 8
P3 7
P4 3

The first process to be scheduled would be Process 4 because its burst time is
the lowest (3) compared to the burst times of other processes.
Just like in FCFS, the process chosen will run through its full burst. The next
process to be scheduled would be Process 1 because its burst time is lower
than 8 and 7.

Next we would schedule Process 3.

Then finally Process 2.

Calculating AWT is the same as calculating it for FCFS.


( 0 + 3 + 9 + 16 ) / 4 = 7 ms

Please do note that we are dividing by 4 in this case because there are 4
processes

Example 2

Process Burst Time (ms)


P1 6
P2 2
P3 5
P4 3

In this set of processes we would schedule the processes in this order:

P2 (lowest burst time), P4, P3, P1 (highest burst time)

The gantt chart would look like the following:

The AWT would be:


( 0 + 2 + 5 + 10 ) / 4 = 4.25ms

Example 3

Process Burst Time (ms)


P1 2
P2 5
P3 7
P4 2

This is an interesting example because you can see that Process 1 and Process
4 share the same burst time (as well as the lowest).

From our previous example, according to the algorithm, the way we break this
tie is to use FCFS. So we would schedule Process 1 first then Process 4.

The AWT is:


( 0 + 2 + 4 + 9 ) / 4 = 3.75ms AWT

Example 4

Process Burst Time (ms)


P1 4
P2 8
P3 6
P4 7

The AWT is:


( 0 + 4 + 10 + 17 ) / 4 = 7.75ms

Exercise Problems
Given the following process schedules, calculate the AWT.

Problem 1

Process Burst Time (ms)


P1 7
P2 4
P3 10
P4 2

Problem 2

Process Burst Time (ms)


P1 3
P2 7
P3 7
P4 10

Problem 3

Process Burst Time (ms)


P1 4
P2 9
P3 2
P4 3

Problem 4

Process Burst Time (ms)


P1 4
P2 7
P3 4
P4 2
Chapter 2.3 - Shortest Job First
(Preemption)
Shortest Job First with preemption is basically SJFS (non-preemption) with
the addition of another column: Arrival

The arrival column is pretty important as it lets us know if we need to preempt


a running process in favor of another. The arrival gives us at which time a
process comes in.

For example - if a process has arrival time of 0 - this means it is the first
process running. If 2 processes both have the same arrival time then you will
use FCFS to break the tie.

Since a process can be preempted we need to take into account the arrival time
and finish when calculating the average wait time. This can easily be done by
applying the formula of:

Where F is the final time, A is the arrival time and B is the burst time.

Or more simply:
W(time) = ( F(inal) - A(rrival) ) - B(urst)

Or:
W = ( F - A ) - B

Where the Wait time is equal to the ( Finish - Arrival ) - Original Burst Time.
Perform this formula for each process, add them together, and divide by the
number of processes and you will get the average wait time. The reason we do
this is to take into account the arrival time. Lets examine the following
process schedule:

Process Arrival Burst Time (ms)


P1 0 8
P2 8 4

P1 will be scheduled first because its arrival is 0 even though its burst time is
higher than P2 - P2 wont appear to be scheduled for 8 ms right after P1 has
finished. Therefore, P2 did not need to wait to run so its awt is 0.

We start the gantt chart with P1

Then schedule P2

P1 wait time:

( 8 - 0 ) - 8 = 8 - 8 = 0

P2 wait time:

( 12 - 8 ) - 4 = ( 4 ) - 4 = 0

The average wait time would be:


( 0 + 0 ) / 2 = 0 ms
0 ms wait time makes perfect sense because neither process had to wait to run.

If we didnt use the formula:

( 0 + 8 ) / 2 = 4ms

Which states that both processes had to wait on average 4ms to run which is
not correct.

Note: If you apply the formula and find that a process has a negative wait
time - then you applied the formula incorrectly. If a process has a negative
wait time it makes as much sense as a planet having negative moons.

Example 1
Examine the following process schedule:

Process Arrival Burst Time (ms)


P1 0 8
P2 1 4
P3 2 9
P4 3 5

We will start scheduling at P1 but at 1ms P2 will be entered to be scheduled. At


1ms P1 as 7ms left in its burst time (its burst time was 8 but ran for 1). Then
P2s burst time is 4 which is less than P1s remaining burst time.

Then the gantt chart would look lik this:

At 1ms P1 will be preempted for P2 because P2s burst time is lower than P1s.
At this point you can imagine the process schedule to look like this:

Process Arrival Burst Time (ms)


P1 0 7
P2 1 4

Now examining the rest of the processes. At 2ms P3 will be introduced to the
operating system. At that time you can imagine the process schedule to look
like this:

Process Arrival Burst Time (ms)


P1 0 7
P2 1 3
P3 2 9

You will notice that P3s burst time is higher than P2 so P2 will not be
preempted and P3 will have to wait to run. Looking ahead further to at time
3ms P4 -

Process Arrival Burst Time (ms)


P1 0 7
P2 1 1
P3 2 9
P4 3 5

P2 still has the lowest burst time compared to the other processes. P2 will run
its full burst and since the other processes have been submitted it becomes
SJFS (Non-preemption) with FCFS if there is a tie.

The order in which the processes would be scheduled are:

< P1, P2, P4 ,P1 ,P3 >


The gantt chart would look like this:

As you recall the formula for average wait time when using preemption is

Or:
W = ( F - A ) - B

P1w = ( 17 - 0 ) - 8 = 9

P2w = ( 5 - 1 ) - 4 = 0

P3w = ( 26 - 2 ) - 9 = 15

P4w = ( 10 - 3 ) - 5 = 2

The average wait time for this process schedule is


( 9 + 0 + 15 + 2 ) / 4 = 6.5ms

Example 2

Process Arrival Burst Time (ms)


P1 0 8
P2 1 4
P3 2 2
P4 3 5
In this example you can see its the same idea as the previous example. We
start out with P1 but at 1ms P2 is started on the system. P2 has a burst time of 4
which is less than P1s burst time at time = 1ms (which would be 7). So P1 is
preempted for P2.

Looking ahead - at time 2ms - P3 is introduced. Which has a burst time of 2. At


Time 2ms P2s remaining burst time would be 3. P2 will be preempted for P3.

The full gantt chart breakdown would be as follows:


To calculate the average wait time just apply the formula:
W = ( F - A ) - B

P1w = ( 19 - 0 ) - 8 = 11

P2w = ( 7 - 1 ) - 4 = 2

P3w = ( 4 - 2 ) - 2 = 0

P4w = ( 12 - 3 ) - 5 = 4

The average wait time for this process schedule is


( 11 + 2 + 0 + 4 ) / 4 = 4.25ms

Example 3

Process Arrival Burst Time (ms)


P1 0 5
P2 1 7
P3 2 8
P4 3 9

In this example you will notice that P1s burst time is 5ms. If you look ahead
you will notice that if the 3 other processes are introduced then P1s burst time
is still the lowest.

Essentially for scheduling you can ignore the arrival time and treat it like a
normal SJFS but you must still apply the formula to determine the average wait
time.
Here is the resulting gantt chart:

The average wait time would be:

P1w = ( 5 - 0 ) - 5 = 0

P2w = ( 12 - 1 ) - 7 = 4

P3w = ( 20 - 2 ) - 8 = 10

P4w = ( 29 - 3 ) - 9 = 17

AWT = ( 0 + 4 + 10 + 17 ) / 4 = 7.75ms

Example 4

Process Arrival Burst Time (ms)


P1 0 8
P2 1 9
P3 2 4
P4 3 5

In this process schedule we would schedule P1 first. At time 1ms P2 is


introduced with a burst time of 9ms which is less than P1s remaining burst
time of 7 (it ran for 1ms once it reached arrival time of 1).

However, when we reach P3 - P1s remaining burst time would be 6ms. P3 has
a burst time of 4 which is less than P1s remaining burst time. At this time P1 is
preempted in favor of P3.

When P4 is introduced P3s remaining burst time would be 3 which is less than
P4s burst time. So P3 would run through its full burst.

After that it becomes a simple SJFS where after P1 and P3 we then schedule P4,
the remainder of P1, and then finally P2.

Here is the resulting gantt chart:


The average wait time would be:

P1w = ( 17 - 0 ) - 8 = 9

P2w = ( 26 - 1 ) - 9 = 16

P3w = ( 6 - 2 ) - 4 = 0

P4w = ( 11 - 3 ) - 5 = 3

AWT = ( 9 + 16 + 0 + 3 ) / 4 = 7ms

Exercise Problems
Given the following process schedules calculate the AWT.

Problem 1

Process Arrival Burst Time (ms)


P1 0 4
P2 1 8
P3 2 3
P4 3 5

Problem 2

Process Arrival Burst Time (ms)


P1 0 4
P2 1 2
P3 2 7
P4 3 5

Problem 3

Process Arrival Burst Time (ms)


P1 0 5
P2 1 2
P3 2 3
P4 3 5

Problem 4

Process Arrival Burst Time (ms)


P1 0 9
P2 1 4
P3 2 8
P4 3 3
Chapter 2.4 - Priority
Priority is a simple algorithm similar to SJFS. Instead of scheduling by
shortest burst time, we are scheduling based on priority. In the following
examples we will assume the lowest priority is the highest or more important -
but it is certainly valid for a higher number to be the higher priority.

As you can probably guess we introduce a new column called priority which
has a number value determining on the priority of the process.

Priority schedule does not have preemption so you can calculate the average
wait time using the same method in FCFS or SJFS (Non-preemption).

If there are 2 processes with the same priority then you would break the tie
using FCFS.

Example 1

Process Burst Time (ms) Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

If we assume that the lower number is the higher priority then we can see that
the first process to be scheduled would be P2 with a priority of 1.

As a result the order in which we would schedule these processes are:

< P2, P5, P1, P3, P4 >

There is no preemption so processes will run their full burst before the next
process is scheduled. And as a result of no preemption we do not use the
formula W = ( F - A ) - B from SJFS Preemption.

The full gantt chart would be:

The average wait time is calculated just like we would in FCFS:


( 0 + 1 + 6 + 16 + 18 ) / 5 = 8.2ms

Example 2
Process Burst Time (ms) Priority
P1 4 1
P2 3 3
P3 2 4
P4 5 2

While this set of schedules only has 4 processes you apply the algorithm the
same way.

We will start with P1 because that has the highest priority (lowest number),
then P4, P2, and finally P3.

So in order:

< P1, P4, P2, P3 >

The gantt chart would look like the following:


The average wait time is:
( 0 + 4 + 9 + 12 ) / 4 = 6.25ms

Example 3

Process Burst Time (ms) Priority


P1 5 3
P2 4 2
P3 9 1
P4 3 4
P5 1 5

In this example we would schedule P3 first, then P2, P1, P4, and finally P5.

In order:

< P3, P2, P1, P4, P5 >

The resulting gantt chart would be:


The average wait time is:
( 0 + 9 + 13 + 18 + 21 ) / 5 = 12.2ms

Example 4

Process Burst Time (ms) Priority


P1 5 3
P2 5 2
P3 7 1
P4 10 4

The gantt chart for this set of processes is:


The average wait time is:
( 0 + 10 + 13 + 18 ) / 4 = 10.25ms

Exercise Problems
Given the following process schedules calculate the AWT.

Problem 1

Process Burst Time (ms) Priority


P1 7 1
P2 4 2
P3 10 3
P4 2 4

Problem 2

Process Burst Time (ms) Priority


P1 3 4
P2 2 1
P3 8 3
P4 4 2

Problem 3

Process Burst Time (ms) Priority


P1 1 4
P2 5 1
P3 7 3
P4 2 2

Problem 4

Process Burst Time (ms) Priority


P1 8 4
P2 1 1
P3 2 3
P4 9 2
Chapter 2.5 - Round Robin
Round robin is similar to FCFS but with a slight modification. In this algorithm
we introduce the idea of a time quantum or sometimes referred to as a time
slice. A time quantum is a more fair way of giving processes processor time.

It works simply starting out like you would with FCFS - schedule the first
process on the list. However, if that process burst time is greater than the time
quantum then it is preempted for the next process. If the burst time is less than
or equal to the time quantum then it will run its full burst without being
preempted.

If a process burst time or remaining burst time is less than the time quantum it
will run its full burst and continue onto the next process. The algorithm will not
pad the difference. For example, if the time quantum was 3ms and the
process had a burst time of 2ms - the process would run for 2ms and continue
onto the next process without pausing for that extra 1ms.

Since this algorithm has the potential for preemption you must apply a
simplified version of the formula from SJFS preemption:
w = ( F - A ) - B

In round robin (at least in these examples) it has no concept of arrival - all
processes are assumed to arrive at the same time (so at time 0ms). This fact
allows you to simplify the formula to just:

Or:
w = F - B
Example 1
time quantum = 4ms

Process Burst Time (ms)


P1 24
P2 3
P3 3

In this example we will start with P1 - but since its burst time is 24ms it will
be preempted when the time reaches 4ms to give 4ms of processor time to P2.
Since P2s burst time is 3ms which is less than or equal to the given time
quantum of 4ms it will run its full burst without being preempted. After that we
will schedule P3, but keep in mind P1 still has 20ms burst time left. P3 also has
a burst time 3 which means it will run its full burst. After P3 we will schedule
P1 in 4ms bursts.

The gantt chart for this example would be:


The average wait time would be:

P1w = 30 - 24 = 6

P2w = 7 - 3 = 4

P3w = 10 - 3 = 7

AWT = ( 6 + 4 + 7 ) / 3 = 5.66ms

Example 2
time quantum = 5ms
Process Burst Time (ms)
P1 10
P2 7
P3 5

In this example the time quantum is 5ms so we will schedule the first process
P1 for 5ms, preempted it for P2, and preempt P2 for P3 then the scheduling
would repeat back starting with P1.

The gantt chart for this example would be:

The average wait time would be:


P1w = 20 - 10 = 10

P2w = 22 - 7 = 15

P3w = 15 - 5 = 10

AWT = ( 10 + 15 + 10 ) / 3 = 11.67ms

Example 3
time quantum = 4ms

Process Burst Time (ms)


P1 8
P2 4
P3 3

The gantt chart for this example would be:


The average wait time would be:

P1w = 15 - 8 = 7

P2w = 8 - 4 = 4

P3w = 11 - 3 = 8

AWT = ( 7 + 4 + 8 ) / 3 = 6.33ms

Example 4
time quantum = 2ms

Process Burst Time (ms)


P1 5
P2 6
P3 3

The gantt chart for this example would be:

The average wait time would be:

P1w = 12 - 5 = 7
P2w = 14 - 6 = 8

P3w = 11 - 3 = 8

AWT = ( 7 + 8 + 8 ) / 3 = 7.66ms

Exercise Problems
Given the following process schedules calculate the AWT.

Problem 1

time quantum = 2ms

Process Burst Time (ms)


P1 8
P2 7
P3 6

Problem 2

time quantum = 3ms

Process Burst Time (ms)


P1 8
P2 7
P3 6

Problem 3

time quantum = 4ms

Process Burst Time (ms)


P1 8
P2 7
P3 6

Problem 4

time quantum = 5ms

Process Burst Time (ms)


P1 8
P2 7
P3 6
Chapter 2.6 - Rate Monotonic
Rate Monotonic and Earliest Deadline First are both algorithms desiged for
real time operating systems (RTOS). The idea behind a RTOS is that given a
set of processes that they can and will be scheduled. Failure to schedule a
given process in its given period is the equivalent of the RTOS failing.

I will say that Rate Monotonic and Earliest Deadline First can be confusing
due to the lack of information out there but I think the content in this book will
help you.

Before you even attempt to schedule Rate Monotonic you can apply a formula
to determine if the set of schedules can be scheduled with Rate Monotonic.

It is important to point out that if the formula passes - then the set of
processes can be scheduled with Rate Monotonic. If it fails - it is not a
guarantee that it cannot be scheduled. In order words its a way to prove
it can be scheduled rather than cant.

That formula is expressed as the following:

Its simply the sum of the burst times divided by the periods less than or equal
to n times (21/2 - 1).

Processes may be preempted in either algorithm (though it is rare). In RTOS


we dont care about average wait time, the only thing we care about is if the
set of processes can actually be scheduled or not.

One important note is that unlike the previous algorithms a real time algorithm
will continue forever. Even though the processes have a fixed burst time - their
period indicates that they will run again.
In rate Monotonic the shorter the period the higher the priority.

Note: Sometimes the period is referenced as its deadline

Example 1

Process Burst Time (ms) Period (ms)


P1 20 50
P2 35 100

In this example we only have 2 processes - P1 has a burst time of 20 and runs
every 50 ms and P2 has a burst time of 35 ms and a period of 100 ms.

That means that P1 needs to run for a total of 20 ms every 50 ms and P2 needs
to run for 35 ms every 100 ms.

Lets apply the formula to determine if we can schedule this set of processes
using Rate Monotonic (keeping in mind that if the formula fails that it may still
be schedulable).

Or

.75 .828

So yes this should be schedulable. So lets take a look at the gantt chart starting
with P1. P1 has a burst time of 20 ms.
Then we will schedule P2 with a burst time of 35 ms.

Hopefully you will notice that we stopped short at 50 ms. The reason for this is
due to the fact that the period for P1 is 50 ms which is less than P2s period of
100 ms. This gives priority to P1. So we preempt P2 with a remaining burst
time of 5 ms. So we schedule P1:

Then we schedule the rest of P2.


At this point P1 is the next process to run but it wont run until 100 ms. So we
have dead time until 100 ms. Dead time can be represented in a number of
ways - we will show it as black.

Then we will scheduled P1 again and the cycle will repeat.

Example 2

Process Burst Time (ms) Period (ms)


P1 1 8
P2 2 5
P3 2 10

Again lets apply the formula as a check to see if this can be scheduled:

Which will simplify to:

.725 .77976

Lets see how the gantt chart will look like.


Starting with P2 because its period is the shortest.

Then we schedule P1 because it has the second lowest period.

And finally P3. So far so good - we were able to schedule all 3 processes
within in their periods. And no processes were overlapping which would
require us to determine if a process needs to be preempted.

It just happens that at time 5 ms is when P2 will begin its next period so we
schedule that. P2 has a burst time of 2 ms which puts us at 7 ms.
The next process that will run is P1 at time 8 ms (its period is 8 ms). But we
are current at time 7 ms - so we have dead time for 1 ms. After the dead
time we can schedule P1.

Exercise Problems
Given the following process schedules determine if you can schedule it with
Rate Monotonic scheduling.

Remember: Just because the formula fails doesnt mean that you cant
schedule it

Problem 1

Process Burst Time (ms) Period (ms)


P1 20 40
P2 35 100

Problem 2
Process Burst Time (ms) Period (ms)
P1 4 20
P2 2 8
P3 2 10
Chapter 2.7 - Earliest Deadline First
Earliest Deadline First (or EDF) is a dynamic scheduling algorithm. Compared
to RMS (or Rate Monotonic Scheduling) which is a static scheduling
algorithm.

Just like Rate Monotonic - EDF has a formula to determine if a set of


processes can be scheduled. But in EDF is is a true test if it can be scheduled.
In order words if the formula fails then the processes cannot be scheduled with
EDF.

Its similar to the formula for Rate Monotonic where C is the burst times, t is
the priod and n is the number of processes - but in this case you are comparing
against 1.

The technical difference between the two algorithms is that RMS will assign a
priority based on the shortest period, EDF will dynamically determine priority
based on the earliest deadline.

Example 1

Process Burst Time (ms) Period (ms)


P1 1 8
P2 2 5
P3 4 10

If we apply the formula to determine if we can schedule this using EDF:


.925 1

The formula passes so we can schedule it using EDF.

The process that we will schedule first is P2 because its period is the shortest
among the 3 processes.

Next we will schedule P1:

And finally P3. But, do take note that scheduling P3 will overlap the period for
P2 (before we schedule P3 we are at time 3 ms - scheduling P3 would put us at
time 7 ms). The question should arise if we should preempt P3 for P2? At this
time the answer is no. Simply because P3 and P2 both have the same next
periods so a tie occurs and the way we break ties in previous algorithms is to
use FCFS. In this case, P3 will not be preempted and run its full burst.
The next process that we will schedule would be P2. Its period was at 5 ms but
P3 ran and was not preempted. P2 will run its full burst and not be preempted.

At this point you should notice that P2 crosses over P1s period. But P2 was not
preempted because its deadline is closer than P1.

Next we will schedule P1.

After scheduling P1 this will put us at time 10 ms. Both P2 and P3 have the
same period, however, P2s period is shorter so we schedule that first then P3.
Example 2

Process Burst Time (ms) Period (ms)


P1 25 50
P2 35 80

If we apply the formula:

.9375 1

The formula check passes.

To start we will schedule P1 because its next period is sooner than P2. (50 vs
80)

Next we will schedule P2. P2 will cross over into P1s period but this will not
preempt P2. The reason is because P2s next period is 80 vs P1s of 100. So P2
will run its full burst.

At this point we will schedule P1. It will cross into the deadline of P2 but it
will not be preempted because P1s next deadline is 100 vs P2s of 160.

Now we will schedule P2 but P2 crosses into P1s deadline. P2 will be


preempted because P1s next deadline is 150 which is less than P2s deadline
of 160.

And finally we will end this example showing that after finishing the rest of the
burst from the preempted P2 - there is some dead time.
Exercise Problems
Given the following process schedules, determine if you can schedule it with
Earliest Deadline First scheduling.

Problem 1

Process Burst Time (ms) Period (ms)


P1 20 40
P2 35 100

Problem 2

Process Burst Time (ms) Period (ms)


P1 4 20
P2 2 8
P3 2 10
Chapter 3 - Resource Allocation
Graph
A resource allocation graph is a graph that depicts resources and processes.
The purpose of it is to allow you to determine if a dead exists in a given set of
processes and resources.

They are very easy to read and interpret. The important thing to remember is
that a cycle is an indication of a deadlock but is not a guarantee.

In a resource allocation graph, a process can hold and request any number
of resources. Processes are denoted using a circle. Resources are denoted
using a square with a number of dots on the inside related to the count of slots
that the resource has (for example - a CD Rom driver might control multiple
CD-ROMs).

A hold is denoted by an arrow coming from the dot to the process. A


request is denoted by an arrow from the outside of the process to the outside
of the resource box.
Example 1
In this example lets say we have 2 resources. Resource 1 has one slot:

R1 -> 1

P2 also has one slot:

P2 -> 1

P1 holds R1 and P2 holds R2.

The resource allocation graph in this case would look like the following:

In this case no process is waiting on a resource that the other process is


holding. Each process can finish which means that there is no deadlock in this
case.
Example 2
Now lets examine a deadlock situation.

R1 -> 1

R2 -> 1

P1 holds R1 and requests R2 P2 holds R2 and requests R1

In this case there is a deadlock because in order for P1 to finish it needs a


resource held by P2. However, in order for P2 to finish it needs a resource
currently held by P1.
Example 3
In this example we will see a cycle but there will not be a deadlock.

R1 -> 2

R2 -> 2

P1 holds R2 requesting R1

P2 holds R1

P3 holds R1 requesting R2

P4 holds R2
In this example we have a cycle of:

P1 -> R1 -> P3 -> R2 -> P1

But this is not a deadlock because P2 can finish and release R1 allowing P1 to
finish or P4 can finish and release R2 allowing P3 to finish.
Exercise Problems
Given the following scenarios determine if there is a dead lock using a
resource allocation graph.

Problem 1
R1 -> 1

R2 -> 2

R3 -> 1

R3 -> 3

P1 holds R2 requesting R1

P2 holds R2 + R1 requesting R3

P3 holds R3

Problem 2
R1 -> 1

R2 -> 2

R3 -> 1

R3 -> 3

P1 holds R2 requesting R1
P2 holds R2 + R1 requesting R3

P3 holds R3 requesting R2
Chapter 4 - Disk Scheduling
Hard disk technology has changed over the years. The technology that I grew
up with involved an arm and spinning platters. The idea was that a platter was
divided into tracks, then a track is divided up into sectors. Many of these
drives would spin at 5400, 7200, 10000, and even 15000RPM.

However, with the introduction of solid state disk (SSD) really changed the
hard disk as we knew it. There is no longer a spinning media (and as a result
the algorithms may not apply) and data access times are almost instant.

Just like the process scheduling algorithms - the following algorithms were
designed to try to access data in the quickest and most efficient way possible.
The resulting calculation of these algorithms is called total head movements.
This is a number that represents the total number of the movements the head
would make if it was accessing data in a real hard drive.

These algorithms will always state the starting position of the head - but it will
sometimes state the beginning and end positions.

To determine how efficient an algorithm is - we create a graph that shows the


movement of the head and total up the total head movements of the arm as it
fills requests.
Chapter 4.1 - First Come First Serve
First Come First Serve, First in First out (or FCFS/FIFO) is of the simplest
algorithms. Basically you start from the beginning position and move to the
next. Adding up the total movements as you service the request at that
position.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53

To calculate the total head movements we will add up the distances between
when the line changes direction.

| 183 - 53 | = 130
| 183 - 37 | = 146
| 122 - 37 | = 85
| 122 - 14 | = 108
| 124 - 14 | = 110
| 124 - 65 | = 59
| 65 - 67 | = 2
= 640 total head movements
Example 2
queue = 23, 89, 132, 42, 187
start = 100

| 100 - 23 | = 77
| 132 - 23 | = 109
| 132 - 42 | = 90
| 187 - 42 | = 145
= 421 total head movements

Example 3
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50
| 180 - 50 | = 130
| 180 - 34 | = 146
| 119 - 34 | = 85
| 119 - 11 | = 108
| 123 - 11 | = 112
| 123 - 62 | = 61
| 64 - 62 | = 2
= 644 total head movements

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40

Problem 4

queue = 40, 98, 114, 20, 126, 109, 99, 26


start = 8
Chapter 4.2 - Shortest Seek Time First
Shortest Seek Time First (or SSTF) is an algorithm which will serve the
closest request first in relation to the current position of the head.

A quick example: if the head is currently at 100 and 30, 101, 104 are in the
queue it will service 101, 104, then 30.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53

The easiest way to determine which is the closest request is to put the requests
in order:

14, 37, 65, 67, 98, 122, 124, 183

The starting position is 53 and it is closest to 65. To show this you take 53 and
subtract from the request. So the 2 that are closest to 53 is 37 and 65. | 53 - 37 |
= 26 and | 53 - 65 | = 12 . So the algorithm will start at 65 and service the next
closest which would be 67, then 37, 14, 98, 122, 124 and finally 183.

| 53 - 67 | = 14
| 67 - 14 | = 53
| 183 - 14 | = 169
= 236 total head movements

Example 2
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50

Putting them in order:

11, 34, 62, 64, 95, 119, 123, 180

| 50 - 64 | = 14
| 64 - 11 | = 53
| 180 - 11 | = 169
= 236 total head movements

Example 3
queue = 23, 89, 132, 42, 187
start = 100

23, 42, 89, 132, 187


| 100 - 89 | = 11
| 187 - 89 | = 98
| 187 - 23 | = 164
= 273 total head movements

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40

Problem 4

queue = 40, 98, 114, 20, 126, 109, 94, 26


start = 8
Chapter 4.3 - SCAN
SCAN is an algorithm that will service requests to the nearest end. Usually
tracks start at 0 and the end track is given.

For example if the head is currently at 180 - it will go to 199 and service all
requests in that direction.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53
end = 199

Since the head starts at position 53 it will go towards 0 since it is closer to 0


than 199. The easiest way to see which requests to service is to put them in
order again:
14, 37, 65, 67, 98, 122, 124, 183

Then we will travel to 0 servicing 37 and 14.

The total head movement can be calculated simply:

| 53 - 0 | = 53
| 183 - 0 | = 183
= 236 total head movements

Example 2
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50
end = 199

Putting them in order:

11, 34, 62, 64, 95, 119, 123, 180

| 50 - 0 | = 50
| 180 - 0 | = 180
= 230 total head movements

Example 3
queue = 23, 89, 132, 42, 187
start = 100
end = 199

23, 42, 89, 132, 187


| 100 - 0 | = 100
| 187 - 0 | = 187
= 287 total head movements

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33
end = 199

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16
end = 199

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40
end = 199

Problem 4

queue = 40, 98, 114, 20, 126, 109, 94, 26


start = 8
end = 199
Chapter 4.4 - C-SCAN
C-SCAN algorithm is very similar to SCAN in the sense that it will still go
and hit the closest end but it jumps to the other end and services going in the
opposite direction.

For example if the head starts at 100 - it will service all requests down to 0.
But then jump up to 199 and service all requests going back down to 0. You
should not include the distance of the jump in the total head movements.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53
end = 199

Since the head starts at position 53 it will go towards 0 again but after it hits 0
it will jump directly to 199.
14, 37, 65, 67, 98, 122, 124, 183

The total head movement can be calculated simply:

| 53 - 0 | = 53
| 199 - 65 | = 134
= 187 total head movements
Example 2
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50
end = 199

Putting them in order:

11, 34, 62, 64, 95, 119, 123, 180

| 50 - 0 | = 50
| 199 - 62 | = 137
= 187 total head movements

Example 3
queue = 23, 89, 132, 42, 187
start = 100
end = 199

23, 42, 89, 132, 187


| 100 - 0 | = 100
| 199 - 132 | = 67
= 167 total head movements

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33
end = 199

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16
end = 199

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40
end = 199

Problem 4

queue = 40, 98, 114, 20, 126, 109, 94, 26


start = 8
end = 199
Chapter 4.5 - LOOK
The LOOK algorithm is similar to SCAN but allows you to look ahead and
not require you to end the end or beginning. This will save you head
movements as you will see in the examples.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53
end = 199

Since the head starts at position 53 it will go towards 0 but stop at the lowest
request (14) then head towards the highest request servicing the other requests
along the way.
14, 37, 65, 67, 98, 122, 124, 183

The total head movement can be calculated simply:

| 53 - 14 | = 39
| 183 - 14 | = 169
= 208 total head movements

Example 2
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50
end = 199

Putting them in order:

11, 34, 62, 64, 95, 119, 123, 180

| 50 - 11 | = 39
| 180 - 11 | = 169
= 208 total head movements

Example 3
queue = 23, 89, 132, 42, 187
start = 100
end = 199

23, 42, 89, 132, 187


| 100 - 187 | = 87
| 187 - 23 | = 164
= 251 total head movements

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33
end = 199

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16
end = 199

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40
end = 199

Problem 4

queue = 40, 98, 114, 20, 126, 109, 94, 26


start = 8
end = 199
Chapter 4.6 - C-LOOK
The C-LOOK algorithm works similar to C-SCAN where it jumps but just
like in LOOK it will not go to the end. It will look ahead to determine where
it needs to go and stop.

Example 1
queue = 98, 183, 37, 122, 14, 124, 65, 67
start = 53
end = 199

Since the head starts at position 53, it will go towards 0 but stop at the lowest
request (14) then jump up to 183 and travel towards 0 servicing the requests
along the way and stopping at 65. In some versions of the algorithm they may
include the cost of the jump however we would argue you wont because
you wont service requests in the jump.
14, 37, 65, 67, 98, 122, 124, 183

The total head movement can be calculated simply:

| 53 - 14 | = 39
| 183 - 65 | = 118
= 157 total head movements
Example 2
queue = 95, 180, 34, 119, 11, 123, 62, 64
start = 50
end = 199

Putting them in order:

11, 34, 62, 64, 95, 119, 123, 180

| 50 - 11 | = 39
| 180 - 62 | = 118
= 157 total head movements

Example 3
queue = 23, 89, 132, 42, 187
start = 100
end = 199

23, 42, 89, 132, 187


| 100 - 23 | = 77
| 187 - 132 | = 55
= 132 total head movements
Please note:
While this version goes towards 0 -
it would also be correct for it to go towards the nearest end.

Circular scanning works just like the elevator to some extent. It begins its scan
toward the nearest end and works it way all the way to the end of the system.
Source: http://www.cs.iit.edu/~cs561/cs450/disksched/disksched.html

Exercise Problems
Problem 1

queue = 23, 45, 123, 67, 33, 11, 99


start = 33
end = 199

Problem 2

queue = 37, 126, 4, 129, 66, 12, 45, 58


start = 16
end = 199

Problem 3

queue = 35, 43, 107, 24, 149, 111, 67, 27


start = 40
end = 199

Problem 4

queue = 40, 98, 114, 20, 126, 109, 94, 26


start = 8
end = 199
Chapter 5 - Solutions
Chatper 2.1 - FCFS
Problems:

1. 6
2. 9
3. 4.3
4. 6.33

Chatper 2.2 - Short Job First (Non-Preemption)


Problems:

1. 11.25
2. 17.25
3. 4
4. 4.5

Chatper 2.3 - Short Job First (Preemption)


Problems:

1. 3.75
2. 3.5
3. 3.25
4. 5.75

Chatper 2.4 - Priority


Problems:
1. 9.75
2. 5.5
3. 6.5
4. 5.75

Chatper 2.5 - Round Robin


Problems:

1. 12.67
2. 12.67
3. 11.67
4. 12.67

Chapter 3 - Resource Allocation Graphs


Problems:

1. No deadlock
2. Deadlock

Chapter 4.1 - FCFS


Problems:

1. 310
2. 520
3. 410
4. 406

Chapter 4.2 - SSTF


Problems:

1. 134
2. 137
3. 147
4. 118

Chapter 4.3 - SCAN


Problems:

1. 156
2. 145
3. 151
4. 134

Chapter 4.4 - C-SCAN


Problems:

1. 187
2. 178
3. 196
4. 187

Chapter 4.5 - LOOK


Problems:

1. 134
2. 137
3. 141
4. 121

Chapter 4.6 - C-LOOK


Problems:
1. 100
2. 104
3. 122
4. 118

Das könnte Ihnen auch gefallen