time between issuing a command and getting a result
Back
Zombie processes
Front
-process has terminated
-parent process has not collected status
-dead, but not gone
Back
Waiting time
Front
amount of time a process has been waiting in the ready queue
Back
Evaluating Scheduling Policies
Front
-CPU utilization
-throughput
-turnaround time-how long process takes to run, from init to term, inc wait time
-response time
-waiting time
Back
Operating System
Front
Software that manages a computer's resources
-Easier to write desired applications
-Runs applications efficiently
Back
Evaluating an OS
Front
1) Reliability
-does exactly what it is designed to do
2) Security
- OS cannot be compromised by hackers
- strong fault isolation helps but isn't enough
3) Portability
-OS doesn't change when hardware does
-3 interfaces
-can last a long time
Back
Interactive Timesharing (1970-)
Front
-cheap terminals to let multiple users use the system at the same time
-requires more sharing, protection and concurrency
-New services: shell, file system, rapid process switching ,etc
Problem: response time, thrashing
Back
What is a process?
Front
A program during execution
(process = executing program = program + execution state)
-basic unit of execution in an OS
-at minimum, process execution requires: memory to contain program code and data, and set of CPU registers to support execution
Back
Saving the State of an Interrupted Process
Front
Privileged register points to exeception stack
-on switch, pushes interrupted process registers on exception stack BEFORE handler
-handler pushes the rest
-when returned, reverse
Don't use user-level stack because of reliability and security
One exception state per processor/process/thread
Back
Booting an OS Kernel
Front
CPU loads boot program from ROM
Boot program:
-examines/checks machine config (# CPU's, how much mem, # and type of hardware devices)
-builds a config structure describing the hardware
-loads OS kernel and gives it to config structure
-initialize kernel data structures and state of all hardware devices
-create number of processes to start operation
then runs an idle loop if no user programs are avaliable
-performs system management and profiling
-halts processor and enters low power mode
OS wakes up on interrupts from hardware devices
Back
Why study OS?
Front
-show how computers work
-learn to manage complexity with abstractions
-learn about system design
Back
Dual Mode Execution
Front
User vs Kernel Modes
Back
Transitioning from User to Kernel Mode
Front
Exceptions (synchronous)
-user program acts weird (divides by zero)
-attempt to perform privileged instructions
Interrupts (asynchronous)
-something interrupts the currently running process
System calls/traps (synchronous)
-user requests OS service (ex. function call)
Back
Process Control
Front
OS must include calls to enable special control of a process
-priority manipulation: nice(), specifying base process priority
-Debugging support: ptrace(), allows process to be put under control of another
-Alarms and times: sleep puts a process on a timer queue waiting for some number of seconds
Back
Fork()
Front
fork(): copies processes into an identical process
-copies var values and PC from children to parent
-returns twice: once to the parent and to child
-return values (parent = child PID and child = 0)
-both processes begin execution from same point after fork
-each process has its own memory and copy of each var
Back
CPU Utilization
Front
percentage of time that the CPU is busy
Back
Switching back to User Mode
Front
-From and interrupt --> just reverse all steps (asynchronous, so not related to executing instruction)
-From exception and system call-->increment PC on return (synchronous)
--on exception: handler changes PC at the based of the stack
--on sys call: increment is done by the hardware
Back
How does the OS enforce restricted rights?
Front
-OS interprets each instruction
-Dual Mode Execution
Back
Short-term scheduling
Front
The decision as to which available process will be executed by the processor.
Scheduler schedules next process on CPU
Scheduler may execute when:
-a process switches from running to blocking
-a process is create or terminated
-an interrupt occurs
Back
Exec
Front
-overlays process w/ new program (PID doesnt change)
-code, stack, heap overwritten
-new program begins at main
Back
Process States
Front
Process state has at least:
-content of the address space (code for running program, static data running, the stack, heap)
-registers and contents (heap pointer, program counter for next instruction, stack pointer)
-set of OS resources in use (open files)
-process identifier (PID)
-process execution state (ready, running, etc)
Back
Wait() system call
Front
causes the parent process to wait for the child process to terminate
-allows parent process to get return val from child
-puts parents to sleep waiting for child's results
-when a child calls exit(), the OS unblocks the parent and returns value passed by exit() as a result of the wait call
-if no children alive, returns immediately
-if zombies are waiting for parents, wait() returns one of the values immediately (and de allocates zombies)
Back
Throughput
Front
The number of processes that are completed per time unit
Back
Overlap IO and Computation Interrupts
Front
-No sharing, only protect OS from apps
-add concurrency within the SAME PROCESS
-OS requests IO, goes back to computation, gets interrupt when IO device finishes
Back
Terminating a process: kill()
Front
A parent can terminate a child using kill()
-kill also used for interprocess communication
-sends a signal to a specified process (by PID)
-receiving a process can define signal handlers to handle signals in a particular way
-if handler for that signal does not exist, default action is taken
Back
Three Interfaces
Front
1) Abstract Machine Interface (AMI)
-between OS and apps: API, memory access model, legally executable instructions
2) Application Programming Interface (API)
-Function calls provided to apps
3)Hardware Abstraction Layer (HAL)
-Abstract hardware internally to OS
Back
Batch Processing (1955-1965)
Front
-load, run, output to tape, print, repeat-users give punch cards to humans who schedules in batches Advantage: next job loaded right after previous finishes Disadvantages: no protection and computer can idle during IO
Back
How does the OS track this data?
Front
OS uses a Process Control Block (PCB)
- dynamic kernel data structure kept in mem
-represents execution state and location of each process when not executing
PCB contains:
-processor identification number, PC, stack pointer, general purpose registers, memory management info, etc
PCB's initialized when a process is created and deleted when a process terminates
Back
Dual Mode Execution: Protection Pieces
Front
For efficient protection, the hardware must support these 3 features:
1) Privileged Instruction
--prevents user processes from halting the machine or something
--implementation: mode status bit in process status register
2) Timer Interrupts
--prevents process from gaining control of the CPU and never releasing it
--implementation: hardware timer can be set to expire after a delay (pass back to kernel)
3) Memory protection
--prevents unauthorized access of data
Back
If applications had free rein...
Front
buggy or malicious applications could
-crash others
-violate other's privacy
-hog resources
-change or crash OS
Back
Phase 3 - OS History (1)
Front
Personal computing
-computers are cheap so everyone has one
-simplify OS by elimination of multiprogramming, concurrency and protection
Back
Multiprogramming
Front
-several program run at the same time sharing the machine
-one job runs until it performs IO, then another job gets the CPU
-OS manages interactions between concurrent programs
Requires memory protection and relocation
Back
User Mode to Kernel Mode: Details
Front
OS states state of user prog
-hardware identifies why boundary is crossed
-hardware gets entry from interrupt vector and appropriate handler is evoked
Back
Orphaned Processes
Front
-when a parent terminates before child
-in UNIX, the init process automatically becomes new parent, other instances kill all children when parent dies
-child can orphan itself to cont. running in the bg
Back
System Calls: OS Job
Front
-Hardware calls OS at sys-call handler (specified location)
-OS identifies service and executes it
-sets registers to contain result
-execute RTI instruction to return to user prog
User prog receives result and continues
Back
User Mode vs Kernel Mode
Front
User processes cant:
-address I/O directly
-use instructions to manipulate OS memory
-set mode bits from user to kernel
-disable/enable interrupts
-halt machine
Kernel mode can do all of these
Back
Parallel and Distributed Computing
Front
-multiple processors in the same machine, sharing memory and I/O devices
-multiple processes communicate via network
-Advantages: increase performance, increased reliability, sharing of specialized resources
Back
OS 3 Hats
Front
1) Referee
-manages shared resources
-isolation (protects processes from one another)
-communication (all processes work together)
2) Illusionist
-illusion of unlimited resources
3) Glue
- provides a standard interface to the hardware
-ex: file sys, VM, networking, etc
Back
Scheduling Policies: 2 Big Categories
Front
Non-premptive
Preemptive
Back
Preemptive
Front
scheduler runs on interrupts, mostly timers, but also system call and other hardware device interrupts
-executing process might be pre-empted from the CPU
-Most OSes today use this
Back
System Calls
Front
-request by user-level function to call function in kernel
-interface between application and OS (API)
-Parameters passed according to calling convention
Back
Timesharing
Front
A timer interrupt is used to multiplex CPU among jobs
Back
History of OS
Front
1) $$$ hardware, $ humans
2)$ hardware, $$$ human
3) $ hardware, $$$$ human
Back
Turnaround Time
Front
The CPU scheduling metric that measures the elapsed time between a process's arrival in the ready state and its ultimate completion (initialization to termination)
Back
Terminating a process: exit()
Front
After the program finished execution, it calls exit()
-takes the result of process as an argument
-closes all open files/connection/etc
-deallocates memory and most of the OS structures supporting the process
-checks if parent is alive (if so, hold the ret val until parent requests it, if not, it dellocates)
-process termination is garbage collection (resources reclamation)
Back
Performance of OS Evaluation
Front
Efficiency/Overhead, Fairness, Response time, throughput, predictability
Back
How to Create a Process
Front
One process can create other processes
-create processes are children and creator is parent process
In some systems, parents define/donate resources and privileges to its children
Parent can either wait for the child to complete or continue in parallel
Back
Process Life Cycle
Front
Processes are either running, ready to run or blocked waiting for an event to occur
And know how to draw it
Back
Non-Preemptive
Front
runs until exits kernel mode, blocks, or voluntarily yields CPU; Essentially free of race conditions in kernel mode;
an executing process is not ever pre-empted
Back
Phase 1 - OS History (1)
Front
One user & one process at a time (1945-1955)
-single user system
-Problem: low utilization of expensive components
Back
Section 2
(50 cards)
Interrupts and Context Switches
Front
interrupt state is part of the thread state
-when a thread blocks, its interrupt state is saved with the rest of it's context
-when a process continues running, its interrupt state is restored with the rest of its context
Back
Locks (more formal)
Front
provide mutual exclusion to shared data with 2 atomic routines
Lock acquire: wait until lock is free, then grab it
Lock release: unlock and wake up ant thread waiting in acquire
locks have 2 states: busy and free (*initially free)
Back
Critical Section
Front
a piece of code that only one thread can execute at a time
Back
Thread Life Cycle
Front
new process > WAITING > RUNNING > BLOCKED
- waiting starts and runs or times out
- runs unless blocked until unblocked or until complete
new, ready, running, blocking, terminated (just like processes)
Back
Round Robin Scheduling
Front
A PREEMPTIVE scheduling designed for Time Sharing Systems
The Ready Queue is treated as a circular queue
A small execution time interval is defined as the Time Quantum, or TIME SLICE
Interrupts in Round Robin
When the executing interval of a process reaches the time quantum, a SYSTEM TIMER will cause the OS to interrupt the process
The OS carries out a CONTEXT SWITCH to the next selected process from the ready queue.
Length of the Time Quantum
Time quantum that is too short will generate many context switching and results in lower CPU efficiency.
Time quantum too long may result in poor performance time.
Back
Metadata Structures
Front
-Process Control Block (PCB) contains process-specific info
(owner, PID, heap pointer, priority, active thread, and pointers to thread info)
-Thread Control-Block (TCB) contains thread-specific info
(stack pointer, PC, thread state, register vals, a pointer to PCB)
Back
Binary Semaphore
Front
-same as lock
-mutually exclusive access to a resource
has 2 values: 0 or 1 (busy or free)
-initial value is always free
Back
User-Level Threads: Context Switches
Front
Similar to processes and kernel-level threads:
-thread is running
-thread is interrupted by a signal or voluntarily yields
-library code saves thread state (to TCB)
-Library code chooses new thread to run
-library code loads its state (from TCB)
-thread is running
Back
Race Conditions
Front
Instances where the result changes based on scheduling
Back
Multilevel Feedback Queue
Front
-multiple queues with different priorites
-OS uses round robin scheduling at each priority level, running jobs in the highest priority queue first
-once those finish, OS runs jobs out of the new highest queue
-round robin time slice increases exponentially at lower priorities
Back
Liveness (critical section and correctness)
Front
if there is no thread in the critical section, there must be one waiting that a thread must be guaranteed to eventually enter
Back
First-Come-First-Served (FCFS)
Front
A priority-based scheduling method in which the scheduler always dispatches the ready thread that has been waiting the longest.
job runs until either complete or block on I/O
Back
Types of Semaphores
Front
Binary semaphores and counted semaphores
only difference is the initial value!!
Back
Multi-threaded processes with kernel-level threads
Front
-threads created by OS (so knows about them)
-thread management through sys calls
-TCBs and PCBs in-kernel ready list
-scheduled by OS
Back
Ideal Synchronization
Front
-satisfies correctness properties (safety, liveness and bounded waiting)
-no busy waiting (thread should block when waiting and then be awakened when it is their turn)
-extendable to many threads (symmetric)
Back
Processes Review
Front
-A process is the abstraction used by the OS to manage resources and provide protection
-defines an address space
-has a single thread of control that executes instructions sequentially
Back
Kernel-Level Threads
Front
-the OS is aware that they exist
-scheduled and managed by the kernel
-small context switch req: save thread state, mode switches
(when switching between kernel level threads of the same process)
Back
Mutual Exclusion
Front
The requirement that when one process (thread) is in a critical section that accesses shared resources, no other process (or thread) may be in a critical section that accesses any of those shared resources.
Two threads adding to linked list can corrupt the list
Back
Locks
Front
allows one thread to prevent another thread from doing something
-lock before entering a critical section or before accessing shared data
-unlock when leaving critical section or when access to shared data is complete
-wait if locked
Back
Test&set
Front
the hardware solution to mutual exclusion that reads the contents of memory into a register then stores a nonzero value in the memory location as an indivisible instruction
-reads a value from memory
-writes '1' back to the memory location
Back
Independent vs Cooperating Threads
Front
Independent have no shared state with other threads
-simple to implement
-deterministic
-reproducible
-scheduling order doesn't matter
Cooperating threads share state
-non-deterministic
-non-reproductive
-give concurrency
Back
When to use Mutual Exclusion
Front
Anytime you access shared data
-if a thread check a values
-if a thread updates a piece of shared data
Back
User-Level Threads
Front
-OS does not know about
-OS only schedules the process NOT the threads within the process
-Programmer uses a thread library to manage threads (create, delete, synchronize, and schedule)
-switching threads does not involve a context switch
Back
Using the past to predict the future
Front
policy is adaptive because it relies on past behavior and changes in behavior result in changes to scheduling decisions
Back
Atomic Read-Modify-Write Instructions
Front
atomically read a value from memory into a register and write a new value
-read a mem location into register and write a new val to that location
-uniprocessor just needs a new instruction
Back
SJF (Shortest Job First)
Front
Shortest jobs get highest priority in waiting queue. Shortest average wait time
I/O bound job get priority over CPU bound jobs
-Works with preemptive and non-preemptive
Back
Atomic Operation
Front
an operation that is uninterruptible
Back
Synchronization
Front
using atomic operations to ensure cooperation between threads
Back
Real CPU Scheduler's Policy
Front
-minimize response time
-minimize variance of average response time (predictability)
-maximize throughput
-minimize waiting time
Back
Single Threaded Processes
Front
-what we have been assuming
-each has exactly one kernel-level thread
-add protection
Back
Threads and Address Space
Front
Threads share an address space
-all process data can be accessed by any thread (global data and heap also shared)
Each thread has it's own:
-stack (but with no protection from other threads)
-exclusive use of CPU registers while it is executing (when pre-empted, register vals are saved as part of the state)
Back
Multi-threaded processes with user-level threads
Front
-threads are created in user space
-have exactly one kernel-level thread
-thread management through procedure calls
-scheduled by user-space scheduler
-TCB's in user-space ready list
Back
Critical Section and Correctness
Front
For properties are required:
1) safety
2) liveness
3) bounded waiting
4) failure atomicity
Back
ideal CPU scheduler
Front
-maximizes CPU utilization and throughput
-minimizes turnaround time, waiting time and response time
-requires knowledge of process behavior we typically don't have
Back
One Abstraction, Many Flavors
Front
-Single-threaded processes
-Multithreaded processes with user-level threads
-Multi-threaded processes with kernel level threads
-In-kernel threads
Note: kernel level threads and user level threads are unrelated to kernel mode and user mode execution!!!!!!
Back
Semaphores
Front
give mutual exclusion and more general synchronization
-basically generalized locks
(2 atomic operations: up and down, has a value but is more than just busy/free)
Back
Improving Fairness
Front
Possible solutions:
-give each queue a fraction of CPU time. Only fair if there is an even distribution of jobs among queues
-Adjust the priority of the jobs as they do not get serviced. (Avoids starvation but average wait time suffers when the system is overloaded because all jobs end up at a high priority)
Back
How MLFQ's work
Front
1) Job starts in the highest priority queue
2)if job's time slice expires, drop priority a level
3)if job's time slice does not expire (context switch comes from I/O instead), increase its priority one level
In practice, CPU bounds drop and I/O jobs stay at a high priority
Back
Kernel-Level Threads: Context Switch
Front
Similar to processes:
-thread is running
thread blocks, is interrupted or voluntarily yields
-mode switch to kernel mode
-OS code saves thread state (to TCB)
-OS code chooses new thread to run
-OS code loads its state (from tCB)
-mode switch to user mode
-thread is running
Back
In-kernel threads
Front
-threads that are part of the OS (init, idle)
Back
Why Use Threads?
Front
-better represent the structure of tasks
-improve performance
(one thread can perform computation while the other waits for I/O)
Back
Safety (for critical section and correctness)
Front
only one thread in the critical section at a time
Back
Safety and Liveness (more generally)
Front
properties define over the execution of a program
Safety: nothing bad happens
-holds in every finite execution prefix
Liveness: something good eventually happens
-no partial execution is irremediable
Back
Threads
Front
-an abstract entity that executes a sequence of instructions
-defines a single sequential execution streams within a process
-a thread is bound to a single process
-each process may have multiple threads of control but MUST have one
Back
Failure atomicity
Front
It's okay for a thread to die in the critical section
Back
Disabling interrupts
Front
Tells the hardware to delay handling any external events until after the thread is finished modifying the critical section
in some implementations, done by setting and unsetting the interrupt status bit
Back
Power of Kernel Threads
Front
-I/O: the OS can choose another thread in the same process when a thread does I/O
-Kernel-level threads can exploit parallelism
Back
Bounded Waiting
Front
if a thread wishes to enter a critical section, then there exists a bound on the number of other thread that may enter the critical section before the thread does
Back
Threads are Lightweight
Front
Creating a thread is cheaper than creating a process
-communication between threads is easier than between processes
-context switching between threads is cheaper (because they are in the same address space)
Back
Schedules/Interleavings
Front
-model of concurrent execution
-interleave statement from each thread into a single thread
-if any interleaving yields incorrect results, some synchronization is needed
Back
Section 3
(37 cards)
Resource Variables
Front
condition variables (unlike semaphores) keep no state
-each cond var should have a resource variable that tracks the state of the resource
-check resource var before calling wait on cond var
-before signaling, indicate that the resources is avaliable bu increasing the resource var
Back
Lock Ordering (Deadlock Prevention)
Front
-Order all locks
-all code grabs locks in a predefined order
Complications
-maintaining global order is difficult in a large project
-Global order can cause something to grab a lock earlier than needed, tying up lock for longer than it should
Back
No pre-emption
Front
a thread only releases a resource voluntarily, another thread or the OS cannot force the thread to release the resource
Back
Write-Ahead Log
Front
All changes are written to the log (special part of disk) prior to being written to their true locations
-changes MUST be idempotent (can be done over and over and still be the same)
Back
Deadlock
Front
when 2 or more threads are waiting for an even that can only be generated by these same threads
IS NOT starvation
-starvation can occur without deadlock but deadlock implies starvation
Back
Counted Semaphore
Front
-represents a resource with many units available
-initial count is typically the number of resources
-allows a thread to continue as long as more instances are available
-used for more general synchronization
Back
Transactions
Front
group of actions together so that they are:
-atomic: all happen or they all don't
-serializable: transactions appear to happen one after the other
-durable: once it happen, it sticks (ensures that all changes are written to disk)
Critical sections give us all but durability :(
Back
One Big Lock
Front
Advantage: Simple
-easy to get correct
Disadvantage: eliminates advantages due to multi-threading for that part of code (only 1 thread can be active at a time, could be an unnecessary restriction)
Back
Conservative Two-Phase Locking
Front
a thread must acquire all the locks it needs (or release and wait until all locks can be acquired)
-make necessary changes, commit, and release lock
-thread b cant see A's changes until A commits and releases the lock
-provides serializability and prevents deadlock
Back
When to Use Semaphores
Front
-Mutual exclusion
-control access to a pool of resources
-general synchronization (to enforce general scheduling restraints where a thread must wait for some reason, value is typically 0 to start, up and down are called by different thread)
Back
Semaphores: Up and Down
Front
Down(): decrements the value (actually P())
-when down returns, the thread has the resource
-can block: if resource is unavailable, thread will be placed in a wait queue and put to sleep
Up(): (actually V()), increments value
-never blocks
-if a thread is asleep in the wait queue, it will be awaken
Back
Achieving Durability
Front
need to be able to:
-commit: indicate when transaction is finished
-roll back: recover from an aborted transaction
-basically do a set of operations tentatively, if we make it to commit everything is ok, otherwise roll back as if operation never happened
Back
Monitors
Front
encapsulates shared data
-allows operations on shared data (define functions for accessing shared data)
-provide mutual exclusion (exactly one lock FIRST)
-allow threads to synchronize in critical section (guarantees against deadlock)
Back
Monitors vs Semaphores
Front
Monitors: user-code
Semaphores: kernel-code
Back
Locks define critical sections
Front
Back
Fine-Grained Locking
Front
Better for performance
-matters more in kernel
Complex
-may need to acquire multiple locks to accomplish a task
-deadlock is more likely in complex code
Back
Mutual Exclusion (deadlock)
Front
at least one thread must hold a resource in non-sharable mode
Back
Tradeoff and Problems: Difficult to Get Right
Front
-ensure safety, liveness, no race conditions, no starvation, no priority inversion, no deadlock
Back
Signal() Semantics
Front
if there are no waiting threads, the signaler continues and the signal is effectively lost
-if there is a waiting thread (or 2)
exactly one of these can execute or we will have more than one thread active in the monitor (but this violates mutual exclusion)
Back
Hoare Style
Front
thread that signals gives up the lock and the waiting thread gets the lock
-when the thread that was waiting and is now executing exit or waits again, it releases the lock back to the signaling thread
Back
Identifying Condition Variables
Front
may be able to manage with fewer condition variables
-ask, where should they wait for a resource?
Back
Condition Variable Operations
Front
1) Wait (Lock lock)
-atomic (release lock, move thread to waiting queue, suspend thread)
-when the thread wakes up it re-acquires the lock (before returning from wait)
-thread will ALWAYS block
2) Signal (Lock lock)
-wake up waiting thread, if one exist, otherwise no-op
3) Broadcast (Lock lock)
-wake up all waiting threads, if any exist, otherwise no-op
Back
Waiting using Condition Variables
Front
every time wait is called, you're about to release the lock
Back
Conditions for Deadlock
Front
1)Mutual Exclusion
2)Hold and wait
3)No Pre-emption
4)Circular Wait
Back
Signal Vs Broadcast
Front
it is always safe to use broadcast() instead of signal()
-signal() preferable when at most one waiting thread can make progress, any thread waiting on cond var can make progress
-broadcast() preferable when multiple threads may be able to make progress or the same cond var is used for multiple predicates
Back
Condition Variables
Front
enables threads to wait efficiently for changes to shared state protected by a lock
-each one is a queue of waiting threads (no state)
-enable thread to block inside the critical section by atomically releasing the lock at the same time the thread is blocked
A THREAD MUST hold the lock when doing cond var operations
Back
Hold and wait
Front
at least one thread hold a resource and is waiting for other resources to become available. A different thread is holding the resource
Back
Interrupt Vector
Front
figures out what code to execute because of an exception
-only happens in user-mode (context switch to kernel-mode)
Back
Mesa/Hansen Style
Front
The thread that signal keeps the lock (and thus the processor)
-waiting thread waits for lock
Back
Circular Wait
Front
a set of waiting threads where ti is waiting on ti+1 (i=1 to n) and tn is waiting on t1
Back
Mode Switch
Front
switch from running user-mode to kernel-mode
when the user requests privileged operations
Back
Monitors: Implementation
Front
Acquire lock at the start of every function
-temporarily release the lock if they can't complete due to missing resource (use condition variable for this)
-reacquire lock when they can continue (cond var)
Release lock at the end
Back
Managing Locks
Front
add a lock as a member var for reach object in the class to enforce mutual exclusion on the object's shared state
-acquire a lock at the start for each public method
-release lock at the end of each public method
Back
Monitors (Formal Definitions)
Front
One lock and zero or more condition variables for managing concurrent access to shared data
-lock ensures a single thread is active in a monitor at any point
-lock also give mutual exclusion for shared data
-condition variables enables threads to block waiting for an event inside the critical sections
Back
Deadlock Prevention
Front
Easiest to fix: Circular Wait
-impose an ordering on the locks and request them in order
Mutual Exclusion - make resources shareable
Hold and Wait - guarantee a thread cannot hold one resource and when it requests another (or must request all at once)
No Pre-emption: if a thread wants a resource it cannot immediately have, OS preempts all resources the thread is currently holding. When they are available, OS will restart the thread
Back
Context Switch
Front
Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process.
Has to happen in kernel-mode
Back
6 Rules for Monitors
Front
1) Always do things the same way
2) Always synchronize with locks and condition vars
3) Always acquire lock at the beginning and release at the end
4) Always hold lock when operating cond var
5) Always wait in a while loop (so semantics don't matter)
6) (Almost) never sleep() --> condition vars to block, not sleep