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

ch8-1 OS

Uploaded by

Zeeshan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ch8-1 OS

Uploaded by

Zeeshan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 71

Chapter 8: Main Memory

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Chapter 8: Memory Management

 Background
 Swapping
 Contiguous Memory Allocation
 Paging
 Structure of the Page Table
 Segmentation
 Example: The Intel Pentium

Operating System Concepts – 8th Edition 8.2 Silberschatz, Galvin and Gagne ©2009
Objectives

 To provide a detailed description of various ways of organizing


memory hardware

 To discuss various memory-management techniques, including paging


and segmentation

 To provide a detailed description of the Intel Pentium, which supports


both pure segmentation and segmentation with paging

Operating System Concepts – 8th Edition 8.3 Silberschatz, Galvin and Gagne ©2009
Background

 Program must be brought (from disk) into memory and placed within a
process for it to be run

 Main memory and registers are only storage CPU can access directly

 Register access in one CPU clock (or less)

 Main memory can take many cycles

 Cache sits between main memory and CPU registers

Operating System Concepts – 8th Edition 8.4 Silberschatz, Galvin and Gagne ©2009
Background
 We must ensure correct operation has to protect the operating
system from access by user processes and to protect user
processes from one another.

 To do this …
 Each process has a range of legal addresses.
 Process can access only these legal addresses.

 We can provide this protection by using two registers:


 Base register – hold the smallest legal physical memory
address.
 Limit register – specify the size of the range.

Operating System Concepts – 8th Edition 8.5 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
 A pair of base and limit registers define the logical address space

30004
30004++12090
12090=
=
42094!!
42094!!

Operating System Concepts – 8th Edition 8.6 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
 Then, we compare every address generated in user mode with the
registers.
 Any attempt (in user mode) to access operating-system memory or other
users’ memory results in a fatal error.
 Therefore, we can prevent a user program from modifying the code or
data structures of either the operating system or other users.

Operating System Concepts – 8th Edition 8.7 Silberschatz, Galvin and Gagne ©2009
Memory Management

 Subdividing memory to accommodate multiple processes

 Memory needs to be allocated to ensure a reasonable supply of


ready processes to consume available processor time

Operating System Concepts – 8th Edition 8.8 Silberschatz, Galvin and Gagne ©2009
Memory Manag. Requirements

 Relocation
 Programmer does not know where the program will be
placed in memory when it is executed
 While the program is executing, it may be swapped to disk
and returned to main memory at a different location
(relocated)
 Memory references in the code must be translated to actual
physical memory address

Operating System Concepts – 8th Edition 8.9 Silberschatz, Galvin and Gagne ©2009
Memory Manag. Requirements

 Sharing
 Allow several processes to access the same portion of
memory
 Better to allow each process access to the same copy of the
program rather than have their own separate copy

Operating System Concepts – 8th Edition 8.10 Silberschatz, Galvin and Gagne ©2009
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
 Main memory use is inefficient. Any program, no matter
how small, occupies an entire partition. This is called
internal fragmentation.

Operating System Concepts – 8th Edition 8.11 Silberschatz, Galvin and Gagne ©2009
Fixed Partitioning

Operating System Concepts – 8th Edition 8.12 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm

 Equal-size partitions
 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

Operating System Concepts – 8th Edition 8.13 Silberschatz, Galvin and Gagne ©2009
 Problems with equal size fixed partitions: „
 If program is bigger than a partition size, use of overlays „
 Main memory utilization is extremely inefficient; Internal Fragmentation – waste
of space internal to partition due to the fact that block of data loaded is smaller
than partition

Operating System Concepts – 8th Edition 8.14 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm

Operating System Concepts – 8th Edition 8.15 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning

 Partitions are of variable length and number


 Process is allocated exactly as much memory as
required
 Eventually we get holes in the memory. This is called
external fragmentation
 Must use compaction to shift processes so they are
contiguous and all free memory is in one block

Operating System Concepts – 8th Edition 8.16 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning

A hole of 64K is left after loading 3 processes: not enough room


for another process.
Eventually each process is blocked. The OS swaps out process 2
to bring in process 4.

Operating System Concepts – 8th Edition 8.17 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning

 Another hole of 96K is created.


 Eventually each process is blocked. The OS swaps out process 1 to
bring in again process 2 and another hole of 96K is created ...

Operating System Concepts – 8th Edition 8.18 Silberschatz, Galvin and Gagne ©2009
Internal / External Fragmentation

1. Internal Fragmentation – allocated memory may


be slightly larger than requested memory; this size
difference is the memory internal to a partition, that
is not being used.
2. External Fragmentation – total memory space
exists to satisfy a size n request, but that memory
is not contiguous.

