Download presentation
Presentation is loading. Please wait.
1
H2O
2
A chemical reaction is the pathway by which two substances bond together.
Chemical reactions happen all around us: when we light a match, start a car, eat dinner or walk the dog.
3
One reaction we are mentioning today is the reaction of hydrogen with oxygen to form water.
4
Problem Statement So lets Consider the problem of making water.
One oxygen atom and two hydrogen atoms are needed to be combined to make water.
5
Synchronization Now lets assume that atoms are processes, and that exactly two hydrogen and one oxygen process must call the procedure makeWater at once to create water.
6
Semaphores We used semaphores to provide a solution to this problem because semaphores is considered a common and efficient tool for synchronization. A semaphore is an object with 2 methods: 1) Wait: The counter of the process is positive: counter decreased by 1, thread resumes execution. The counter of the process is zero: thread is suspended and placed in a private queue.
7
2) Signal: The queue of the process has no waiting thread: counter increased by one. The queue of the process has waiting threads: counter must be 0.
8
Hydrogen Process: while (1) { wait(hydrogen); wait(mutex); waiting++;
if (waiting == 3) then signal(go); signal(go); signal(go); else signal(mutex); wait(go); // makeWater(); signal(hydrogen); }
9
Oxygen Process { wait(oxygen); wait(mutex); waiting++;
while (1) { wait(oxygen); wait(mutex); waiting++; if (waiting == 3) then signal(go); signal(go); signal(go); else signal(mutex); wait(go); // makeWater(); signal(oxygen); }
10
Deadlock In this program, things will quickly come to a halt as all processes start up and immediately start waiting on one of the two counters, which all start as zero. Every process is then waiting for an action to be taken by another process, and we will have a deadlock and starvation situation.
11
Solution We can eliminate this problem by changing the order for one of the elements. One of the processes has to make the first move. If hydrogen first signals hydrogen available and then waits on the oxygen counter, it will break the deadlock because raising the hydrogen count allows the oxygen process to get through acquiring hydrogen to signal the oxygen needed by the hydrogen process, which will allow both the elements to move on.
12
Solution continued… Initial Values semaphore mutex = 1
semaphore hydrogen = 2 semaphore oxygen = 1 semaphore go = 0 variable int waiting = 0
13
Hydrogen Process: while (1) { wait(hydrogen); initial value =2 value = 2-1= 1 wait(mutex); initial value = 1 value = 1-1 = 0 //One Hydrogen Atom waiting++; value = 1 if (waiting == 3) then signal(go); signal(go); signal(go); else signal(mutex); value of mutex = 0
14
Oxygen Process: while (1) { wait(oxygen); initial value = 1
wait(mutex); // One Oxygen atom waiting++; value = 2 if (waiting == 3) then signal(go); signal(go); signal(go); else signal(mutex); value = 0
15
Hydrogen Process: while (1) { wait(hydrogen); previous value = 1
new value = 1-1 = 0 wait(mutex); // 2 Hydrogen Atom waiting++; value = 3 if (waiting == 3) then // enters loop signal(go); signal(go); signal(go); else signal(mutex); go++; wait(go); // makeWater(); // H2O ready signal(hydrogen); }
16
Conclusion Semaphores is the best synchronization tool which is an efficient way to solve problems and provide a deadlock free algorithm.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.