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

CSIT123 Week 7 Lecture

Uploaded by

Ahmed Osta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CSIT123 Week 7 Lecture

Uploaded by

Ahmed Osta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

University of Wollongong in Dubai

Learning objectives
 Understand and explain the units of measure of
memory.
 Explain the role of memory management.
 Understand the reasons for memory partitioning.
 Explain the different portioning techniques.
 Discuss their advantages and disadvantages
 Explain internal and external fragmentation
 Understand and explain the compaction technique.
 Understand and explain the different placement
techniques ( best fit, first fit, worst fit, next fit)
How memory is used?
Bytes, Kilobytes, Megabytes, Gigabytes,…….

Unit used Abbreviation Equivalent in bytes

1 Kilobyte 1 KB 1,024 Bytes


1 Megabyte 1 MB 1,024 KB, OR (1024)2 = 1,024*1,024 bytes=
1,048,576 bytes
1 Gigabyte 1 GB 1,024 MB OR 1,024 * 1,024* KB OR (1024)3
=1,024*1,024* 1,024 bytes or,
1073741824 bytes

1 Terabyte 1TB 1,024 GB OR (1024)4 =1,024*1,024*


1,024*1,024 bytes =
1099511627776
1 Petabyte 1 PB 1,024 TB OR (1024)5=1,024* 1,024*1,024*
1,024*1,024 bytes =
1125899906842620
How memory is used?

Three is a difference between KB and Kb


KB for Kilobytes (Unit of data
storage or memory size)
Kb for Kilobits (Unit for data
transfer rate or network speed)
Some more definitions

Throughput
Is the number of jobs per hour that the system
completes.

All things considered,

finishing 50 jobs per hour is better than finishing 40


jobs per hour.
Some more definitions
Turnaround time is for batch jobs

Turnaround time
The actual time to complete a job

It measures how long the average


user has to wait for the output.
Some more definitions
Turnaround time is for batch jobs

Turnaround time is the sum of the periods


spent
waiting in the ready queue,
executing on the CPU,
and doing I/O.
Some more definitions
Response time is for interactive users sitting at a terminal

Response time is the time taken


From the submission of a request
(you hit return or click the mouse)

until the first response is produced.


Some more definitions

(CPU) Processor utilization


percentage of time
that the processor is active
(not waiting)
Memory management

Main memory (RAM)

• is an important resource
• that must be very carefully
managed.
Memory management
While the average home
computer nowadays has
100,000 times more
memory than the IBM
7094, the largest
computer in the world in
the early 1960s,

Programs are
getting bigger
faster than
memories.
Memory management

Paraphrasing Parkinson’s Law,

‘‘Programs expand to
fill the memory
available to hold them”
Memory management
What every programmer would like:

A private, infinitely large, infinitely fast


memory

That is also non-


volatile,
Memory management
What every programmer would like

A private, infinitely large, infinitely fast


memory

That is also non-volatile,

But , today’s technology does


not provide such memories
Memory management
But there is a second choice

Over the years, people discovered

the concept of a
memory hierarchy,
Memory management
what is a memory hierarchy?

computers have

 a few megabytes of very fast, expensive, volatile


cache memory,
 a few gigabytes of medium-speed, medium-
priced, volatile main memory,
 and a few terabytes of slow, cheap, non-volatile
magnetic or solid-state storage,
 not to mention removable storage, such as USB
sticks.
Memory manager
The part of the operating system that manages the memory
hierarchy

Its role is to efficiently manage memory:

Keep track of which parts of memory are in use,

allocate memory to processes when they need


it,

and deallocate it when they are done


Memory management techniques a simple example

The first (lowest) memory address is 0 (zer0)

The highest memory address is 0xFFF which means FFF in


hexadecimal.
Memory management techniques

What is FFF in What is 1111 So what is the


binary? 1111 1111 in total memory
• 1111 1111 1111 decimal? available:
• 4095 • 4096
Memory management techniques

Case (a),

 The operating system is at the very bottom of


memory in RAM (Random Access Memory),
 This model was used in mainframes.
 It is rarely used today
Memory management techniques

Case (b)

 The OS is in ROM (Read-Only Memory) at the


top of memory,
 This model is used on some handheld
computers and embedded systems.
Memory management techniques

Case (c)

 the device drivers are the top of memory in a ROM


 and the operating system in RAM down below,.
 This model was used by early personal computers (e.g.,
running MSDOS),where the portion of the system in the
ROM is called the BIOS (Basic Input Output System).
Memory management techniques

