Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam

05/16/2024 F.
1
Contents

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
2
Overview
o Program must be brought into memory and placed within a process
for it to be executed.
o In a multiprogramming system, the “user” part of memory must be
further subdivided to accommodate multiple processes.
o The task of subdivision is carried out dynamically by the operating
system and is known as memory management.
o Main memory and registers are only storage CPU can access
directly.
o Register access in one CPU clock (or less), Main memory can take
many cycles
o Cache sits between main memory and CPU registers
o Memory needs to be allocated efficiently to pack as many processes
into memory as possible.
o Problem
 How to manage relative speed of accessing physical memory?
 How to Ensure correct operation to protect the operating system
from being accessed by user process and user processes from one
another?
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
3
Overview(con’t….)
 To provide the above protection, we need to ensure that
each process has a separate address space.
 Determine the legal addresses that the process can access
legally
 Ensure that a process can access only these legal addresses.
 This protection can be done using two Defines legal
registers
addresses
– Base registers: - holds the physical address Ensures memory
where its program begins in memory protection
– Limit registers:-holds the length of the program.
o Use a HW to compare every address generated in user space
with registers value
o Base and limit registers are loaded only by the OS, using
special privileged instructions

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
4
Address Binding
o

o f
o se
u rp
p
the
s
Department of Computer Science, School of Computing |DDUIoT at’ g?
Operating System byhKidanemariam
05/16/2024 F. W din 5
in
Address Binding(con’t…)
o Address binding of instructions and data to memory
addresses can happen at three different stages.
–Compile time:
» If memory location is known a prior, absolute
code can be generated; must recompile code if
starting location changes.
–Load time:
» Must generate re-locate-able code if memory
location is not known at compile time.
–Execution time:
»Binding delayed until run-time if the process can be
moved during its execution from one memory segment e of
p os
to another p ur
the
»Need hardware support for maps (e.g. basehat’and s
g ? limit
W di n
registers). bin
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
6
Logical vs. Physical Address Space
o T
h
e

c
o
n
c
e
p
t

l
ca
o

gi
lo
f

n
ee
? tw
es be
a

re nce
dd re
ss
l a iffe
ica d
l

ys e
ph th
o
d t’s
an ha
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
W
05/16/2024 F.
7 g
Swapping

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
8
Schematic View of Swapping

Swapping is done when


oQuantum expires
oPriority issues arises

Conditions for swapping


o Process to be executed must be not
in memory
o No sufficient free memory location

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
9
Contiguous Allocation
• E
a
c
h

p
r
o
c
e
s
s

i
s

c
o
n
t
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam a
05/16/2024 10
F. i
Contiguous Allocation(con’t…)

OS OS OS OS
Process 5 Process 5 Process 5 Process 5
Process 9 Process 9

Process 8 Process 10

Process 2 Process 2 Process 2 Process 2

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
11
Fixed Partitioning
 Divide memory into several fixed-sized partitions.

 Each partition contains one process.

 Degree of multiprogramming is bound by the number


of partitions.

 When a partition is free, a process is selected from


the input queue and is loaded into the free partition.

 When the process terminates, the partition becomes


available for another process.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
12
Fixed Partitioning

Equal size partition unequal size partition

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
13
Fixed Partitioning
Equal-size partitions
– any process whose size is less than or equal to the
partition size can be loaded into an available partition
– if all partitions are full, the operating system can swap a
process out of a partition
– a program may not fit in a partition. The programmer
must design the program with overlays
– because all partitions are of equal size, it does not
matter which partition is used
Unequal-size partitions
– can assign each process to the smallest partition within
which it will fit
– queue for each partition
– processes are assigned in such a way as to minimize
wasted memory within a partition.
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
14
Fixed Partitioning(con’t…)
 Its no longer used because it has various drawbacks
like:
1. Degree of multi-programming is bounded by
the number of partitions.
2. Internal fragmentation
• Internal fragmentation: is the phenomenon, in
which there is wasted space internal to a partition
due to the fact that the block of data loaded is
smaller than the partition

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
15
Internal fragmentation

