10. (10) Two hydrogen atoms and one oxygen atom combine to make a water molecule
ID: 3596916 • Letter: 1
Question
10. (10) Two hydrogen atoms and one oxygen atom combine to make a water molecule. Write code to count the number of water molecules given the following scenario: each time a Hydrogen) atom (i.e., thread) arrives, it increments the count of hydrogen atoms and then wakes the Water) thread before blocking. The Oxygen) thread is similar. The Water) thread, when woken, checks whether there are sufficient atoms (at least 2 hydrogen atoms and 1 oxygen atom) to make a water molecule. If so, the Water() thread increments the count of water molecules and wakes 2 hydrogen threads and 1 oxygen thread. There is 1 Water thread, and several Hydrogen, Oxygen threads. The code for the Hydrogen() thread is given. Write the corresponding Oxygen) thread and the Water) thread. /count of waiting hydrogen / /count of waiting oxygen / int h, water; /* count of water molecules/ Initially h-o-water 0 semaphore lock, waitH, /waiting hydrogen / waito, / waiting oxygen / molecule; creates water molecule if sufficient atoms Initially waitH waito-molecule-0; lock=1 ; Hydrogen () P (lock)i V (molecule) V (lock)i P (waitH);Explanation / Answer
the oxygen thread is as follows :
oxygen()
// wait() operation is termed as P and signal() operation is termed as V
{
P(lock);
o++;
V(molecule);
V(lock);
P(waitO);
}
water()
{
P(lock);
if(h==2 && o==1)
{
molecule++;
}
V(lock);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.