Models (a) and (c)

 have the disadvantage that a bug in the user program


 can wipe out the operating system,
 possibly with disastrous results.
 WHY?
Memory management techniques

Look at the OS

 In (b) the OS is written in ROM


 In (a) and (c) in RAM
 A user program can overwrite the OS in (a) and
(c) but not in (b)
Memory management techniques

Is there any disadvantage

In Using the ROM to store the OS


Memory management techniques

Is there any disadvantage

• What happens if your OS version


is updated?
Memory management techniques

When the system is organized in this way,


generally only one process at a time
can be running
Memory management techniques - multiprogramming

Multiprogramming called for


sharing main memory between:
The operating systems

And the users

The number of users is determined by the


degree of multiprogramming used.
Memory management techniques - multiprogramming

How is this sharing performed?


The simplest scheme for
managing this available memory

is to partition it into regions


with fixed boundaries.
Memory management techniques - multiprogramming

The partition
The simplest scheme for
managing this available memory

is to partition it into regions


with fixed boundaries.
Memory management techniques - multiprogramming

The partitioning can be

Fixed Dynamic
Memory management techniques - multiprogramming

Fixed partitioning

 Main memory is divided into a number of


static partitions at system generation time. A
process may be loaded into a partition of
equal or greater size.
 Simple to implement; little operating system
overhead.
 Inefficient use of memory due to internal
fragmentation; maximum number of active
processes is fixed.
Memory management techniques - multiprogramming

Dynamic partitioning

 Partitions are created dynamically, so


each process is loaded into a partition of
exactly the same size as that process
 (+) No internal fragmentation; more
efficient use of main memory.
 (-) Inefficient use of processor due to the
need for compaction to counter external
fragmentation.
Fixed vs.
dynamic
partition
of a
64MB
memory
Memory management techniques - multiprogramming
Fixed partitioning - Disadvantages
Disadvantages

 A program may be too big to fit into a partition.


 Main memory utilization is extremely inefficient.
 Any program, no matter how small, occupies an entire partition
Causing internal fragmentation

Internal fragmentation

 In our example, there may be a program whose length is less than 2


Mbytes; yet it occupies an 8-Mbyte partition
 Case where there is wasted space internal (8-2)= 6MB wasted
 to a partition due to the fact that the block of data loaded is smaller
than the partition,
Memory management techniques - multiprogramming
Dynamic partitioning

Dynamic partitioning

 The partitions are of variable length


and number.
 When a process is brought into main
memory, it is allocated exactly as much
memory as it requires and no more.
• Let us see the next example
Memory management techniques - multiprogramming
Dynamic partitioning- the example of a 64Mbytes memory

We will discuss
each of the 8 cases.

From (a)

Through
(h)
Memory management techniques - multiprogramming
Dynamic partitioning- case (a)

Case (a)
main memory is empty,
except for the OS
Which occupies 8MB
And 56 MB free
Memory management techniques - multiprogramming
Dynamic partitioning- case (b)

Case(b)
Process 1(20MB) is
loaded into memory

Just below the OS

Now 36 MB are still


available
Memory management techniques - multiprogramming
Dynamic partitioning- case (c)

Case(c)
Process 2(14 MB) is
loaded into memory

Just below process 1

Now 22MB are still


available
Memory management techniques - multiprogramming
Dynamic partitioning- case (d)

Case(d)
Process 3(18 MB) is
loaded into memory

Just below process 2

Now only 4MB are


still available
Memory management techniques - multiprogramming
Dynamic partitioning- case (e)

Case (e )
At some point, none of the processes
in memory is ready.

The operating system throws out


process 2

There is a memory hole of 14MB

There a memory hole of 4MB


Memory management techniques - multiprogramming
Dynamic partitioning- case (f)

Case(f)
Process 4 (8MB) is loaded into
memory

It cannot fit in the hole of 4MB

So, it is fit in the hole previously


occupied by process 2
But since it is only, 8MB it will
create a new hole of 6 MB
Memory management techniques - multiprogramming
Dynamic partitioning- case (g)

Case (g)
At some point, none of the processes
in main memory is ready,

But process 2, in the Ready-Suspend


state, is available.

Because there is insufficient room in


memory for process 2,

the operating system swaps process


1 out
Memory management techniques - multiprogramming
Dynamic partitioning- case (h)

Case (h)

the operating system


had swapped process 1
out

And now, swaps


process 2 back in
Memory management techniques - multiprogramming
Dynamic partitioning- case (h)

Case (h)
And we end up with 3
small size holes

6MB, 6 MB, 4MB

that cannot fit any user


process.
Memory management techniques - multiprogramming
Dynamic partitioning- external fragmentation

External fragmentation
As this example shows,

this method starts out well,

but eventually it leads to a situation in which

there are a lot of small holes in memory.


Memory management techniques - multiprogramming
Dynamic partitioning- external fragmentation

As time goes on,


Memory becomes more and
more fragmented,

and memory utilization


declines.
Memory management techniques - multiprogramming
Dynamic partitioning- external fragmentation

This phenomenon is referred to


as external fragmentation,

indicating the memory that is external to all


partitions

becomes increasingly fragmented.


Memory management techniques - multiprogramming
Dynamic partitioning- external fragmentation-memory compaction

How to deal with external


fragmentation
Compaction is one technique for overcoming
external fragmentation

From time to time, the OS shifts the processes


so they are contiguous

and all of the free memory is together in one


block.
Memory management techniques - multiprogramming
Dynamic partitioning- external fragmentation-memory compaction-example
Memory management techniques - multiprogramming
Dynamic partitioning- problems with compaction

Problem with Compaction

 it is a time-consuming procedure and wasteful of processor time.


 It is usually not done because it requires a lot of CPU time.

For example,

• on a 16-GB machine that can copy 8 bytes in 8 nanosec or ,10-9


seconds
• it would take about 16 sec to compact all of memory
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

the OS designer
Because
must be clever
memory
in deciding how (how to plug
compaction is
to assign the holes).
time
processes to
consuming,
memory
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

When then
 it is time to load or swap a  the operating system must
process into main memory, decide which free block to
 and if there is more than allocate.
one free block of memory
of sufficient size,
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

First-fit

Best-fit
Placement
algorithms:
Next-fit

Worst-fit
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

First fit.
Allocate the first hole that is big enough.
Searching can start either

at the beginning of the set of holes or at the


location where the previous first-fit search ended.

We can stop searching as soon as we find a free


hole that is large enough.
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

Best fit
Allocate the smallest hole that is big enough.

We must search the entire list, unless the list is


ordered by size.

This strategy produces the smallest leftover


hole.
Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

Worst fit.
Allocate the largest hole.

Again, we must search the entire list, unless it is sorted by size.

This strategy produces the largest leftover hole,

which may be more useful than

the smaller leftover hole from a best-fit approach.


Memory management techniques - multiprogramming
Dynamic partitioning- placement algorithms

Next-fit
begins to scan memory from the
location of the last placement

and chooses the next available


block that is large enough.
Memory management techniques - multiprogramming
placement algorithms example

Example of
Memory
Configuration
before and
after
Allocation of
16-Mbyte
Block
Memory management techniques - multiprogramming
placement algorithms example

Figure (a)
 shows an example
memory configuration
after a number of
placement
 and swapping-out
operations.
 The last block that was
used was a 22-Mbyte
block
 from which a 14-Mbyte
partition was created.
Memory management techniques - multiprogramming
placement algorithms example

Figure b
 shows the
difference between
 the best-,first-, and
next-fit placement
algorithms
• in satisfying a 16-
Mbyte allocation
request.
Memory management techniques - multiprogramming
placement algorithms example

Best-fit
 will search the entire list of
available blocks
 and make use of the 18-
Mbyte block,
 leaving a 2-Mbyte fragment.

First-fit
 results in a 6-Mbyte
fragment,

Next -fit
 results in a 20-Mbyte
fragment
Memory management techniques - multiprogramming
placement algorithms: best fit, first fit, next fir Performance

The first-fit algorithm


is not only the simplest , but usually the best
, and fastest as well.

on the other hand, it may litter the front


end with small free partitions

that need to be searched over on each


subsequent first-fit pass.
Memory management techniques - multiprogramming
placement algorithms: best fit, first fit, next fit Performance

The nextfit algorithm


tends to produce slightly worse results than the first-fit.

more frequently lead to an allocation from a free block at the end of memory.

The result is that the largest block of free memory, which usually appears

at the end of the memory space, is quickly broken up into small fragments.

Thus, compaction may be required more frequently with next-fit.


Memory management techniques - multiprogramming
placement algorithms: best fit, first fit, next fir Performance

The best-fit algorithm,


despite its name, is usually the worst performer.

It looks for the smallest block that will satisfy the requirement,

it guarantees that the fragment left behind is as small as possible.

Although each memory request always wastes the smallest amount of memory,

the result is that main memory is quickly littered by blocks too small to satisfy memory allocation
requests.

Thus, memory compaction must be done more frequently than with the other algorithms
exercise
• Assume a system with the following holes in memory:
– 10 KB
– 4 KB
– 12 KB
– 8 KB
– 18 KB
– 7 KB
– 9 KB
– 12 KB
– And 15 KB
• Which hole is taken for the next fit successive requests of memory:
– P1 : 12 KB
– P2 : 10 KB
– P3: 9 KB
• Apply all three algorithms; First fit, best fit, Worst fit
Some definitions we will used
 A block of memory status is either
– Free (hole) represented by hole
– Or used represented by Used
 A process is defined by:
– Its arrival
– And its memory demand
 After allocation of a memory block to a process
– The free block (hole) becomes used
– This is reflected in the new status of memory
Fist Fit
Initial status and Arrival of process 1

initial Status= new Status=


Size (K) status used/hole sizes(K) used/hole
10hole hole 10 hole
4hole hole 4 hole
12hole used 12 used
8hole hole 8 hole
18hole hole 18 hole
7hole hole 7 hole
9hole hole 9 hole
12hole hole 12 hole
15hole hole 15 hole
with demand of
arrivals Time 0 P1 arrives 12K
Fist Fit
Arrival of process 2

current status size (K) new status


hole 10 used
hole 4 hole
used 12 K used
hole 8 hole
hole 18 hole
hole 7 hole
hole 9 hole
hole 12 hole
hole 15 hole
P2 arrives with demand of 10 K
Fist Fit
Arrival of process 3

current status size (K) new status


used 10 used
hole 4 hole
used 12 K used
hole 8 hole
hole 18 used
hole 7 hole
hole 9 hole
hole 12 hole
hole 15 hole
P3 arrives with demand of 9 K
Best Fit
Initial status and Arrival of process 1

initial Status= new Status=


Size (K) status used/hole sizes(K) used/hole
10hole hole 10 hole
4hole hole 4 hole
12hole used 12 used
8hole hole 8 hole
18hole hole 18 hole
7hole hole 7 hole
9hole hole 9 hole
12hole hole 12 hole
15hole hole 15 hole
with demand of
arrivals Time 0 P1 arrives 12K
Best Fit
Arrival of process 2

current status size (K) new status


hole 10 used
hole 4 hole
used 12 K used
hole 8 hole
hole 18 hole
hole 7 hole
hole 9 hole
hole 12 hole
hole 15 hole
P2 arrives with demand of 10 K
Best Fit
Arrival of process 3

current status size (K) new status


used 10 used
hole 4 hole
used 12 K used
hole 8 hole
hole 18 hole
hole 7 hole
hole 9 used
hole 12 hole
hole 15 hole
P3 arrives with demand of 9 K
Worst Fit
Initial status and Arrival of process 1

initial new Status=


Size (K) status Status= used/hole sizes(K) used/hole
10hole hole 10 hole
4hole hole 4 hole
12hole used 12 used
8hole hole 8 hole
18hole hole 18 used
7hole hole 7 hole
9hole hole 9 hole
12hole hole 12 hole
15hole hole 15 hole
with demand of
arrivals Time 0 P1 arrives 12K
Worst Fit
Arrival of process 2

current status size (K) new status


hole 10 hole
hole 4 hole
used 12 K used
hole 8 hole
hole 18 used
hole 7 hole
hole 9 hole
hole 12 hole
hole 15 used
P2 arrives with demand of 10 K
Worst Fit
Arrival of process 3- why did I pick the 2nd 12 k slot?

current status size (K) new status


hole 10 used
hole 4 hole
used 12 used
hole 8 hole
hole 18 used
hole 7 hole
hole 9 hole
hole 12 used
used 15 used
P3 arrives with demand of 9K
References
 William Stallings, Operating Systems Internals and Design
Principles, Ninth Edition , Global Edition, 2018
 Tanenbaum, Andrew S. Modern operating systems. Pearson Ed,
5th, 2023
 Abraham Silberschatz, Operating Systems Concepts, , Wiley,
10th Edition, 2018

You might also like