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

20-ch9_part3

The document covers key concepts in memory management, including contiguous memory allocation, segmentation, paging, and swapping. It explains the implementation of page tables, memory protection, shared pages, and the advantages of virtual memory, which allows processes to run without being fully loaded in physical memory. Additionally, it details demand paging, the handling of page faults, and the use of valid-invalid bits in managing memory access.

Uploaded by

otgameryt0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

20-ch9_part3

The document covers key concepts in memory management, including contiguous memory allocation, segmentation, paging, and swapping. It explains the implementation of page tables, memory protection, shared pages, and the advantages of virtual memory, which allows processes to run without being fully loaded in physical memory. Additionally, it details demand paging, the handling of page faults, and the use of valid-invalid bits in managing memory access.

Uploaded by

otgameryt0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Year: 2024-2025

Fall Semester

Operating Systems
Dr. Wafaa Samy
Dr. Hanaa Eissa

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.1 Modified by Dr. Wafaa Samy
Chapter 9: Main Memory
(Part 3)

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 Modified by Dr. Wafaa Samy
Chapter 9: Memory Management

 Background
 Contiguous Memory Allocation
 Segmentation
 Paging (Cont.)
 Swapping

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.3 Modified by Dr. Wafaa Samy
Free Frames

Before allocation After allocation


Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.4 Modified by Dr. Wafaa Samy
Example (1)
 Consider a computer system with a 32-bit logical address and 4 KB page
size.
 The system supports up to 512 MB of physical memory.
 How many entries are there in a conventional single-level page table?

 Answer:
 For given logical address space 2m and page size 2n

page number page offset


p d

# of pages= # of entries = ? m -n n
Size of logical address space = 2m = # of pages × page size
m = 32
232 = # of pages × 212
page size = 4 KB = 4 × 210
# of pages =232 / 212= 220 pages = 22 × 210 = 212
n = 12
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.5 Modified by Dr. Wafaa Samy
Implementation of Page Table
 Page table is kept in main memory:
• Page-table base register (PTBR) points to the page table.
• Page-table length register (PTLR) indicates size of the page table.
 In this scheme every data/instruction access requires two memory
accesses:
page number page offset
1. One for the page table and p d
2. one for the data / instruction. m -n n
 The two-memory access problem can be solved by the use of a special
fast-lookup hardware cache called translation look-aside buffers (TLBs)
(also called associative memory  parallel search).
Page # Frame #
• TLBs typically small (64 to 1,024 entries).
 Logical address translation (p, d):
• If p is in associative register, get frame # out.
• Otherwise, get frame # from page table in memory.
 On a TLB miss, value is loaded into the TLB for faster access next time:
• Replacement policies must be considered.
• Some entries can be wired down for permanent fast access.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.6 Modified by Dr. Wafaa Samy
Paging Hardware With TLB

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.7 Modified by Dr. Wafaa Samy
Memory Protection
 Memory protection implemented
by associating protection bit with
each frame to indicate if read-only
or read-write access is allowed.
• Can also add more bits to
indicate page execute-only,
and so on.
 Valid-invalid bit 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
Valid (v) or Invalid (i) Bit In A Page Table
page is not in the process ’
logical address space.
 Any violations result in a trap to
the kernel.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.8 Modified by Dr. Wafaa Samy
Shared Pages
 Shared code:
• One copy of read-only (reentrant) code shared among
processes (i.e. text editors, compilers, window systems).
• Similar to multiple threads sharing the same process space.
• Also, useful for interprocess communication if sharing of
read-write pages is allowed.

 Private code and data:


• Each process keeps a separate copy of the code and data.
• The pages for the private code and data can appear
anywhere in the logical address space.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.9 Modified by Dr. Wafaa Samy
Shared Pages Example
Example:
• Three processes sharing
the pages for the
standard C library libc.
• Only one copy of the
standard C library need be
kept in physical memory,
and the page table for
each user process maps
onto the same physical
copy of libc.
• Note: Although the figure
shows the libc library
occupying four pages, in
reality, it would occupy
more.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.10 Modified by Dr. Wafaa Samy
Swapping
 A process can be swapped temporarily out of memory to a backing store
(e.g. hard disk), and then brought back into memory for continued execution.
• Total physical memory space of all processes can exceed physical
memory.
 Backing store – fast disk large enough to accommodate copies of all
memory images for all users; must provide direct access to these memory
images.
 The advantage of standard swapping is that it allows physical memory to be
oversubscribed, so that the system can accommodate more processes
than there is actual physical memory to store them.
 Roll out, roll in – swapping variant used for priority-based scheduling
algorithms; lower-priority process is swapped out so higher-priority process
can be loaded and executed.
 Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped.
 System maintains a ready queue of ready-to-run processes which have
memory images on disk.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.11 Modified by Dr. Wafaa Samy
Schematic View of Swapping
• Standard swapping of two processes using a disk as a backing store.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.12 Modified by Dr. Wafaa Samy
Swapping with Paging
 Swapping not used in modern operating systems because the amount of
