CS代写 CSCI 4061 Introduction to Operating Systems – cscodehelp代写

CSCI 4061 Introduction to Operating Systems
Instructor:
Today
 OS Memory Management  Virtual Memory
 Paging and Swapping
2
Process Memory Layout
Args, environment
Stack
Heap
bss
Data Segment
Program Text
High Address
Low Address
3
Activation Records (function params, local vars, saved registers)
Dynamic Memory
Uninitialized Static Data Initialized Static Data
Binary Code
Where does the memory come from?
 Registers, cache, main memory (RAM)
 Process has to reside in the main memory
 The instructions being executed  Data segment
 Heap and stack
 Question: How is the process’s logical memory mapped to physical memory?
4
1

Multiprogramming
 OS executes multiple processes in quick succession
 Multiple processes need to reside in the memory
 What if there are 20 processes where:
 Each process has a memory map of 1GB
 Amount of RAM in your machine is 512 MB
 Questions:
 How to fit all processes in memory?
 What happens to the memory of a process when it is not running?
5
OS Memory Management
 OS manages the available physical memory to meet demands of multiple processes
 Needs to “fit” individual processes into the memory when running
 Needs to multiplex the available memory among multiple processes
 Goals:
 Maximum memory utilization  Highest level of performance
6
Memory Management Techniques
 Virtual Memory
 Hiding the physical memory details and constraints
from the processes and the programmer  Paging
 Breaking a process memory layout to “fit” it into available memory
 Swapping
 Using secondary storage to multiplex memory among multiple processes
7
Virtual Memory
 The process is given a “logical” view of the memory
 Contiguous
 Starting from low address (typically just above the OS image)
 Ending at high address (based on the architecture)
 The process references “virtual” memory addresses
 Not physical memory addresses
 Process has no knowledge of where an address would actually lie in physical memory
8
2

Virtual Memory Mapping
 Virtual addresses are mapped to physical addresses
 The instructions and data have to be in physical memory while being used
 The mapping is controlled by the OS and the hardware
 Transparent to the process
 Multiple processes can have same virtual addresses mapped to different physical addresses
9
Virtual Memory Mapping
Virt. Addr. 1052
Virt. Addr. 1052
Phys. Addr. 2560
Phys. Addr. 808
10
Process A Virtual Memory
Memory Management Unit (MMU)
Physical Memory
Process B Virtual Memory
Virtual Memory: Benefits
 Process unaware of physical memory layout
 Virtual addresses can be generated at link/load time
 Memory Protection
 Process cannot access another process’s memory directly
 Multiple processes can use the same (virtual) address space
 Generally the whole address space
 Multiple processes can share a physical memory region
 Mapped to different virtual memory regions  E.g.: shared libraries
11
How to do Virtual Memory Mapping?
 Approach 1: Map whole virtual address space to a contiguous physical memory region
 Problems?
 Approach 2: Map each virtual address
independently to a physical address  Problems?
12
3

Paging
 Page: Contiguous chunk of memory addresses  Process virtual memory is divided up into equal-
size pages
 Frame: Physical memory is also divided up into
same-sized chunks
 Virtual memory mapping: Maps virtual pages to physical frames
14
Paging
Virtual Memory
Physical Memory
Frame 7
Frame 4 Frame 3
Frame 1
Page 3
Page 2
Page 1
Page 0
Page Table
3
1
2
7
1
3
0
4
15
Paging: Address Translation
 Virtual address: VA = p*pg_size+d
 Page number (p)
 Page offset (d): Byte number within the page
 Physical address: PA = f*pg_size+d
 Frame number (f): Page table entry mapped to p  Frame offset (d): Same as page offset
16
Paging: Address Translation Example
 Assume page size = 1000
 Virtual address: 3456. What is:
 Page number (p)
 Page offset (d)  What is:
 Frame number (f)  Frame offset (d)  Physical Address
Page Table
Page no.
Frame no.
3
1
2
7
1
3
0
4
17
4

Paging: Benefits
 Virtual memory mapping is more efficient  Process does not have to be placed
contiguously
 OS can assign any available frame
 Locality: Bunches together memory chunks  Nearby addresses are used together
 E.g.: instructions in a loop, array elements
18
Swapping
 How does the OS “fit” multiple processes into a small physical memory?
 What happens to the memory of a process when it is not running on the CPU?
 Solution: Use secondary storage (disk) as a backup
19
Swapping
 Move inactive process to secondary storage
 Bring in active process when scheduled to run
 Swap space: Portion of hard disk devoted to swapping
Physical Memory Swap Space
Proc B
20
Proc A
Demand Paging
 Combines swapping and paging
 Process uses only part of its whole memory at a
given time
 Current set of instructions
 Portion of data, heap, and stack currently being
used
 Move inactive pages to disk
 Bring in pages from secondary memory when
accessed
21
5

Page Faults
 What happens when a process accesses an address, but the page is on the disk?
 OS generates an exception called page fault
 Goes to the swap space and finds the page
 Allocates a physical memory page
 Copies the contents of the page from the swap
space into the physical page
 Performance penalty: Takes a long time compared to a direct memory access
22
Page Fault Example
 How many page faults would happen?  Assume page size=1K
 Be careful in how you access memory
 Can have substantial effect on performance
23
char A[1024][1024];
for (i=0; i<1024; i++) for (j=0; j<1024; j++) A[i][j]=‘a’; char A[1024][1024]; for (j=0; j<1024; j++) for (i=0; i<1024; i++) A[i][j]=‘a’; 6

Leave a Reply

Your email address will not be published. Required fields are marked *