please share the answer as soon as possible.. Suppose that you have a set of pro
ID: 666231 • Letter: P
Question
please share the answer as soon as possible..
Suppose that you have a set of processes that are controlling a set of valves and switches. Valve 1 lets chemical X into the mixing vat. Valve 2 lets chemical Y into the mixing vat. Switch 1 starts the agitator in the mixing vat. Valve 3 releases the contents of the mixing vat into the holding tanks. Process A is responsible for delivering the indicated amount of chemical X into the mixing vat. Process B is responsible for delivering the indicated amount of chemical Y into the mixing vat. Process C is responsible for agitating the chemical for the requisite amount of time. Process D is responsible for emptying the vat into the holding tanks. The activities of the processes must be controlled according to the following constraints: a. Chemical X must be put into the mixing vat before chemical Y b. both chemicals must be in the mixing vat before agitation begins, c. agitation must be complete prior to the emptying of the mixing vat into the holding tanks d. and, the mixing vat must be completely emptied before the next cycle of mixing can begin Complete the pseudo-code below using semaphores wait and signal operations to implement the processes according to the above constraints. Assume that valves are controlled with functions open_valve(i), close_valve(i) and switches are controlled by functions on_switch(i) and off_switch(i). To wait to be signaled, a process uses the wait() function on the appropriate semaphore. To signal another process, a process uses the signal() function with the appropriate semaphore. For example, wait(sem1) waits on semaphore sem1 and signal(sem1) signals semaphore sem1. The four semaphores must be intialized to 0 or 1 to work correctly. Initialize each semaphore to the appropriate value. semaphore waitForMixingVatReady = __________; // to let
Process A open and close valve 1 semaphore waitForChemX = __________; // to let
Process B open and close valve 2 semaphore waitForChemXAndY = __________; // to let
Process C turn on and off switch 1 semaphore waitForMixingComplete = __________; // to let
Process D open and close valve 3
Process A { whle(1) { ; // wait to be signaled by C open_valve(1); close_valve(1); ; // signal B } }
Process B { while(1) { ; // wait to be signaled by A open_valve(2); close_valve(2); ; // signal C } }
Process C { while(1) { ; // wait to be signaled by B on_switch(1); off_switch(1); ; // signal D ; // signal A } }
Process D { while(1) { ; // wait to be signaled by C open_valve(3); close_valve(3); }
Explanation / Answer
Answer:
// my pseudo code changes are kept in bold
Initialize each semaphore to the appropriate value.
semaphore waitForMixingVatReady = 0; // to let wait until mix is ready
Process A open and close valve 1 semaphore waitForChemX = 1; // to let Process A to open at start
Process B open and close valve 2 semaphore waitForChemXAndY = 1; // to let Process B for Chemical X and Y before agitation
Process C turn on and off switch 1 semaphore waitForMixingComplete = 1; // to let Process C agitation is finished before realease
Process D open and close valve 3:
Process D
{
while(1)
{
open_valve(i); //critical secton entry
.....
.....
close_valve(i); //critical section exit
}
Process A {
whle(1) { ;
wait( waitForChemX); // wait to be signaled by C open_valve(1);
close_valve(1);
signal( waitForChemX); ; // signal B
}
}
Process B {
while(1) {
wait(waitForChemXAndY);
// wait to be signaled by A open_valve(2); close_valve(2); ;
signal(wait(waitForChemXAndY);
// signal C
}
}
Process C {
while(1)
{
close_valve(3); //Wait signal to D
on_switch(switch1); // wait to be signaled by B on_switch(1); off_switch(1);
//critical section code
off_switch(switch1); // signal D ;
open_valve(3); //open signal to D // signal A
}
}
Process D {
while(1) {
opend_valve(3); //wait for open signal by C
//critical section code
close_valve(3); // Releasing valve3 //wait to be signaled by C open_valve(3); close_valve(3); }
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.