time required to move entire processes between memory and the
backing store is prohibitive but modified versions are common.
 Modified versions of swapping are found on many systems (i.e., UNIX,
Linux, and Windows).
 Swap only when free memory extremely low.
 Pages of a process—rather than an entire process—can be swapped.
• Swapping normally disabled.
• Started if more than threshold amount of memory allocated.
• Disabled again once memory demand reduced below threshold.
 Swapping with paging.
• A page out operation moves a page from memory to the backing
store;
• The reverse process is known as a page in.
 Most operating systems for PCs and servers support swapping
pages.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.13 Modified by Dr. Wafaa Samy
Swapping with Paging

e.g. a subset of pages for processes A and B are


Operating System Concepts – 10th Edition
being paged-out and paged-in respectively.
Silberschatz, Galvin and Gagne ©2018 9.14 Modified by Dr. Wafaa Samy
Chapter 10: Virtual Memory

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 Modified by Dr. Wafaa Samy
Chapter 10: Virtual Memory
 Background
 Demand Paging

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.16 Modified by Dr. Wafaa Samy
Background
 Code needs to be in memory to execute, but entire program rarely
used.
• Error code, unusual routines, large data structures.
 Entire program code not needed at same time.
 Consider ability to execute partially-loaded program.
• Program no longer constrained by limits of physical memory.
• Each program takes less memory while running  more
programs run at the same time.
 Increased CPU utilization and throughput with no increase in
response time or turnaround time.
• Less I/O needed to load or swap programs into memory  each
user program runs faster.
 Thus, running a program that is not entirely in memory would
benefit both the system and its users.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.17 Modified by Dr. Wafaa Samy
Virtual Memory
 Virtual memory is a technique that allows the execution of processes that are
not completely in memory.
 Virtual memory – 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.
• More programs running concurrently.
• Less I/O needed to load or swap processes.
 Advantages:
• Programs can be larger than physical memory.
• Virtual memory abstracts main memory into an extremely large, uniform
array of storage, separating logical memory as viewed by the programmer
from physical memory.
• This technique frees programmers from the concerns of memory-storage
limitations.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.18 Modified by Dr. Wafaa Samy
Virtual Memory (Cont.)
 Virtual address space – logical view of how process is stored in
memory:
• Usually start at address 0, contiguous addresses until end of
space.
• Meanwhile, physical memory organized in page frames.
• The physical page frames assigned to a process may not be
contiguous.
• Memory Management Unit (MMU) must map logical pages to
physical page frames in memory.
 Virtual memory can be implemented via:
• Demand paging.
• Demand segmentation.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.19 Modified by Dr. Wafaa Samy
Virtual Memory That is Larger Than Physical Memory

Diagram showing virtual memory that is


larger than physical memory
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.20 Modified by Dr. Wafaa Samy
Demand Paging
 Could bring entire process into memory at load time.
or
 Demand paging: bring a page into memory only
when it is needed.
• Less I/O needed, no unnecessary I/O.
• Less memory needed.
• Faster response.
• More users.

 With demand-paged virtual memory, pages are loaded only when they
are demanded during program execution. Pages that are never accessed
are thus never loaded into physical memory.
 Similar to paging system with swapping.
 Page is needed  reference to it.
• invalid reference  abort.
• not-in-memory  bring to memory.
 Lazy swapper – never swaps a page into memory unless page will be needed.
• Swapper that deals with pages is a pager.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.21 Modified by Dr. Wafaa Samy
Basic Concepts
 With swapping, pager guesses which pages will be used before
swapping out again.
 Instead, pager brings in only those pages into memory.
 How to determine that set of pages?
• Need new MMU functionality to implement demand paging.
 If pages needed are already memory resident.
• No difference from non demand-paging.
 If page needed and not memory resident.
• Need to detect and load the page into memory from storage:
 Without changing program behavior.
 Without programmer needing to change code.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.22 Modified by Dr. Wafaa Samy
Page Table When Some Pages Are Not
in Main Memory

• While a process is
executing, some
pages will be in
memory, and some
will be in secondary
storage.
• Thus, we need some
form of hardware
support to
distinguish between
the two:
 The valid–invalid
bit scheme can be
used for this
purpose.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.23 Modified by Dr. Wafaa Samy
Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(v  in-memory – memory resident, i  not-in-memory).
 Initially valid–invalid bit is set to i on all entries.
 Example of a page table snapshot:

 During MMU address translation, if valid–invalid bit in page table


entry is i  page fault.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.24 Modified by Dr. Wafaa Samy
Steps in Handling Page Fault
1. If there is a reference to a page, first reference to that page
will trap to operating system.
• Page fault.
2. Operating system looks at another table to decide:
• Invalid reference  abort.
• Just not in memory.
3. Find free frame.
4. Swap page into frame via scheduled disk operation.
5. Reset tables to indicate page now in memory:
Set validation bit = v
6. Restart the instruction that caused the page fault.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.25 Modified by Dr. Wafaa Samy
Steps in Handling a Page Fault (Cont.)

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 9.26 Modified by Dr. Wafaa Samy
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 9.27 Modified by Dr. Wafaa Samy

You might also like