COMP 3511 - Operating System
COMP 3511 - Operating System
12:03 AM
Interrupts
Computer Organization
- Signal sent to CPU Stop currently running prog Allow OS do
immediate jobs
- CPUs:
Move data from/to main mem, local
buffer
- Device controller:
In charge of a device
- Bus:
Connect CPUs, device controllers
Provide access to shared mem
- Types:
External:
- Multiprocessor system:
2 processors in close comm, share bus, block, mem,
peripheral devices
- Multiprocessor system:
2 processors in close comm, share bus, block, mem,
peripheral devices
Advantages:
Type
Feature
Register
Volatile
In CPU, high-speed
Cache
Volatile
In CPU/Module
Volatile
Caching
- Clustered Systems:
11:33 PM
OS Implementation
Operating System
- Multiprogram:
Organize jobs (code + data) Keep CPU busy all times
Job scheduling:
Select & run job
When job has to wait (e.g. I/O), switch to another
- Multitasking/Time sharing:
CPU switches jobs frequently Users can interact with
each job while it running Interactive platform
- Dual-mode Operation:
User mode:
Certain machine instruction, reg, mem locs not avail
I/O can't accessed
12:53 AM
System call
- Implementation:
OS Services
- User interface:
Touchscreen
- Sys call: Prog request services from OS kernel
- Param passing:
- Error detection
- Types:
Process control: Create, terminate, wait,
File management
Device management: Request, release device,
OS System Design
Layered
Microkernel
Modular
- OS = n layers
- Microkernel:
- Layered + Microkernel:
- Limited structuring
- Typically: Kernel =
Everything below
sys-call, above
physical hardware
Layer 0 = Hardware
Layer n = UI
Layered:
Each kernel section has defined, protected
interface
BUT no hierarchy: Any module can call
any others
Microkernel:
Core module: Core funcs, knowledge of
how to load & comm with sub-modules
BUT: Sub-modules no need to invoke
msg passing to comm
Microkernel:
Pros
Little overhead
Performance
Info hiding:
Upper not need to know how lower
implemented, only know what it does
& how to use them
Cons
Difficult to
implement &
maintain
Virtualization
- VM (Virtual machine):
Create illusion of multiple procs, each exec on
own processors with own (virtual) mem
Benefits:
Programming simplicity: Each procs think
it has all mem/CPU time
Fault isolation:
Procs unable to directly impact others
Bugs not crash whole machine
Protection & portability
- Virtualization: Run OS as app within another OS
- Emulation:
Used when source CPU type target CPU type
Allow apps compiled for source CPU run on
target CPU
- Performance:
Process
Wednesday, September 21, 2016
1:18 PM
Overview
Mem-management info
Accounting info:
1 prog 1 proc
- Components:
Text: Prog code
Process Scheduling
Type
Long-term
Short-term
Medium-term
Function
Job scheduler:
CPU scheduler:
Process-swapping
scheduler:
Provide
Provide lesser
Reduce
Minimal
Part of
Speed
Lowest
Highest
Middle
Control deg of
multiprog
- States:
New: Created
Running: Instruction exec
Waiting: Wait for some event to occur (I/O)
Context Switch
Process Creation
- What OS does:
Create new PCB (inexpensive)
Setup new page tables for addr space (more expensive)
Copy data from parent proc (very expensive)
- What OS does:
Create new PCB (inexpensive)
Setup new page tables for addr space (more expensive)
Copy data from parent proc (very expensive)
Copy I/O state (medium expensive)
- UNIX commands:
fork(): Create new proc:
Process Termination
- UNIX commands:
exit(): Proc exec last statement, ask OS to delete it,
dealloc resource
abort(): Parent terminates exec of child proc, when:
Child exceed usage of resources
- Why IPC:
Info sharing
Computation speedup (Subtask exec in parallel, )
Modularity (Sys func divided into multiple procs, thread, )
Convenience (Users multitask, )
- Shared Mem (Implicit: no msg exchanged)
How:
Indirect Comm
activities
Ordinary pipes (UNIX)
Direct Comm
Named pipes:
Bidirectional, no parent-child rel
Can be used by 2 procs
Indirect Comm
Comm 1-to-1
mode
suppor
t
- Many-to-Many
- Complex implementation
- Latency, Overhead
Sync
Async
Pros
Cons
- Sender: Can't do extra while waiting - Buffer space problem: Sender &
for receipt confirmation
Receiver must buffer msg until
receipt confirmed
- Receiver: Can't do extra besides
- Receiver must test very often to
waiting for msg
test if new msg waiting Difficult
in interrupt & signal prog
Multithreaded Programming
Thursday, September 29, 2016
5:16 PM
Parallelism vs Concurrency
- Benefits:
Responsiveness: Allow continuous exec if part of
process blocked, especially important for user interface
Resource sharing:
Threads within proc share resources by default
Easier than shared mem, msg passing (must
explicitly arranged by programmer)
Amdahl's Law
Economy:
Thread creation much cheaper than process
creation
Thread switching much lower overhead than
proc's context switching
- Thread params:
Private: Thread Control Block (TCB)
Exec info: CPU Register, program counter, stack
Scheduling info: state, priority, CPU time
Accounting info
Many-to-One
One-to-One
Many-to-Many
- More concurrency
than Many-to-One
- Allow OS to create
sufficient no of kernel
threads
than Many-to-One
threads
Threading Issues
Solaris Green
Windows NT
GNU Portable
Linux
Solaris 8.x
Process Scheduling
Thursday, September 29, 2016
5:16 PM
Dispatcher
- Module that gives CPU control to proc selected by shortterm scheduler
CPU Scheduling
- Steps:
- Def: Act of selecting next proc for CPU to service once current proc
leaves CPU idle
Switch context
Switch user mode
- Criteria:
Max CPU utilization: Keep CPU as busy as possible
Max Throughput: No of proc completing exec per time unit
Min Turnaround time: Time to execute a proc (from time of
submitting proc to ready queue to time of completion)
Min Waiting time: Time proc spending waiting in ready queue
Min Response time: Time from when proc submitted until its
first response produced (in time-sharing env)
n+1 = tn + (1 - ) n
= 0.5
By interrupts
3: Proc switch from waiting ready
I/O complete
4: Proc terminated
10
- Scheduling type:
Preemptive (2 & 3): Currently running proc may be interrupted
& moved to ready state by OS
Non-preemptive (1 & 4): If proc in running state, it continues to
execute until termination / I/O required / Syscall required
Scheduling Algorithm
Name
Details
Preemptive / Issue
Nonpreemptive
Nonpreemptive
Example
- Cons:
Short proc behind long proc CPU
utilization
(FCFS)
Proc
P1
24
P2
P3
10
Priority
Schedulin
g
Both
Non-preemptive prio-scheduling
Proc
P1
10
P2
P3
P4
P5
ShortestJob-First
(SJF)
Nonpreemptive
- Pros:
Optimal waiting time
High throughput
- Cons: Starvation
ShortestRemaining
-TimeFirst
(SRTF)
Preemptive
- Pros:
P5
Proc
P1
P2
P3
P4
Proc
P1
P2
P3
P4
Proc
P1
24
P2
P3
10
Multilevel Queue
- Mem stall: When proc access mem, it spends significant time amount
waiting for data to be avail (due to cache miss, )
Multiprocessor Scheduling
(for heterogeneous sys (all CPUs of same type))
- Approaches:
Asym:
- Scheduling takes advantage of mem stall to make progress on
another thread
Dual thread processor core (2 logical processor)
Dual thread, dual core sys = 4 logical cores
Thread Scheduling
Unbound Threads /
Bound Threads /
Many-to-1
1-to-1
Many-to-many
- Association of user-lvl thread to
LWPs managed by thread library
- Simulation
- Real implementation
Process Synchronization
Wednesday, October 19, 2016
9:19 AM
Race Condition
{ Remainder Section }
} while (true)
Entry section: Request entry into critical section
Proc
C++ code
Proc instructions
P0
P1
Counter--
Register2 = Counter
Register2 = Register2 - 1
Counter = Register2
- Solution:
Mutual Exclusion: Only 1 proc in Critical Section at any time
Proc
Hardware Instruction
Value
P0
Register1 = Counter
Register1 = 5
P0
P1
Register2 = Counter
P1
P0
Counter = Register1
Counter = 6
P1
Counter = Register2
Counter = 4
Register2 = 5
General approaches:
- Protect critical regions via locks
Implemented by special atomic hardware instruction
Test/Set/Swap mem words
- Disable interrupt:
Make currently running code execute without being preempted by others
Not work for multiprocessor env
Initialization:
int intendToEnter[] = {0, 0};
// turn = i: It's turn for proc i to enter Critical Section
int turn;
Lock-based Solution
1. Test-and-Set:
P1
while (1) {
/* Entry section */
intendToEnter[0] = 1;
turn = 1;
while (intendToEnter[1]
&& turn == 1);
while (1) {
/* Entry section */
intendToEnter[1] = 1;
turn = 0;
while (intendToEnter[0] &&
turn == 0);
{CRITICAL SECTION}
{CRITICAL SECTION}
/* Exit section */
intendToEnter[0] = 0;
/* Exit section */
intendToEnter[1] = 0;
{REMAINDER SECTION}
}
{REMAINDER SECTION}
}
3. Proof of Correctness:
- Mutual exclusion:
For both P0 & P1 to enter Critical Section:
intendToEnter[0] = intendToEnter[1] = 1
turn = 0 & turn = 1 at same time
Impossible
b. Implementation:
// Initialization:
// lock = 1: A proc is still in Critical Section
int lock = 0;
// waiting[i] = 1: Proc i is waiting to enter Critical
Section
int waiting[MAX_N];
memset(waiting, sizeof(waiting), 0);
// Acquire lock
int TestAndSet(int* target) {
int retVal = *target;
*target = 1;
return retVal;
}
// Pi's code
do {
/* Entry Section */
waiting[i] = 1;
while (waiting[i] && TestAndSet(&lock));
waiting[i] = 0;
{CRITICAL SECTION}
- Progress:
P0 can be prevented from entering Critical Section if
/* Exit Section */
// Scan waiting[] in cyclic ordering i+1, i+2, , n-1,
{CRITICAL SECTION}
- Progress:
P0 can be prevented from entering Critical Section if
/* Exit Section */
// Scan waiting[] in cyclic ordering i+1, i+2, , n-1,
0, , i-1
// Find first proc j with waiting[j] = 1
int j = (i + 1) % n;
while (j != i && !waiting[j]) j = (j + 1) % n;
P0 can't be prevented
- Bounded-Waiting:
{REMAINDER SECTION}
} while (1)
Spinlock
lock = 0 only if: Pi is the first to enter Critical Section. After entering
Critical Section, lock = 1
- Proc waste CPU cycle due to busy waiting (using loop to
repeated check condition) for entry to Critical Section
- Bounded-Waiting:
2. Mutex lock
Semaphore
b. Implementation:
// Initialization
bool available = true;
void
wait(int& S) {
while (S <= 0);
S--;
void signal(int& S) {
S++;
}
Support funct
Proc code
Acquire() {
// Busy Waiting
while (!available);
available = false;
}
do {
Acquire();
{CRITICAL SECTION}
Release();
- Type:
Counting: No restrict for S's domain
Release() {
available = true;
}
Binary: S {0, 1}
{REMAINDER SECTION}
} while (true)
- Non-busy-waiting implementation:
struct Semaphore {
int value;
ProcessWaitingQueue Q;
}
void wait(Semaphore& S) {
S.value--;
void signal(Semaphore& S) {
S.value++;
if (S.value < 0) {
/* Push proc invoking wait()
to waiting queue */
S.Q.push(GetInvokingProc());
if (S.value <= 0) {
Process P = S.Q.pop()
/* Syscall: Resume
exec of previously
blocked proc */
wakeup(P)
}
}
- Deadlock: 2 waiting procs waits indefinitely for event caused by only 1 of them
(3)
(4)
P0
P1
wait(S);
wait(Q);
signal(S);
signal(Q);
wait(Q);
wait(S);
signal(Q);
signal(S);
Priority Inversion
Similar for P1
COMP 3511 Page 16
Priority Inversion
Similar for P1
Synchronization Problems
Sunday, October 23, 2016
4:20 PM
Readers-Writers
- Statement:
Bounded-Buffer
- Solution:
Semaphore
Init
Usage
mutex
haveData
haveEmptySlot n
Initialization:
// Ensure writer/reader access mutually excluded
// No separate access among readers
Semaphore rw_mutex = 1;
Producer
Consumer
do {
do {
{Produce item}
wait(haveData);
wait(mutex);
wait(haveEmptySlot);
wait(mutex);
{Add item to buffer}
signal(mutex);
signal(haveData);
} while (true)
Reader
do {
do {
wait(rw_mutex);
wait(mutex);
read_count++;
{Perform writing}
signal(rw_mutex);
} while (true)
Deadlocks
Wednesday, November 9, 2016
10:20 AM
Deadlock
3: Release resource
- Resource Allocation Graph:
Vertices:
Procs: P = {P1, P2, , Pn}
Resource types: R = {R1, R2, , Rm}
Edge:
Request edge: Pi Rj (Pi request 1 instance of R j)
Assignment edge: Rj Pi (1 instance of Rj assigned to Pi)
No cycle
Cycle
Cycle
No deadlock
Deadlock
No deadlock
Deadlock Prevention
Deadlock Recovery
- Terminate proc:
Abort all deadlock proc:
Not desirable
Abort proc 1 by 1, until deadlock cycle eliminated
Consideration overhead (must run detection algorithm every time 1 proc
aborted)
- Preempt resource:
Successively preempt resources from proc & give them to other procs until
deadlock cycle broken
Possibly starvation (same proc always picked as victim)
Deadlock Avoidance
Friday, December 16, 2016
11:58 AM
Deadlock avoidance
Safe State
R 1 P1
- When Pi release Rj, assignment edge reconverted to claim edge
P2
P3
Banker's Algorithm
- Assumption:
- Implication:
If Pi's resource needs not immediately available: Wait until
all/some Pj (j < i) finish
When proc get all resources, it must return them in finite time amount
- Data structure:
n = No of procs
m = No of resource types
available[1 m]:
available[j] = k: Rj has k avail instances
10
P2
No deadlock
Max[1 n, 1 m]:
Max[i, j] = k: Pi may request at most k instances of Rj
Allocation[1 n, 1 m]:
Finish[i] = false, i = 1, 2, , n
Loop until Finish[i] = True i:
Find i:
Finish[i] = false
Need[i] Work
If i: Sys not in safe state, terminate
Add Pi to sequence
Work Work + Allocation[i]
Finish[i] True
Finish[i] True
- Resource-request algorithm:
Given Pi, Rj
Request[i, 1 m]: Pi suddenly wants k more instance of Rj (in addition to
Allocation[i])
If unsafe:
Pi must wait
Restore Avail, Allocation[i], Need[i]
Deadlock Detection
Friday, December 16, 2016
12:25 PM
- Data structure:
Available[1 m]:
Available[i] = k: Ri still has k avail instance
Allocation[1 n, 1 m]:
- Wait-for graph:
Vertices: Procs
Request[1 n, 1 m]:
- Detection algorithm:
1: Initialize
Work Available
2: Find i:
Finish[i] = False
Request[i] Work
If i: Go to step 4
- Periodically search for cycle in graph
Cycle Deadlock
Go to step 2
- Complexity: O(n2)
Memory Management
Thursday, December 15, 2016
9:11 PM
- Addr protection:
Addr space of proc/user defined by base & limit register
Overview
- Prog must brought into mem & placed within proc to run
Done by long-term scheduler
CPU checks every mem access generated in user mode, ensure it within base for
that proc/user
Implementation: Limit register: Contain range of logical addr
- Dynamic relocation:
Routines kept on disk in relocated format & not loaded to mem until called
Utilize mem space
Unused routine never loaded
Large code portion handling infrequently occurring cases rarely loaded
Implementation:
Prog designed to support dynamic loading
- Compile time:
Compiler translates
symbolic relative addr
Same logical & physical
addr
- Loading time:
Loader translate relative
absolute addr
Same logical & physical
addr
- Exec time:
Memory Management
Unit translates absolute
physical addr
Contiguous Allocation
Swapping
- Allocation strategy:
- Temporarily swap proc from mem to backing store, then
brought back into mem to continue exec
Support: Physical mem space < Logical mem space
Degree of multiprogramming
- Ready-queue: Manage ready-to-run proc having mem images
on disk
First fit
Best fit
Worst fit
Fragmentation
- External fragmentation: Mem space exist to satisfy request, but not contiguous
Segmentation
- Internal fragmentation:
Allocated mem slightly larger than requested mem
However, remaining mem is inside allocated partition, can't used by other procs
- Solution
Compaction:
Shuffle mem content to place all free mem together in 1 large block
Possible only if addr relocation is dynamic, done at exec time
Can be expensive
Segmentation
Paging
Paging
Thursday, December 15, 2016
10:24 PM
Page Table
- Register:
Page-table base register (PTBR): Page table location in mem
Page-table length register (PTLR): Indicate page table's no of entries
Overview
- Effective Access Time (EAT): Avg time needed to access physical mem addr
EAT = (TLB Search Time + Mem Access Time)
+ (1 - ) (TLB Search Time + 2 Mem Access Time)
: Hit ratio: Prob of finding page no in TLB
Page size = 2n
Logical space size = 2m
No of page table = 2m - n
Hierarchical Paging
Memory Protection
- Protection bit:
Associated with each frame
Indicate read/write access
- Valid-invalid bit
Associated with each page table entry
Meaning:
Valid: Page in proc's logical addr space Legal page
Invalid: Page not in proc's logical addr space
- Motivation: Dont always need all logical addr space Don't need to create
intermediate-lvl page table at same time
- Intel IA-32:
Page Size
- Intel x86-64:
Only implement 48 bit addressing
4 lvl paging hierarchy
- ARM:
1 lvl paging for section
2 lvl paging for smaller pages
2 lvl of TLBs:
Outer lvl: 2 micro TLB (1 data + 1 instruction)
Inner: 1 TLB
11:13 PM
Demand Paging
- Lazy swapper principle: Page only brought from disk to mem when needed
No unnecessary I/O
Prepaging
(1 - p) Memory Access
+ p Avg Page-Fault Service Time
s pages prepaged
of these pages used (0 < < 1)
Cost of saving page fault s
Cost of prepaging unnecessary pages s (1 - )
0: Prepaging lose
Page Replacement
Thursday, December 15, 2016
12:01 PM
Algorithm
Usage
FIFO (First-infirst-out)
Illustration
OPT (Optimal)
Counting
LFU (Least Frequently Used) Algorithm: Replace page with min count
MFU (Most Frequently Used) Algorithm: Replace page with max count
Thrashing
Frame Allocation
Thursday, December 15, 2016
3:10 PM
- Approach:
Equal allocation: Each proc get equal no of frame
100 frame, 5 proc
Each proc get 20
- Reason:
Proc migrates between localities
S = si
m = Total no of frames
Working-Set Model
Local
10000 instruction
- WWSi (Working set of proc i) = No of pages referenced in most recent
too small: Not encompass entire locality
Page-Fault Frequency
= 10000
Timer interrupt after every 5000 units
2 ref bit for each page: x1, x2
Page referenced: Set x1 = 1
Whenever timer interrupt, copy all x1's to x2's, set all x1's to 0
If x1 = 0 or x2 = 0: Page in working set
File System
Friday, December 16, 2016
- Sequential access:
Read next
File Overview
Write next
Reset
No read after last write (rewrite)
- Attribute:
Name, Type, Size
Time
User identification
Identifier: Unique tag (number) to identify files within file sys
- Structure:
None: Just byte/word sequence
Simple record
- Operation:
Create/Read/Write/Delete/Truncate
Write n
Position to n
Read next
Write next
Rewrite n
File Sharing
UNIX
Disk structure
chgrp G game
- Disk divided into partition, each partition can protected against
failure using RAID
Directory Structure
Illustration
Pros
Cons
Single-lvl
- Single directory for all users - Naming problem: Different user can't
create directory with same name
- Very simple
- Grouping problem: No subdirectory
Two-lvl
Tree-Structured
- Efficient search
- Group capability
Acyclic-Graph
- Limited-grouping capability
Acyclic-Graph
General Graph
Allow cycle
8:38 PM
- In-mem structure:
Mount table: Info about mounted volumes
Role of Disk
UFS features
NTFS features
Boot
control
block
Per volume
Partition boot
sector
Volume
control
block
Per volume
- Total no of blocks
Stored in master
file table
Superblock
- No of free blocks
- Block size
- Pointers
- Free FCBs count & pointers
Layer
Roles
(high low)
App prog
Logical file sys - Metadata: All file sys structure except file content
- File Control Block (FCB): Info about file (ownership,
permission, file content location on disk)
File organization
- File name
File
control
block
File details
- inode no
Per file
Stored in master
- Associate inode file table
(FCB) no
- Permission
- Size
- Date
File
organization
module
- Open file:
Device
If file already in use (by other proc): Per-proc open-file table entry
created, point to existing system-wide open-file table
Otherwise: Search directory structure:
File found: Copy FCB into sys-wide open-file table in mem
- Read file:
Directory Management
- Structure management:
Linear list
Hash table
Stored in master
file table using
relation database
(1 row/file)
Simple to implement
Method
Description
Pros
Cons
Contiguous
- File size:
Difficult to grow
Must known at time of file creation
Overestimated Internal fragmentation
- Finding free space for new file:
Time consuming
Common strategies: Best fit, first fit (more
efficient than worst fit)
Linked
- No external fragmentation
Indexed
- Index block:
- No external fragmentation (Pointer can point Wasted space: Indexed's pointer overhead >
to any free disk block)
Linked's overhead
- Support direct access
Method
Description
Bit vector
Pros
Cons
- Simple
Linked-list
Solid-State Disk
Magnetic Tape
1:49 AM
Magnetic Disk
- Structure:
- Relatively permanent
- Can hold large quantities of data
- Mainly used for backup, storage of
infrequently-used data
- Very slow access time ( 1000 slower
than disk)
Disk structure
- Disk drive
Large 1D arrays of logical blocks
Sectors, sequentially
(sector 0: 1st sector of 1st track on outermost cylinder)
- SCSI:
- Metrics:
+ Controller overhead
=9.27 ms
- Head crash: Result from disk head making contact with disk structure
Disk Scheduling
200 cylinder 0 199
Disk Management
Disk Scheduling
200 cylinder 0 199
Queue = 98, 183, 37, 122, 14, 124, 65, 67
Disk Management
Head starts at 53
- Low-lvl (Physical) format:
Algorithm Explanation
Illustration
FCFS
(First
come first
serve)
Total distance = (183 - 53) + (183 - 37) + (122 - 37) + (122 14) + (124 - 14) + (124 - 65) + (67 - 65) = 640
Help recover from disk failure, but not prevent/detect data corruption
Techniques:
Parity: Re-generate lost content from parity-saved info elsewhere
Stripe: Store data randomly to multiple disk (no full data in single disk)
Mirror: Store copies of data on multiple disk
- Issues:
Starvation
Hot-spare disk: Disk left unallocated, automatically replace failed disk & have data
rebuilt onto them
Calculation
overhead
RAID lvls:
Head movement: 53, 65, 67, 37, 14, 98, 122, 124, 183
Total distance
Lvl
0
Illustration
Properties
- Blocks striped
Excellent
performance
- No mirror, No parity
No redundancy
- Blocks mirrored
Excellent redundancy
- No striping, no parity
Good performance
Head movement: 53, 37, 14, 0, 65, 67, 98, 122, 124, 183
Total distance = (53 - 0) + (183 - 0) = 236
LOOK
- Similar to SCAN
Head movement: 53, 65, 67, 98, 122, 124, 183, 37, 14
- Except head only goes as Total distance = (183 - 53) + (183 - 14) = 299
far as last request in
each direction, then
reverses immediately
C-SCAN
C-LOOK
- 2 data disks
- 1 parity disk
Assume head starts from 0
Head movement: 53, 65, 67, 98, 122, 124, 183, 199, 0, 14, 37
Total distance = (199 - 53) + (199 - 0) + (37 - 0) = 382
- Similar to LOOK
- 1 parity disk
- Block striped
Good performance
- Distributed parity
Good redundancy
- Block striped
Good performance
- Distributed parity
Good redundancy
- Block striped
- 2 distributed parities (2
parity block for each data
block)
0+1
Mirror of stripes
1+0
Stripe of mirrors
- ZFS: