Download presentation
Presentation is loading. Please wait.
Published byPreston Rogers Modified over 8 years ago
1
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
2
Without synchronization amongst processes (and threads), results are unpredictable how do variables x and y become corrupted?
3
Processes compete for resources Once obtained, the resource is fully dedicated to a process Often, mutual exclusion is required ▪ No other process is allowed access to the resource Processes cooperate with other processes Shared resources Specific ordering or sequencing of events all of this applies to threads, too!
4
A semaphore is a synchronization mechanism Semaphore S is a special integer variable OS provides two atomic operations on S: wait(S) or P(S): ▪ wait for a resource to become available signal(S) or V(S): ▪ signal that we’re done using a resource
5
The wait(S) operation decrements semaphore S only when S is available wait(S) { while ( S <= 0 ) { /** no-op **/ ; } S--; } this will block indefinitely in a busy wait
6
The signal(S) operation increments semaphore S to release a resource signal(S) { S++; } to protect critical section wait(S); // CRITICAL // SECTION signal(S);
7
A binary semaphore provides mutually exclusive access to a shared resource Initialize semaphore S to 1 Use wait(S) and signal(S) Possible values of S are 0 and 1
8
A counting semaphore controls access to a finite number of resources: e.g. open files, network connections, shared buffers, etc. // n instances of a finite resource semaphore S = n Write pseudocode for the producer-consumer problem using semaphores to synchronize access to the shared buffer of size N
9
A process faces starvation when it is forced to wait indefinitely for shared resource X as other processes use that shared resource X Also known as indefinite blocking
10
A system enters a deadlock state when multiple processes are unable to obtain a lock on all necessary resources After acquiring a resource, a process holds that resource indefinitely // P 0... wait(S) wait(Q)... signal(Q) signal(S)... // P 1... wait(Q) wait(S)... signal(S) signal(Q)... semaphore S, Q Deadlock!
11
Deadlock requires four conditions: Mutual exclusion Hold and wait No preemption Circular wait ▪ i.e. a cycle!
12
A resource allocation graph is a directed graph showing processes and resources
15
Rice Five philosophers at a table Each philosopher thinks or eats To eat, a philosopher must pick up the closest two chopsticks A philosopher may only pick up one chopstick at a time Represents allocating shared resources to competing and cooperating processes
16
Rice Potential solution: Can deadlock occur? // philosopher i while ( true ) { think(); wait( fork[i] ); wait( fork[(i+1)%5] ); eat(); signal( fork[(i+1)%5] ); signal( fork[i] ); } semaphore fork[] = { 1, 1, 1, 1, 1 };
17
Allow the system to enter a deadlock state, then recover by: Terminating one or all deadlocked processes Rollback deadlocked processes to a safe checkpointed state Or....
18
Guarantee that the system will never enter a deadlocked state Deadlock prevention ensures that at least one of the four necessary conditions is never met Deadlock avoidance allows a system to change state by allocating resource(s) only when it is certain deadlock will not occur as a result
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.