Process Synchronization
Process Synchronization
- Cooperating Processes
- Directly share space
- Share a file <-> DB
- Producer
Race Condition
-
A situation like this, where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition.
-
we want any changes that result from such activities not to interfere with one another. Because of the importance of this issue, we devote a major portion of this chapter to process synchronization and coordination among cooperating processes.
-
Critical section model
Critical Section Problem
- a section of code where the processes may be changing common data Critical Section
Difference between Concurrency and Parallelism
- Structue and implementation
This is just showing hardware logic.
circuit tree
This is hardware logic
Mutex Lock
- aquire()
- release()
Semaphore
- Simplest of synchronization tool
- int S
- wait(s) : {while(s<=0);//busy wait s–;}
- signal(s) : {s++;}
- wait and signal operations must be executed indivisibly.
- There are counting and binary semaphores.
- counting semaphore : can range over an unrestricted domain
- binary semaphore : can range only between 0 and 1
Non-busy wait semaphore
Deadlock
- A process requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes.
- Necessary conditions for deadlock
- Mutual Exclusion
- Hold & Wait : process holding resource while waiting
- No Preemption : holding your resource, another process can’t take it away
- Circular Wait : there is circle in between processes
- p0->p1->p2->p0
- Resource allocation graph
- P = process
- R = resource
- Pi -> Rj request edge
- Rj -> pi allocated edge
- Do we have a deadlock?
Methods for handling deadlocks
- we can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state.
- prevent: remove 1 or more of the necessary cmd for deadlock
- avoid : keep the system in a “safe” state
- we can allow the system to enter a deadlocked state,detect it, and recovoer.
- detection & recovery:
- we can ignore the problem altogether and pretend that deadlocks never occur in the system.
- Ignore: application developer has to handle dead lock(Unix,windows)
Starvation is not a deadlock
- like a chopstick in dining If you don’t get both, you are starving
Bankers Algorithm
Safety Algorithm
Resource Request Algorithm
Remember safe sequence term (ways of getting out)