- Definition of OS: a special layer of software that provides application software access to hardware resources
Four Fundamental OS Concepts
- Thread - concurrency
- execution context
- fully describes program state
- Program counter, registers, execution flags, stack, memory state
- Address space
- memory addresses accessible to program
- Process - protection
- An instance of executing program
- Protected address space + 1 or more threads
- owns address space
- owns file descriptors, file system context
- encapsulate 1 or more threads using shared process resources
- Easy communications within a process; hard between processes
- multiple threads in a process? 1) hardware parallelism; 2) ease of handling I/O and other simultaneous events
- Reliability - crashes process only; security and privacy - protected memory; fairness - shares of disk/CPU
- Dual mode operation / protection
- Only the OS has access to certain resources
- Combined with translation, isolates program from each other and OS from programs
- Hardware provides two modes: 1) Kernel Mode; 2) User Mode
- User mode cannot: changing page table pointer, disable disruptions, write to kernel memory, interact directly with hardware
- Change between modes: system calls, interrupts, exceptions
- User -> Kernel mode methods
- syscall
- interrupt - hardware
- exceptions - software
Motivation for threads
- Performance
- User responsiveness
- Hide latency
Thread state
-
Shared state:
- Global variables, heap
- I/O state: file descriptors, network connections
-
Private state:
- Kept in TCB (Thread control block)
- CPU registers: Program counter, etc
- execution stack: parameters, temporary variables
-
Thus, in an address space:
- 1 heap, global data, code
- Multiple stacks, sets of CPU registers
-
Socket: An abstraction of an endpoint in a network connection
- Scheduling policies provides:
- fairness or
- realtime guarantees or
- latency optimization or
(no parallelism) | Processes | Threads |
---|---|---|
Switch overhead | high | low |
Protection | high | low |
Shared resources overhead | high | low |
Lock using interrupts:
- Disable interrupts in lock acquire and release
- Interrupts enabled after asleep (responsibility of next running thread)
Lock using test&set:
Scheduling Goals
- Fairness
- Share CPU among users in an equitable way
- Minimize response time
- time spent to respond
- Maximize throughput
- operations or jobs per second
Scheduling Policies
-
FIFO
- Bad for small tasks, long response time
-
Round robin
- Context switch overhead adds up for long tasks
-
Strict Priority scheduling
- always execute highest-priority runnable jobs to completion
- Priority inversion: a low-level job holds a lock that a high-level job acquires
-
Tradeoff: fairness and avg response time
-
Shortest Remaining Job first
- run the shortest remaining job available
- Optimal avg response time
- unfair
- unachievable since needs to predict future
-
Lottery scheduling
- Each job gets several tickets, lottery for who goes next
- small jobs get more tickets, large jobs get fewer
-
Multi-level Feedback scheduling
- multiple priority queues, each with a scheduling policy
- Jobs start at the highest priority queue
- if times out, move down a priority; if not, move up a priority (or to the top)
- Policies between queues: priority or time slice
Real-time Scheduling
- Earliest deadline first (EDF)
- schedules the task with earliest deadline
Linux Completely Fair Scheduler
- Fair fraction of CPU
- Track CPU time per thread and schedule threads to match up average rate of execution
- Use weight to achieve different runtime
Requirements for deadlock
- Mutual exclusion
- hold and wait
- no preemption
- circular wait
Deadlock Solutions
- Deadlock prevention
- prevent it from happening
- allocate all at once
- allocate in a certain order
- Deadlock recovery
- let it happen, recover from it
- terminate thread
- preempt resource from thread
- roll back what deadlock threads have done
- Deadlock avoidance
- dynamically delay resource request so deadlock won't happen
- OS checks if it can result in unsafe state (i.e. deadlock may happen)
- Deadlock denial
- ignore deadlock
- Virtual memory
- isolation and protection
- Memory Management Unit (MMU):
- translate from virtual memory to physical memory
- Multi-level Paging
- Pros:
- only allocate as many page table entries as we need
- easy translation
- easy sharing
- cons:
- one pointer per page
- page tables need to be contiguous
- two lookups per reference
- Pros:
- A process cannot modify its own page table translations
- TLB: translation look-aside buffer: store virtual page to physical frame translation
- Sources of cache misses:
- compulsory: first time access
- capacity: full so evict
- conflict: multiple memory addresses to the same cache
- coherence: write to other cache
- Programmed I/O
- Each byte transferred via processor in/out or load/store
- simple
- consumes CPU cycles
- Direct Memory Access
- Give controller access to memory bus
- no CPU cycles needed
- Notify OS using
- I/O interrupt
- OS polling
- Four components of a file system
- Directory
- A special file
- A list of <file_name:file_number> mappings
- Index structure
- Storage blocks
- Free space map
- Directory
- Buffer cache
- OS allocated cache for faster file system operations
- Important characteristics
- Availability: up and running
- durability: recover from crashes
- Reliability: up and running under stated conditions
Reliability Approaches
- careful ordering and recovery
- versioning and copy-on-write
CAP Theorem
- Consistency
- Availability
- Partition-tolerance