Process 1
Input Queue )KB 7( 10
(in the disk) KB

KB 4 KB 8 KB 9 KB 7
Process 2
)KB 9( 10
KB

Internal Process 3
Fragmentation )KB 8( 10
KB

As shown
• This method suffers from internal fragmentations.
• The degree of multiprogramming is bounded to 3 although it
can beDepartment
4. of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
16
Dynamic Partitioning
• Partitions are of variable length and number
• Initially, all memory is available for user processes,
and is considered as one large block of available
memory.

• When a process arrives and needs memory, we search


for a hole large enough for this process using: First fit,
Best fit, Worst fit

• If we find one, we allocate only as much memory as


is needed, keeping the rest available to satisfy future
requests.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
17
Dynamic Partitioning
o Operating system satisfy a request of size n from a list
of free holes-using algorithms:
1. First fit: allocate the first hole that is big enough
(fastest method).
2. Best fit: allocate the smallest hole that is big enough
(produces the smallest leftover hole).
3. Worst fit: allocate the largest hole (produces the
largest leftover hole which may be more useful than
the smaller leftover hole from a best-fit approach.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
18
OS
Process 4
Process 1
)KB 4(
Input Queue )KB 7(
(in the disk) Process 5
KB 9( 2
)Process
)KB 9(
KB 4 KB 8 KB 9 KB 7

Process 3
)KB 8(

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
19
Memory
Using First Fit 0

OS
Base 999
1000
Start 1040 1010
address of Process
process 20
Legal range 1040

Limit
Process
1060
1070
Executable file
(Size =20 Process
memory words) 1100

1125
Process
1150

Loader 1200

Process
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
1250
05/16/2024 F.
20
1255
Memory
Using Best Fit
OS
Base 1000
Start 1100 1010
address of Process
process 20
Legal range 1040

Limit

1070
Executable file
(Size =20 Process
memory words) 1100
Process
1120
1125
Process
1150

Loader
1200

Department of Computer Science, School of Computing |DDUIoT


Process
Operating System by Kidanemariam
05/16/2024 1250 21
F. 1255
Memory
Using Worst Fit
OS
Base 1000
Start 1150 1010
address of Process
process 20
Legal range 1040

Limit

1070
Executable file
(Size =20 Process
memory words) 1100

1125
Process
1150
Process
1170
Loader
1200

Department of Computer Science, School of Computing |DDUIoT


Process
Operating System by Kidanemariam
05/16/2024 1250 22
F. 1255
Dynamic Partitioning
o The degree of multi-programming is changing according
to the number of processes.
o This method suffers from external fragmentation in the
memory (in ready queue).
External fragmentation: is the phenomenon, in
which memory that is external to all partitions
becomes increasingly fragmented.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
23
External fragmentation

OS
Process 4
Input Queue
(in the disk) Process 2

KB 4 KB 8 KB 9 KB 7
Process 3 External
Fragmentations
Process 9

Process 20

As shown:
dynamic partition is suffered from external Fragmentations.
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
24
Compaction

OS
Process 4
Process 4
Input Queue
(in the disk)
Process 2
Process 2

Process 3
KB 4 KB 8 KB 9 KB 7
Process 3
Process 9
Process 9
Process 20
Process 20 A new hole to store a
Compaction: new process
 Is a movement of the memory contents to place all free memory in a one large
block sufficient to store new process.
 It is a solution for the external fragmentation but it is expensive and is not always
possible.
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
25
Paging and Segmentation

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024 F.
26
Basic method
o Paging is a memory-management scheme that permits
the physical-address space of a process to be non-
contiguous.
o It is commonly used in most operating systems.
o Divide physical memory into fixed-sized blocks called
frames.
o Divide Process into blocks of same size called pages
o Size is power of 2, between 512 bytes and 8,192
bytes
o Use a page table which contains base address of each
page in physical memory.
o To run a program of size n pages, need to find n free
frames and load program
o Set up a page table to translate logical to physical
addresses
05/16/2024
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
27
F.
Basic method
 When a process arrives, the size in pages is examined
 Each page of process needs one frame.
 If n frames are available these are allocated, and page table is updated with
frame number.

Before allocation After allocation


Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
28
Address translation
o Address generated by CPU is divided into:
 Page number (p) – used as an index into a page table which
contains base address of each page in physical memory.
 Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit.
o Page number is an index to the page table.
o The page table contains base address of each page in physical
memory.
o The base address is combined with the page offset to define the
physical address that is sent to the memory unit.
o The size of logical address space is 2m and page size is 2n
address units.
o Higher m-n bits designate the page number
o n lower order bits indicate the page offset. logical address

p d

page offset (n)


Page number(m-n)
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
29
Address Translation Architecture

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
30
Paging Example
Assume:-
o page size=4 bytes
o physical memory = 32 bytes (8
pages).
 How a logical memory address can
be mapped into physical memory?
Example :
Logical address 0(containing ‘a’)
00000---address of a.
 Page number=000(the 3 higher order bit)
 Offset number=00( the last 2 least bit)
o address a exist:
i. is on page 0.
ii. is at offset 0.
 then Indexing into the page table using  Logical address 0 maps 5*4+0=20
page number, you can get page 0 is in  Logical address 3 maps to= 5*4+3=23
frame 5.  Logical address 4 maps to =6*4+0=24
 Page number is changed into frame
 Logical address 13 maps to= 2*4+1=9.
number; offset number is used as it is.
 Finally logical address 0 is mapped to
physicalDepartment
20, i.e. 20=[(5x4)+0]
of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
31
More on Paging
o In paging scheme, Pages are allocated as units.
o In this scheme there is no external fragmentation.
o But internal fragmentation is inevitable.
Example:-
― assume page size=2048 bytes.
― a process of 61442 bytes needs 30 pages plus 2 bytes.

n
r
o th r a
use
Since units are managed in terms of pages 31 pages are

y
an ente
W h e O S ce s s r e a n

er
allocated.
―Internal fragmentation=2046 bytes!!!!.

to
e
r p is th
o In the worst case a process needs n pages plus 1 byte.

or
ro
pro ress r use me,
So it will be allocated n+1 pages.

add nce f sche


Fragmentation =(page size-1 byte) ~ entire page.

y?
th
g
o
cha agin

ce s o f
s ?
p
In
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
32
Page table
o Page table is used to map virtual pages onto page
frames.
o Page table can have the following important fields.
 Page frame number: represented the page frame number in
physical memory.
 Present/absent bit: indicate whether the page to which the
entry belongs is currently in memory or not.
 bit 1- the entry is valid and can be used.
 Bit 0 the virtual page is not currently in memory. Accessing a page
table entry with this bit set to 0 causes a page fault.
 Protection bits: tell what kinds of access are permitted. In
the simplest form, this field contains 1 bit, with 0 for
read/write and 1 for read only.
 Modified bit: is bit set when page is written. If the page in
it has been modified, it must be written back to the disk. If
it has not been
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
33
Page table(con’t…)
 Referenced bit: is set whenever a page is referenced, either for
reading or writing.
 Its value is to help the operating system choose a page to
evict when a page fault occurs and plays an important role in
several of the page replacement algorithms
 Cache disable: With this bit, caching can be turned off. This
feature is important for pages that map onto device registers
rather than memory.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
34
Implementation of Page Table
o Two options: Page table can be kept in registers or main memory
o Page table is kept in main memory due to bigger size.
– Ex: address space = 232 to 264
– Page size= 212
– Page table= 232 / 212=220
– If each entry consists of 4 bytes, the page table size = 4MB.
o Page-table base register (PTBR) points to the page table.
o Page-table length register (PTLR) indicates size of the page table.
o PTBR, and PTLR are maintained in the registers.
o In this scheme every data/instruction access requires two memory
accesses. One for the page table and one for the data/instruction.
– Memory access is slowed by a factor of 2.
– Swapping might be better !
• The two memory access problem can be solved by the use of a
special fast-lookup hardware cache called associative memory or
translation look-aside buffers (TLBs)

What’s the purpose


Department of Computer Science, School of Computing |DDUIoT
of using TLB?
Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
35
Segmentation
o

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
36
User’s View of a Program

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
37
Logical View of Segmentation
11

4
1

3 22
4

33

user space physical memory space

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
38
Segmentation Architecture
o Logical address consists of a two tuple
 <segment-number, offset>
o Segment Table
• Maps two-dimensional user-defined addresses into one-
dimensional physical addresses.
• Each table entry has
– Base - contains the starting physical address where
the segments reside in memory.
– Limit - specifies the length of the segment.
• Segment-table base register (STBR) points to the
segment table’s location in memory.
• Segment-table length register (STLR) indicates the
number of segments used by a program;.
 Note: segment number s is legal if s < STLR.
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
39
Segmentation Architecture (cont.)

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
40
Segmented Paged Memory

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
41
Virtual Memory

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
42
Background
o

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
43
Background(con’t…)
 Virtual memory is a technique that allows the execution of
processes that may not be completely in memory.
– Programs are larger than main memory.
– VM abstract main memory into an extremely large,
uniform array of storage.
 Separation of user logical memory from physical memory.
– Only part of the program needs to be in memory for
execution.
– Logical address space can therefore be much larger than
physical address space.
– Allows address spaces to be shared by several processes.
– Allows for more efficient process creation.
– Frees the programmer from memory constraints.
 Virtual memory can be implemented via:
– Demand paging
– Demand segmentation
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
44
Virtual Memory That is Larger Than Physical Memory

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
45
Demand Paging
o

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
46
Presence Bit

Frame # presence bit


1
1
1

0
1

0
0
page table

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
47
Page Table When Some Pages Are Not in Main
Memory

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024 School of Computing, DDUIoT
F.
48
Page Fault
o

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
49
Steps in Handling a Page Fault
1. Check the internal table,
to determine whether this
reference is valid or
invalid.
2. If the reference is
invalid, then trap to OS.
3. Find free frame.
4. Schedule disk operation.
5. Modify the internal table
to indicate that page is
in main memory.
6. Restart the instruction.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
50
Page Replacement
o Prevent over-allocation of memory by modifying page-
fault service routine to include page replacement.
Basic Page Replacement
1. Find the location of the desired page on disk.
2. Find a free frame:
 If there is a free frame, use it.
 If there is no free frame, use a page replacement
algorithm to select a victim frame. And change page
table accordingly.
3. Read the desired page into the (newly) free frame.
Update the page table.
4. Restart the process.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
51
Page Replacement

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
52
Page Replacement algorithm
o Page fault forces a choice
 No room for new page (steady state)
 Which page must be removed to make room for an
incoming page?
o How is a page removed from physical memory?
 If the page is unmodified, simply overwrite it: a copy already
exists on disk
 If the page has been modified, it must be written
back to disk: prefer unmodified pages?
o Better not to choose an often used page
 It’ll probably need to be brought back in soon

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
53
Page Replacement Algorithms(con’t…)
o

Graph of Page
Faults Versus
The Number of
Frames

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
54
Optimal page replacement algorithm
o replace the page that will be used furthest in the future
o Only works if we know the whole sequence!
o Nice, but not achievable in real systems!
Example: 4 frames and 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
string references
1

2 4
6 page faults
3

5
o Used for measuring how well your algorithm performs.
o How can you know what the future references will be?
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
55
Allocation of Frames
o Each process needs minimum number of pages allocation.
o If there is a single process, entire available memory can be
allocated.
o Multi-programming puts two or more processes in memory
at same time.
o We must allocate minimum number of frames to each
process.
o Two major allocation schemes.
– fixed allocation
– priority allocation

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
56
Allocation of Frames(con’t…)
1.

2. Priority Allocation:- Use a proportional allocation


scheme using priorities rather than size.
o If process Pi generates a page fault,
– select for replacement one of its frames.
– select for replacement a frame from a process with
lower priority number.

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
57
Global vs. Local Allocation
o

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
58
Thrashing
o If a process does not have “enough” pages, the page-
fault rate is very high. This leads to:
– low CPU utilization.
– operating system thinks that it needs to increase the
degree of multiprogramming.
– another process is added to the system.
o Thrashing is High paging activity.
o Thrashing  a process is spending more time in
swapping pages in and out.
o If the process does not have number of frames
equivalent to number of active pages, it will very
quickly page fault.
o Since all the pages are in active use it will page fault
again.
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
59
Thrashing

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
60
Causes of thrashing

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
61
Locality model

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
62
Working-Set Model

Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam


05/16/2024
05/16/2024
F.
63
Working Set(con’t…)
 OS monitors the WS of each process allocates to that working set enough
frames equal to WS size.
 If there are enough extra frames, another process can be initiated.
 If D>m, OS suspends a process, and its frames are allocated to other
processes.
 The WS strategy prevents thrashing by keeping MPL as high as possible.
 However, we have to keep track of working set.
Keeping track of Working Set
 Approximate with interval timer + a reference bit,->Example:  = 10,000
 Timer interrupts after every 5000 time units.
 Keep in memory 2 bits for each page.
 Whenever a timer interrupts copy and sets the values of all reference
bits to 0.
 If one of the bits in memory = 1  page in working set.
 Why is this not completely accurate?
 We can not tell when the reference was occurred.
 Accuracy can be increased by increasing frequency of interrupts which
also Department
increases the cost.
of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024
05/16/2024
F.
64
Department of Computer Science, School of Computing |DDUIoT Operating System by Kidanemariam
05/16/2024 F.
65

You might also like