Virtual Memory: Virtual Memory - Separation of User Logical Memory From Physical Memory
Virtual Memory: Virtual Memory - Separation of User Logical Memory From Physical Memory
Background
Demand Paging
Process Creation
Page Replacement
Allocation of Frames
Thrashing
Operating System Examples
Background
Valid-Invalid Bit
Page Fault
• If there is ever a reference to a page, first reference will trap to
OS ⇒ page fault
• OS looks at another table to decide:
Invalid reference ⇒ abort.
Just not in memory.
• Get empty frame.
• Swap page into frame.
• Reset tables, validation bit = 1.
• Restart instruction: Least Recently Used
block move
Page Replacement
Page Replacement
Page Replacement Algorithms
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory at a time per process)
1 1 4 5
9 page faults
2 1 3
2
2 4
3 3
• 4 frames
1 1 5
4
2 2 1
3 3 2 5 10 page faults
4 4 3
Optimal Algorithm
• Replace page that will not be used for longest period of time.
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
2 6 page faults
3
4 5
1 5
2
3 5 4
4 3
• Counter implementation
✦ Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter.
✦ When a page needs to be changed, look at the counters to determine which
are to change.
• Reference bit
✦ With each page associate a bit, initially = 0
✦ When page is referenced bit set to 1.
✦ Replace the one which is 0 (if one exists). We do not know the order,
however.
• Second chance
✦ Need reference bit.
✦ Clock replacement.
✦ If page to be replaced (in clock order) has reference bit = 1. then:
✔ set reference bit 0.
✔ leave page in memory.
✔ replace next page (in clock order), subject to same rules.
• Keep a counter of the number of references that have been made to each page.
• LFU Algorithm: replaces page with smallest count.
• MFU Algorithm: based on the argument that the page with the smallest count was
probably just brought in and has yet to be used.
Allocation of Frames
• Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages.
• Proportional allocation – Allocate according to the size of process.
Priority Allocation
• Use a proportional allocation scheme using priorities rather than size.
• 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.
Thrashing
• 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 added to the system.
• Thrashing ≡ a process is busy swapping pages in and out.
File Concept
Access Methods
Directory Structure
File System Mounting
File Sharing
Protection
File Concept
File Structure
File Attributes
• Name – only information kept in human-readable form.
• Type – needed for systems that support different types.
• Location – pointer to file location on device.
• Size – current file size.
• Protection – controls who can do reading, writing, executing.
• Time, date, and user identification – data for protection, security, and usage
monitoring.
• Information about files are kept in the directory structure, which is maintained on
the disk.
File Operations
• Create
• Write
• Read
• Reposition within file – file seek
• Delete
• Truncate
• Open(Fi) – search the directory structure on disk for entry Fi, and move the
content of entry to memory.
• Close (Fi) – move the content of entry Fi in memory to directory structure on
disk.
File Types – Name, Extension
Access Methods
• Sequential Access
read next
write next
reset
no read after last write
(rewrite)
• Direct Access
read n
write n
position to n
read next
write next
rewrite n
n = relative block number
Sequential-access File
Directory
Files
F1 F2 F4
F3
Fn
Both the directory structure and the files reside on disk. Backups of these two
structures are kept on tapes.
A Typical File-system Organization
Single-Level Directory
Two-Level Directory
• Path name
• Can have the same file name for different user
• Efficient searching
• No grouping capability
Tree-Structured Directories
■ Efficient searching
■ Grouping Capability
■ Current directory (working directory)
✦ cd /spell/mail/prog
✦ type list
■ Absolute or relative path name
■ Creating a new file is done in current directory.
■ Delete a file
rm <file-name>
■ Creating a new subdirectory is done in current directory.
mkdir <dir-name>
Example: if in current directory /mail
mkdir count