Operating System Concepts – 8th Edition 8.19 Silberschatz, Galvin and Gagne ©2009
Reducing External Fragmentation

 Reduce external fragmentation by doing


compaction:
 Shuffle memory contents to place all free
memory together in one large block (or
possibly a few large ones).
 Compaction is possible only if relocation is
dynamic, and is done at execution time.

Operating System Concepts – 8th Edition 8.20 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms

 Operating system must decide which free block to allocate to a


process

 Best-fit algorithm
 Chooses the block that is closest in size to the request
 Worst performer overall
 Since smallest block is found for process, the smallest
amount of fragmentation is left
 Memory compaction must be done more often

Operating System Concepts – 8th Edition 8.21 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms

 First-fit algorithm
 Scans memory form the beginning and chooses the first
available block that is large enough
 Fastest
 May have many process loaded in the front end of memory
that must be searched over when trying to find a free block
 Worst-fit algorithm
 Search the entire list of available memory and allocate the
largest block.
 (The justification for this scheme is that the leftover block
produced would be larger and potentially more useful than
that produced by the best-fit approach.)

Operating System Concepts – 8th Edition 8.22 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
 Next-Fit algorithm

 Scans the memory from the location of last


placement and chooses next available block
that is large enough

Operating System Concepts – 8th Edition 8.23 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms

Operating System Concepts – 8th Edition 8.24 Silberschatz, Galvin and Gagne ©2009
Question
Assume that the main memory has the following 5 fixed
partitions with the following sizes: 100KB,
500KB, 200KB, 300KB and 600KB (in order)
a) How would each of the First-fit, Best-fit, next fit, Worst-fit
algorithms place processes of 212KB,
417KB, 112KB and 426KB (in order)?
c) Compute the total memory size that is not used for each
algorithm.
d) Which algorithm makes the efficient use of the memory?

Operating System Concepts – 8th Edition 8.25 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
 A pair of base and limit registers define the logical address space

Operating System Concepts – 8th Edition 8.26 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers

 Base register
 Starting address for the process
 Limit + Base / Bounds register
 Ending location of the process
 These values are set when the process is loaded or when the process
is swapped in
 The value of the base register is added to a relative address to produce
an absolute address
 The resulting address is compared with the value in the bounds
register
 If the address is not within bounds, an interrupt is generated to the
operating system

Operating System Concepts – 8th Edition 8.27 Silberschatz, Galvin and Gagne ©2009
Logical vs. Physical Address Space

 The concept of a logical address space that is bound to a separate


physical address space is central to proper memory management

 Logical address – generated by the CPU; also referred to as


virtual address
 Physical address – address seen by the memory unit

Operating System Concepts – 8th Edition 8.28 Silberschatz, Galvin and Gagne ©2009
Memory-Management Unit (MMU)

 Hardware device that maps virtual to physical address

 In MMU scheme, the value in the relocation register is added to every


address generated by a user process at the time it is sent to memory

 The user program deals with logical addresses; it never sees the real
physical addresses

Operating System Concepts – 8th Edition 8.29 Silberschatz, Galvin and Gagne ©2009
Dynamic relocation using a
relocation register

Operating System Concepts – 8th Edition 8.30 Silberschatz, Galvin and Gagne ©2009
Hardware Support for Relocation
and Limit Registers

Operating System Concepts – 8th Edition 8.31 Silberschatz, Galvin and Gagne ©2009
Swapping
 A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution

 Backing store – fast disk large enough to accommodate copies of all


memory images for all users; must provide direct access to these
memory images

Operating System Concepts – 8th Edition 8.32 Silberschatz, Galvin and Gagne ©2009
Schematic View of Swapping

Operating System Concepts – 8th Edition 8.33 Silberschatz, Galvin and Gagne ©2009
Swapping
 Examples:
 In a round-robin system …
 When a quantum expires, the memory manager will swap
out the process that just finished
 And swap in another memory for execution.

 In a priority-based system …
 If a higher-priority process arrives, the memory manager
can swap out a lower-priority process.
 Then load and execute the higher-priority process.
 This scheme is sometimes called roll out, roll in.

Operating System Concepts – 8th Edition 8.34 Silberschatz, Galvin and Gagne ©2009
Swapping
 The swapping time:
 Assume that:
 The user process is 10 MB.
 The backing store is a standard hard disk with a transfer rate of 40MB/sec.
 No head seeks.
 Average latency is 8 ms.

 The transfer of the 10-MB process to or from memory takes


 10000 KB / 40000 = ¼ second = 250 ms.

 The swap time = (head seeks) + (latency) + (transfer) = 258.


 We must both swap out and in, so the total swap time is 516 ms.

 For efficiency, we want the execution time for each process to be long
relative to the swap time.
 In this example, the time quantum should be larger than 0.516 seconds.

Operating System Concepts – 8th Edition 8.35 Silberschatz, Galvin and Gagne ©2009
Paging
 Divide physical memory into fixed-sized blocks, called frames
 Divide logical memory into blocks of the same size, called pages
 Size of a page is a power of 2
 Typical page sizes: 1K – 16K

 Keep track of all free frames

 To run a program of size n pages, need to find n free frames and load
program

 Set up a page table to translate logical to physical addresses

 Internal fragmentation

Operating System Concepts – 8th Edition 8.36 Silberschatz, Galvin and Gagne ©2009
Address Translation Scheme

 Logical address generated by CPU is (p , d):


 Page number (p) – used as an index into a page table. Page table
entry contains the frame no. f where page p is loaded.
 Page offset (d) – offset within the page

page number page offset


p d
m-n n

 Physical address of the location referenced by (p , d) is computed by


appending d at the end of f, as shown below:
frame number offset
f d

Operating System Concepts – 8th Edition 8.37 Silberschatz, Galvin and Gagne ©2009
Paging Hardware

Operating System Concepts – 8th Edition 8.38 Silberschatz, Galvin and Gagne ©2009
Paging Model of Logical and
Physical Memory

Operating System Concepts – 8th Edition 8.39 Silberschatz, Galvin and Gagne ©2009
Free Frames

Before allocation After allocation

Operating System Concepts – 8th Edition 8.40 Silberschatz, Galvin and Gagne ©2009
Paging Example
 Page size = 4 bytes 0
Page Frame
 Process address space
1
= 4 pages 0111
 Physical address space 2
= 8 frames
3
 Logical address: (1,3)
= 0111
 Physical address: (6,3)
= 11011

11011

32-byte memory and 4-byte pages


Operating System Concepts – 8th Edition 8.41 Silberschatz, Galvin and Gagne ©2009
Addressing in Paging
 Logical address space of 16 pages of 1024 words each,
mapped into a physical memory of 32 frames.
 Logical address size?
 Physical address size?
 Number of bits for p, f, and d?

 No. of bits for p = ceiling [log2 16] bits = 4 bits


 No. of bits for f = ceiling [log2 32] bits = 5 bits
 No. of bits for d = ceiling [log2 2048] bits = 11 bits

 Logical address size = |p| + |d| = 4+11 = 15 bits


 Physical address size = |f| + |d| = 5+11 = 16 bits

Operating System Concepts – 8th Edition 8.42 Silberschatz, Galvin and Gagne ©2009
Paging Example

 Example: if 16 bits addresses are


used and page size = 1K, we need 10
bits for offset and have 6 bits available
for page number.
 Then the 16 bit address, obtained with
the 10 least significant bits as offset
and 6 most significant bits as page
number, is a location relative to the
beginning of the process.

 1502 >>

Operating System Concepts – 8th Edition 8.43 Silberschatz, Galvin and Gagne ©2009
Paging Example

Operating System Concepts – 8th Edition 8.44 Silberschatz, Galvin and Gagne ©2009
Example
 32-bit linear address
 Process address space = 232 B
= 4 GB
 4K page size
 Maximum pages in a process address space =
232 / 4K = 1M
 Number of bits for d = log2 4K
= 12
 Number of bits for p = 32 - 12

Operating System Concepts – 8th Edition 8.45 Silberschatz, Galvin and Gagne ©2009
Another Example
 Logical address = 32-bit
 Process address space = 232 B
= 4 GB
 Main memory = RAM = 512 MB = 229
 Page size = 4K
 Maximum pages in a process address
space = 232 / 4K = 1M

Operating System Concepts – 8th Edition 8.46 Silberschatz, Galvin and Gagne ©2009
Another Example

 |d| = log 4K = 12 bits


2
 |p| = 32 – 12 = 20 bits
 No. of frames = 512 M / 4 K
= 128 K
 |f| = ceiling [log 128 K] bits = 17 bits
2
 Physical address = 17+12 bits

Operating System Concepts – 8th Edition 8.47 Silberschatz, Galvin and Gagne ©2009
Page Size
 Smaller page size, less amount of internal
fragmentation
 Smaller page size, more pages required per
process
 More pages per process means larger page tables

Operating System Concepts – 8th Edition 8.48 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
 Each page reference can cause two physical
memory accesses
 One to fetch the page table (PTBR) Page table
base register
 One to fetch the data
 To overcome this problem a high-speed cache is set
