Professional Documents
Culture Documents
Data: for global variables - Logical memory remains contiguous but physical memory
Mutual exclusion: If there is a process in CS then all other
Heap: for dynamic allocations
1. Keep frame size (page size) as a power-of-2
process cannot enter CS may be disjoint
Segment Table
Stack: for function invocations 2. Physical frame size == Logical page size
Progress: If no process is in CS then one waiting process Address translation: P
should be granted access - Make frame size
Transient data: variables with automatic storage duration Page(= page size) aOffset
Number power-of-2 <
Yes Frame
Number
Bounded wait: After a process requests to enter the critical Physical
Persistent data: globals, dynamically allocated memory Logical No
section, there exists an upper bound of number of times Address
p d Memory
Addressing
other processes can enter the CS before this process Page
(m – n) bits n bits Error!
Independence: Process not in CS should never block other Alternatives for memory abstraction: Table
Process P0 Process P1 with TLB: Thrashing: Page fault happens too often
Assumption: Linear search based: = T LBhit + T LBmiss Page Table - for well-behaved programs it is unlikely to happen due to
Writing to Turn is an atomic operation
First-Fit: take the first hole that is large enough = 40% × (1ns + 50ns) + 60% × (1ns + 50ns + 50ns) temporal and spatial locality
[ CS2106 L8 - AY1819S1 ]
[ CS2106 L6 - AY1819S1 ] 14
21
Disadvantages: busy waiting, low level, not general Best-Fit: take the smallest hole that is large enough
Worst-Fit: take the largest hole Context switching & TLB: On context switch: Page Table Structure
Merging & Compaction: - TLB entries are flushed (so incoming process won’t get
Semaphores Direct paging: All pages in single table, might occupy
Buddy System: Example
When partition is freed, try merging with adjacent holes incorrect translation)
several memory pages
Properties of correct implementation: Can move occupied partitions around to consolidate holes
Assume:
- (Optional) Save TLB state to PCB, and restore TLB data
Mutual exclusion: If there is a process in CS then all other the largest block is 512 (29)
for incoming process (to reduce TLB misses) 2-level paging: Keep a page directory, [[TODO]]
process cannot enter CS Buddy system:
Only one free block of size 512 initially (x86) On a TLB miss, the hardware searches through the Inverted page table: Single table for all processes, stores
Wait()/P()/Down() and Signal()/V()/Up() T1 Request 100 page table (without invoking the OS); OS is informed only (pid, logical page) indexed by frame number
A[9] 0 on page fault
Threads queue up on a semaphore (fair scheduling) Block P allocated at 0
A[8] size = 128 Page Replacement Algorithms
Extensions for protection:
General semaphore: value can be any non-negative
integer
…
…
Starting address = 0
Size = 29
A[9] Logical Address Translation: Illustration
Access-right (RWX) bits: memory access is checked against
Optimum (OPT): Replace the page that will not be used
… A[8] 256 access right bits (by hardware)
Assume: again for the 7500
longest period of time, not feasible as it needs
Binary semaphore: value can be only 0 or 1 (undefined A[1] A[7]
Valid bit: represent
User invalid
Code logical
Segmentaddresses,
= 0invalid access future knowledge Global
128
behaviour to Signal() on binary semaphore which is NULL pointer to will be caught Global
by OS Data Segment = 1 Data
A[0] indicate no free
… First In First Out (FIFO): Evict the oldest page first
currently 1) Segment
block of this size … Page sharing:Heap Segment
Several = physical
processes use same 2 frame - simple to implement, OS maintains a queue of resident
Producer Consumer: Blocking Version … Stack Segment = 3
Producer-Consumer: - e.g. shared libraries, copy-on-write from fork() page numbers 6000
0 A[0]
while (TRUE) { while (TRUE) {
Free Memory = 512 Memory Access - can be tricked: try 3 / 4 frames with 1 2 3 4 1 2 5 1 2 3 4 5
Produce Item; 0 128 256
wait( notEmpty ); P Free Free Segmentation (Belady’s Anomaly)
5700
< Segment Id, Offset > User locality
wait( notFull ); wait( mutex ); [ CS2106 L7 - AY1819S1 ]
38 - does not exploit temporal
wait( mutex ); item = buffer[out]; - Each element A[J] is a linked list that keeps track of free Code
- Each region of memory is
Least Recently Used
buffer[in] = item;
in = (in+1) % K;
out = (out+1) % K;
blocks of size 2J Base Limit (LRU): Evict the page that has
Segment
count--; placed in a separate segment
count++; signal( mutex ); - Each free block is indicated just by the starting address 3500 2200 not been used for the longest time
signal( mutex ); signal( notFull ); so they can grow/shrink freely 0 3500
- There might be a smallest allocatable block size, i.e. a - makes use of temporalHeaplocality
}
signal( notEmpty );
Consume Item; constant K > 0 such that A[J] exists only when J ≥ K - Each memory segment has a 1 6000 1500 2900
- does not suffer from Belady’s Anomaly
Segment
}
- To allocate size 2S : find smallest free block with segment id and limit - difficult to2400
implement, needs hardware support:
Producer Process Consumer Process 2 2400 1100
size ≥ 2S , then repeatedly split until there is a block of size - Memory references are (option 1) store “time-of-use” and update it on every access,
Initial Values: 2S , then return that block of size 2S specified as: 3 0 1300 need to search1300
through all pages to find earliest time-of-use
count = in = out = 0 Stack
mutex = S(1), notFull = S(K), notEmpty = S(0)
- To deallocate: if buddy is also free, merge with buddy and segment id + offset segment table (option 2) maintain Segment when page is accessed,
a “stack”;
repeat; otherwise add block to the linked list - Can cause external fragmentation remove from stack (if exists) and push on top of stack
[ CS2106 L6 - AY1819S1 ]
38
< 2, 500 > 0
Physical Memory
Second Chance Page Replacement (CLOCK): File System Management File System Implementations Disk Scheduling
Maintain a circular queue of page numbers, and each page Magnetic Disk in One Glance
table entry has a “reference bit”
Second-Chance Page Replacement (CLOCK) Access types: Read, Write, Execute, Append, Delete,
Generic
Generic Disk
disk Organization:
organization: Illustration
Rotation
General Idea: List (retrieve metadata of the file) Simple Partition
(Change Sector)
Boot Code Table
Modified FIFO to give a second chance to pages
that are accessed Access control list (ACL): list of user identities and Seek
(Change Track)
Each PTE now maintains a "reference bit": allowed access types (very customizable but use large space)
1 = Accessed, 0 = Not accessed MBR Partition Partition …… Partition Track
Algorithm:
1. The oldest FIFO page is selected Permission bits: Owner/Group/Universe,
Read/Write/Execute, e.g. rwxr--r-- Disk
2. If reference bit == 0 Page is replaced
Head
3. If reference bit == 1 Page is given a 2nd chance OS Boot Partition Directory Files
File Data Sector
Reference bit cleared to 0 Block Details Structure Info
Arrival time reset page taken as newly loaded In Unix, Minimal ACL = permission bits,
Next FIFO page is selected, go to Step 2 Extended ACL = add named users/groups Information for all Data for all files First-Come-First-Serve (FCFS)
[ CS2106 L11 - AY1819S1 ]
are here 37
Degenerate into FIFO algorithm files are here
When all pages has reference bit == 1 [ CS2106 L11 - AY1819S1 ]
Shortest Seek First (SSF): closest track first
Second-Chance: Implementation Details File structure: 6
[ CS2106 L9 - AY1819S1 ]
37
Array of bytes (usual) Block Allocation SCAN (elevator), C-SCAN (outside to inside only)
Use circular queue Arr. of fixed-length records (can jump to any record easily)
to maintain the
pages: Arr. of var.-length records (flexible but hard to find record) Contiguous: allocate consecutive disk blocks to a file LOOK (real elevator)
With a pointer - simple to keep track, fast access (no need to seek)
pointing to the oldest - has external fragmentation
page ( the victim
page ) Access methods: File System Case Studies
Sequential access: have to read and rewind on order Linked list: each disk block stores next block number too
To find a page to be Random access: read in any order, exposed via either way:
FAT Microsoft FAT File System Layout
- no external fragmentation
replaced: 1. Read(Offset): every read explicitly states position
Advance until a - slow random access to file, part of block is used for pointer
page with '0' 2. Seek(Offset): special operation to move to new location Layout:
reference bit Direct access: like random access, but for fixed-length File allocation table (FAT): next block numbers stored
Clear the reference records (e.g. in database) in single table that is always in memory M
B partition 1 partition 2 partition 3 partition 4
bit as pointer passes - faster random access R
through
- FAT keeps track of all disk blocks (takes up memory
Generic operations on file data: Create, Open, Read, space)
[ CS2106 L9 - AY1819S1 ]
38 Write, Reposition/Seek, Truncate
Indexed allocation: each file has an index block (stores
FAT
FAT Duplicate
Info kept for opened file: File pointer (current location list of blocks containing the file)
Frame Allocation - lesser memory overhead, fast direct access
optional
in file), Disk location, Open count (number of processed Boot root data blocks
that has this file opened) - limited max file size, index block overhead directory
Simple Approaches:
Equal allocation: Each process gets same number of frames Indexed allocation with linked list: index block File [allocation table contains one of:
CS2106 L12 - AY1819S1 ]
Proportional allocation: Each process gets number of Open file table (Unix): contains a pointer to next index block (of the same file) - FREE 4
frames proportional to its memory usage File Operations: Unix Illustration - no file size limit - <Block number> of next block
Op.Type: …
- EOF
Proc A PCB 0 File offset: … Multi-level index: like multi-level paging - BAD
Local/Global Replacement: Process make
file system 0
"File Data":
- very large file size limit Directory Entry Illustration
Local replacement: Evicted page selected from same process calls, usually 1 File1.abc
Combination: e.g. Unix I-node Directory … each
with file … … data block entry: file/subdirectory is
- thrashing limited to single process descriptor fd … …
- special
of a type of file represented as a
directory directory entry
Global replacement: Evicted page can be from any process fd Op.Type: Read
- root directory …
…
is stored in a special location, other
- can cause thrashing in other processes File Descriptor
x File offset: 1234
"File Data": Free Space Management
System Table directories stored in normal data blocks
Calls
… … Bitmap: Each disk block represented by 1 bit 8 bytes 3 1 10 2 2 2 4
Proc B PCB
- e.g. 1=free, 0=occupied
Working Set Model: 0
Op.Type: Write File Name Reserved
1 y File offset: 5678 File2.def
- Set of pages referenced by a process is relatively constant "File Data": Linked list: Use an unrolled linked list of disk blocks
… …
in a period of time (“locality”) - store the free list in free disk blocks File
fd Creation Date
- Page fault only
Working Setwhen changing
Model: to new locality
Illustration … … Extension
+ Time
File Descriptor
- Use magic constant 4 = working set window (interval) Table
Open File Table "Actual File"
Example memory reference strings [ CS2106 L10 - AY1819S1 ] Directory Implementation Attributes First Disk
Block
- Two file descriptors (i.e. open file table entries) can point 28
… 2 6 5 7 1 1 2 7 5 6 3 4 4 4 3 5 3 …
to same file (e.g. two process open same file, or same sub-directory is usually stored as file entry with special type File Size in Bytes
[ CS2106 L12 - AY1819S1 ]
8
∆ ∆ process open file twice) in a directory
- Two processes use the same file descriptor (i.e. open file File deletion: set first letter of filename to 0xE5
t1 t2 table entry) (e.g. after fork()) Linear list:
- requires linear search, usually last few searches are cached Free space: must be calculated by going through FAT
Assume
∆ = an interval of 5 memory references Links in directory structure): Hash table: Clusters: (for newer FATs) group of disk blocks as
∆)={1,2,5,6,7} (5 frames needed)
W(t1,∆ Hard link (limited to file only): ref counted, creates DAG - file name is hashed smallest allocation unit
Symbolic link (can be file or directory): uses special link - fast lookup, but hash table has limited size and depends
W(t2,∆
∆)={3,4} (2 frames needed) file, can create general graph on good hash function Virtual FAT: use multiple dir. entries for long file name
Try using different ∆ values
Ext2
Layout:
Ext2 FS: Layout
M
B partition 1 partition 2 partition 3 partition 4
R
Group
Descriptors
Data Blocks
Block
Super- Bitmap
Block I-node I-node
Bitmap Table
91 F 4 Hi.c 39 F 8 lab5.pdf
74 D 3 sub 0
Name
Use a 0 I-Node
Name Length number to indicate
unused entry
Entry Type
Entry Size
I-Node Number
[ CS2106 L12 - AY1819S1 ]
30
Deleting a file:
- remove its directory entry from the parent directory by
adjusting previous size to point to next entry
- update I-node bitmap by marking file’s I-node as free
- update block bitmap by marking file’s blocks as free
Journaling
Write information or actual data to separate log file before
performing file operation, so it can recover from system crash