Академический Документы
Профессиональный Документы
Культура Документы
In a nutshell
Content
Motivation Race Condition Critical Section problem & Solutions Classical problems in Synchronization Deadlocks
Also can happen in bank account database transactions with, say a husband and a wife accessing the same account simultaneously from different ATMs
count-- implementation:
Let count = 5 initially. One possible concurrent execution of count++ and count-- is register1 = count {register1 = 5} register1 = register1 + 1 {register1 = 6} register2 = count {register2 = 5} register2 = register2 - 1 {register2 = 4} count = register1 {count = 6} count = register2 {count = 4}
count = 4 after count++ and count--, even though we started with count = 5 Easy question: what other values can count be from doing this incorrectly?
Obviously, we would like to have count++ execute, followed by count-- (or vice versa)
Critical Sections
A critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. The goal is to provide a mechanism by which only one instance of a critical section is executing for a particular shared resource. Unfortunately, it is often very difficult to detect critical section bugs
2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted
Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes
Deadlocks
das
Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.
Deadlocks
Deadlock: processes waiting indefinitely with no chance of making progress. Starvation: a process waits for a long time to make progress.
Deadlocks
Deadlock applications not just OS
Network
Two processes may be blocking a send message to the other process if they are both waiting for a message from the other process
Receive/waiting blocks writing
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P0, P1, , P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, , Pn1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.
Resource-Allocation Graph
A set of vertices V and a set of edges E.
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Basic Facts
If graph contains no cycles no deadlock. If graph contains a cycle
if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock.
Deadlock Prevention
Restrain the ways request can be made. Mutual Exclusion not required for sharable resources; must hold for nonsharable resources. Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources.
Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible.
Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
Deadlock Avoidance
Requires that the system has some additional a priori information available. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.