OS Chapter 5: Concurrency: Mutual Exclusion and Synchronization
advantages of enforcing mutual exclusion with special machine instruction
1. Applicable to any number of processes (either single processor or multiple processors sharing main memory) 2. Simple, easy to verify 3. Can be used to support multiple critical sections
3 Control Problems with Competing Processes
1. Mutual exclusion (only one program allowed access to critical resource at once) 2. Deadlock 3. Starvation
6 Requirements for Mutual Exclusion
1. Mutual exclusion must be enforced 2. A process that halts in non critical section must do so without interfering with other processes. 3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely; no deadlock, no starvation allowed. 4. When no process currently in critical section, any process requesting access to critical section must be permitted to enter without delay. 5. No assumptions made about number of processors or relative process speeds. 6. Process remains inside its critical section for finite time only.
What design and management issues are raised by concurrency?
1. OS must track various processes (with process control blocks) 2. OS must allocate and deallocate various resources for each active process. Multiple processes may want same resources simultaneously 3. OS must protect data and physical resources of each process against unintended interference by other processes 4. Function and output of a process must be independent of the speed at which its execution is carried out relative to speed of other concurrent processes
3 Levels of Process Inter-awareness
1. Processes unaware of each other (multiprogramming of multiple independent processes) 2. Processes indirectly aware of each other (share access to some object (like an I/O buffer); exhibit cooperation in sharing the object) 3. Processes directly aware of each other (communicate with each other using process IDs, work jointly; exhibit cooperation)
Difficulties with multiprocessing AND multiprogramming
1. difficult to locate programming errors because results not deterministic or reproducible 2. difficult for OS to manage the allocation of resources optimally 3. sharing of global resources fraught with peril
Concurrency arises in 3 different contexts
1. multiple applications 2. structured applications 3. operating system structure
Central themes of OS design (3)
Multiprogramming Multiprocessing Distributed processing
Control of competition inevitably involves the ____ because it is the ____ that allocates resources
Operating System
2 solutions to readers/writers problem
Readers Have Priority (writers subject to starvation) Writers Have Priority
Dijkstra's treatise
Two or more processes can cooperate by means of simple signals (semaphores), such that a process can be forced to stop at a specified place until it has received a specific signal
Why do concurrency problems occur in multiprocessor systems?
Two processes may be executing simultaneously and both trying to access the same global variable
Can concurrency problems occur when there is a single processor?
Yes, an interrupt can stop instruction execution anywhere in a process.
Semaphore
an integer value used for signaling among processes; may be initialized to nonnegative int, decremented, or incremented; decrement may result in process blocking, increment may result in unblocking
semWait and semSignal primitives are assumed to be ____
atomic
Key terms related to concurrency
atomic operation critical section deadlock livelock mutual exclusion race condition starvation
disadvantages of enforcing mutual exclusion with special machine instruction
busy waiting is employed (wasted processor time) starvation possible (selection of waiting process arbitrary) Deadlock is possible
Processes unaware of each other, relationship?
competition
Fundamental to all OS themes is _____
concurrency
Processes directly aware of each other, relationship?
cooperation by communication
Processes indirectly aware of each other, relationship?
cooperation by sharing
Distributed processing
management of multiple processes executing on multiple, distributed computer systems
Multiprocessing
management of multiple processes within a multiprocessor
Multiprogramming
management of multiple processes within a uniprocessor system
Event flag
memory word used as synchronization mechanism; each bit represents event, threads check bits to check status of certain events; thread blocked until all required bits set (AND) or until one is set (OR)
Basic requirement for support of concurrent processes is ability to enforce _____
mutual exclusion
Mutex
mutual exclusion lock; similar to binary semaphore, but process that locks the MUTEX (sets it to 0) must be the one to unlock it (sets it to 1)
spinlock
mutual exclusion mechanism in which a process executes in an infinite loop waiting for value of a lock variable to indicate availability
Producer/Consumer problem
one or more producers generating some type of data and placing these in a buffer; a single consumer is taking items out of buffer one at a time; system is to be constrained to prevent overlap of buffer operations. (only one agent may access buffer at any one time); ensure that producer doesn't add data if buffer full, consumer doesn't take out if empty
semSignalB
operation that checks if any processes are blocked on the binary semaphore (if equal 0); if so, process blocked by semWaitB operation is now unblocked; if none blocked, value of semaphore set to 1
semWaitB
operation that checks the binary semaphore value; if 0, process executing semWaitB is blocked; if 1, value changed to 0 and process continues execution
semwait
operation that decrements semaphore value; if value becomes negative, process executing semWait is blocked; otherwise, continue execution
semSignal
operation that increments semaphore value; if resulting value less than or equal to 0, then process blocked by semWait operation, if any, is unblocked
In a uniprocessor system, concurrent processes cannot have ______ execution; they can only be _______
overlapped; interleaved
To guarantee mutual exclusion, it is sufficient to _____
prevent a process from being interrupted
In a multiprocessor architecture _______ does not guarantee mutual exclusion
preventing interruption
busy waiting/spin waiting
process can do nothing until it gets permission to enter its critical section but continues to execute an instruction (or set of them) that tests the appropriate variable to gain entrance
Monitor
programming language construct that encapsulates variables, access procedures and initialization code within an abstract data type
Basic problem in multiprogramming systems?
relative speed of execution of processes cannot be predicted
Binary semaphore
semaphore that only takes value 0 or 1
Readers/Writers Problem
there is a data area shared among a number of processes. The data area could be a file, block of main memory, or even a bank of processor registers. There are a number of processes that only read the data area (readers) and a number that only write to the data area (writers). The conditions that must be satisfied are: 1. any number of readers may simultaneously read 2. only one writer at a time may write 3. if a writer is writing to the file, no reader may read it
Race condition
when multiple processes or threads read and write data items so that the final result depends on the order of execution of instructions in the multiple processes; "loser" of the race to write determines the final value
With cooperation among processes by sharing, only _____ operations must be mutually exclusive
writing
How are processes removed from the queue of processes waiting on semaphore?
FIFO (if FIFO, semaphore called "strong semaphore"); guarantee freedom from starvation