up for page table entries
 Called a Translation Lookaside Buffer (TLB)
 Contains page table entries that have been most
recently used

Operating System Concepts – 8th Edition 8.49 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
 Given a logical address, processor examines the
TLB
 If page table entry is present (TLB hit), the frame
number is retrieved and the real address is formed
 If page table entry is not found in the TLB (TLB
miss), the page number is used to index the process
page table

Operating System Concepts – 8th Edition 8.50 Silberschatz, Galvin and Gagne ©2009
Paging Hardware With TLB

Operating System Concepts – 8th Edition 8.51 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
 Teffective on a hit = TTLB + Tmem
 Teffective on a miss = TTLB + 2Tmem
 If HR is hit ratio and MR is miss ratio, then

Teffective = HR (TTLB + Tmem) + MR (TTLB + 2Tmem)

 Effective Access Time (EAT)

EAT = Hit_Ratio * Hit_Time + Miss_Ratio * Miss_Time

Operating System Concepts – 8th Edition 8.52 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
 Tmem = 100 nsec
 TTLB = 20 nsec
 Hit ratio is 80%
 Teffective = ?

Teffective = 0.8 (20 + 100) + 0.2 (20 + 2x100)


= 140 nanoseconds

Operating System Concepts – 8th Edition 8.53 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
 Tmem = 100 nsec
 TTLB = 20 nsec
 Hit ratio is 98%
 Teffective = ?

Teffective = 0.98 (20 + 100) + 0.02 (20 + 2x100)


= 122 nanoseconds

Operating System Concepts – 8th Edition 8.54 Silberschatz, Galvin and Gagne ©2009
Memory Protection

 Memory protection implemented by associating protection bit with each


page table entry

 Valid-invalid bit (v) attached to each entry in the page table:


 “valid” indicates that the associated page is in the process’ logical
address space, and is thus a legal page
 “invalid” indicates that the page is not in the process’ logical
address space
 Read, Write and Execute bits (r,w,x)

Operating System Concepts – 8th Edition 8.55 Silberschatz, Galvin and Gagne ©2009
Valid (v) or Invalid (i)
Bit In A Page Table

Operating System Concepts – 8th Edition 8.56 Silberschatz, Galvin and Gagne ©2009
Segmentation

 Memory-management scheme that supports user view of memory

 A program is a collection of segments


 A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays

Operating System Concepts – 8th Edition 8.57 Silberschatz, Galvin and Gagne ©2009
User’s View of a Program

Operating System Concepts – 8th Edition 8.58 Silberschatz, Galvin and Gagne ©2009
Logical View of Segmentation

4
1

3 2
4

logical memory space physical memory space

Operating System Concepts – 8th Edition 8.59 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
 Logical address consists of a two tuple:
<segment-number, offset>,

 Segment table – maps two-dimensional logical addresses to 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

Operating System Concepts – 8th Edition 8.60 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware

Operating System Concepts – 8th Edition 8.61 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware

Operating System Concepts – 8th Edition 8.62 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation

Operating System Concepts – 8th Edition 8.63 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
 Logical and Physical Addresses
 (2, 399) – PA: 4300+399 = 4699
 (4, 0) – PA: 4700+0 = 4700
 (4, 1000)  trap
 (3, 1300)  trap
 (6, 297)  trap

Operating System Concepts – 8th Edition 8.64 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation

Operating System Concepts – 8th Edition 8.65 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
 Dynamic Storage Allocation
 First fit
 Best fit
 Worst fit
 External fragmentation

Operating System Concepts – 8th Edition 8.66 Silberschatz, Galvin and Gagne ©2009
Paged Segmentation
 Divide every segment in a process into fixed size pages
 Need for a page table per segment
 CPU’s memory management unit must support both
segmentation and paging

s d
index segment
table

p d’
index page table offset within the page
p

Operating System Concepts – 8th Edition 8.67 Silberschatz, Galvin and Gagne ©2009
Examples

 If there are 64 pages, and the page size is


2048 bytes, the length of logical address
is??
 A computer with physical address space of
232 bytes has the frame size of 4096 (212)
bytes. If the hexadecimal physical address
is 34567890, the frame number in
hexadecimal would be:

Operating System Concepts – 8th Edition 8.68 Silberschatz, Galvin and Gagne ©2009
 In system where page size is 1024 bytes,
and the available physical memory is
equal to 218=256KB, the length of the
physical address is equal to ??

Operating System Concepts – 8th Edition 8.69 Silberschatz, Galvin and Gagne ©2009
Paging Example

Operating System Concepts – 8th Edition 8.70 Silberschatz, Galvin and Gagne ©2009
End of Chapter 8

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009

You might also like