Slides - Operating System
Slides - Operating System
andreas.woelfl@th-deg.de
MEMORY MANAGEMENT
MEMORY MANAGEMENT
1. CACHING
CACHING
Cache
Hardware or software used to store data temporarily in a computing context.
• When a cache client attempts to access data, it first checks the cache.
• If the data is found, that is referred to as a cache hit.
• If the data is not found, that is referred to as a cache miss
• In case of a hit, the data is taken directly from the cache.
• In case of a miss, the data is fetched the ordinary way and put into the cache.
• How data gets evicted from the cache depends on the replacement
algorithm1 .
1
We will learn some algorithms later in the lecture
Prof. Dr. Andreas Wölfl Operating Systems and Networks 3
CACHING
• L1-Cache: Built onto the CPU core itself. Used to speed up frequently and
recently used instructions and data.
• L2-Cache: Separate from the CPU cores, but built onto the CPU package.
Used to speed up references to main memory.
• LLC/L3-Cache: Built onto the the CPU package and shared among cores.
Mainly used to tackle the cache coherence problem, which aims to keep all
cached copies of the same memory identical.
• L2 cache access: 5 ns
L1 Cache L1 Cache
• L3 cache access: 10 ns
• RAM access: 100 ns L2 Cache L2 Cache
2. ADDRESS SPACES
ADDRESS SPACES
• Processes may trash the operating system (i.e., write garbage to memory
locations in kernel space → compromise the kernel).
• It is difficult to have multiple programs running at the same time (as two
processes may reference the same physical memory location).
Challenge: How can multiple programs execute without interfering with each
other when referencing main memory?
Address Space
The set of (virtual) addresses that a process can use to address memory.
Relocation
Maps each process’s address space onto a different part of physical memory
Swapping
Temporarily writing a process’s address space in its entirety to secondary
memory so that main memory can be made available for other processes.
• Simplest strategy to deal with memory overload that occurs if the physical
memory is not large enough to hold all processes.
• Causes idle processes to be stored mostly on disk, so they do not take up
any memory when not running.
• Swap out: Writing the address space to disk and removing it from memory.
• Swap in: Reading the address space from disk and loading it to memory.
• First fit: Scan the memory bottom-up until a hole is found that is big enough.
• Next fit: Variant of next fit where the scan starts from the place where it left
off the last time (instead of always at the beginning).
• Best fit: Searches the entire memory for the smalles hole that is adequate.
• Worst fit: Searches the entire memory for the largest hole that is adequate.
C
C C C
A A
B B
A A A D D D D
t0 t1 t2 t3 t4 t5 t6 t7
External Fragmentation
Situation where there is a sufficient quantity of free memory available to satisfy
a processes memory requirements, but the process cannot be loaded because
the memory offered is in a non-contiguous manner.
2
For example, on a 16GB machine that can copy 8 bytes in 8 ns, it would take about 16 seconds to
compact all of memory.
Prof. Dr. Andreas Wölfl Operating Systems and Networks 13
ADDRESS SPACES
Open problems:
Task
Solve the task ”Swapping” in iLearn.
3. VIRTUAL MEMORY
VIRTUAL MEMORY
Virtual Memory
A memory allocation scheme in which secondary memory can be addressed as
though it were part of main memory.
4
From 0 to a maximum, even if a lesser amount of RAM is installed.
5
4kB in most operating systems
Prof. Dr. Andreas Wölfl Operating Systems and Networks 16
VIRTUAL MEMORY
1 MOV EAX, 0x2004 # load memory location 0x2004 into register EAX
Example: The program tries to access address 819610 using the instruction:
Task
Solve the task ”Address Translation” in iLearn.
Page Tables
Data structure in RAM maintained by the operating system that holds the
mapping between pages and page frames.
• The page number is used as an index into the page table, yielding the
number of the corresponding page frame as well as a present/absent bit.
• If the present/absent bit is 0, a kernel trap called page fault is generated.
• If the present/absent bit is 1, the page frame number found in the page table
is copied to the most significant 3 bits of the output, along with the 12 bit
offset, which is copied unmodified from the incoming virtual address.
RAM MMU
Outgoing physical address (2458010)
Page Table
1 1 0 0 0 0 0 0 0 0 0 0 1 0 0
Frame P/A
15 000 0
14 000 0
13 000 0
12 000 0 110
11 111 1
10 000 0 12-bit offset copied
9 101 1 1
directly from input to
8 000 0 output
7 000 0 Present 0
6 000 0 Page Fault
bit?
5 011 1
4 100 1
3 000 1
2 110 1
1 001 1
0 001 1
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0
Virtual page = 2 is used
as an index to the table Incoming virtual address (819610)
Modified Present/Absent
Referenced Protection
• Protection: 3 bits that indicate what kinds of access are permitted (rwx).
• Referenced (R bit): Set if a page is accessed by the current instruction.
• Modified (M bit): Set as soon as a page is written to (stays set until unset).
6
Writes the page frame number, sets the P/A bit and unsets the M bit.
Prof. Dr. Andreas Wölfl Operating Systems and Networks 24
VIRTUAL MEMORY
Question: How does the operating system pick the page to be replaced?
• Label each page with the number of instructions that will be executed before
that page is first referenced (e.g, one page will not be used for 8 million
instructions, another page will not be used for 6 million instructions).
• On page fault: Pick the page with the highest label (i.e., push the page fault
that will fetch it back as far into the future as possible)
Problem: This algorithm is unrealizable. At the time of the page fault, the
operating system has no way of knowing when each of the pages will be
referenced next.
Observation:
• Pages that have been heavily in use in the last few instructions will probably
be heavily used again soon.
• Conversely, pages that have not been used for ages will probably remain
unused for a long time.
• Selects the page that has been unused for the longest time.
• Good approximation to the optimal algorithm (the best that we know).
incoming pages
1 2 3 2 1 5 2 3
3 2 1 5 2 3
page
2 2 3 2 1 5 2
frames
1 1 1 1 3 2 1 5
Counter
Referenced
• With each entry in the page table, a counter is associated (initially 0).
• During each instruction, the operating system scans all pages in memory and
adds the R bit to the counter (i.e., the counter roughly keeps track of how
often each page has been referenced).
• Select the page with the lowest counter.
Counter
Referenced
The Aging algorithm (small modification to NFU, simulates LRU quite well):
• Shift the counter to the right before appending the R bit to the left.
Task
Solve the task ”Page Replacement” in iLearn.
• Based on the observation that most programs tend to make a large number
of references to a small number of pages (and not the other way arround).
• Typically 256 entries, implemented in hardware.
• Can be searched in parallel.
• Huge speedup.
Example:
Task
Solve the task ”Translation Lookaside Buffer” in iLearn.
4. MEMORY ALLOCATION
MEMORY ALLOCATION
0xFFFF
Segments of a process’ address space: Kernel Space
high address
Stack
• Text: The instructions of the program.
• Data: Values of global and static variables.
• Heap: Area used for dynamic memory allocation.
• Stack: Area used for static memory allocation.
• Kernel Space: The kernel (e.g., system call Heap
Heap
Memory region from which a running program can request chunks.
Internal Fragmentation
Situation where a bigger amount of memory is allocated than demanded.
Example:
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
half word
byte
(upper bytes)
address space
half word
Stack
Memory region where data is added or removed in last-in-first-out (LIFO)
manner.
9
Varies among programming languages
Prof. Dr. Andreas Wölfl Operating Systems and Networks 43
MEMORY ALLOCATION
Example (The diagram represents the stack during execution of line 2):
Task
Solve the task ”Fragmentation” in iLearn.
5. SUMMARY
SUMMARY
Summary
You should have accquired the competencies to