Suppose three subprograms (Ready, Aim, and Fire) must coordinate to cause a weap
ID: 3765019 • Letter: S
Question
Suppose three subprograms (Ready, Aim, and Fire) must coordinate to cause a weapons (say, a Howitzer) system to fire at a specified target. Many subprograms may call Ready with a target they want destroyed. However, once subprogram Ready accepts one target, it must first compute and pass the coordinates for that target on to the Aim subprogram before it can accept another target. Furthermore, once subprogram Aim receives a set of coordinates from Ready, it must call Fire as soon as it locks the weapon onto the target coordinates; this must occur before Aim can accept another set of coordinates from Ready.
a) Using only simple binary semaphores, outline the code for calling Ready, Aim, and Fire
b) Assuming no queues are maintained by your binary semaphores, what problems might arise?
Explanation / Answer
Binary Semaphores can only be in the two states i.e taken and not taken.
Taking the binary semaphore brings it in the taken state, trying to take a semaphore already taken enters the invoking thread into the waiting state.
Releasing a binary semaphore bring it into the "not taken" state.
A)
I will outline the code in the java for this problem.
Now a very important thing to notice that,whenever there is lock acquired by one thread on the class, other thread will not be able to call or use the function of that particular class.
Hence we will make one class which will implement all the three function, and each function in turm will call one another, and finally at the end the other thread are allowed to work upon.
public class Execute impelemnts Runnable{
public void run(){
public void Ready(){
Aim();
}
public void Aim(){
Fire();
}
}
}
Hence these functions are calling one by one in turn.
Now we declare the second class which acquire the semaphore and releases it.
class DemoSemaphore
{
private final Semaphore semaphore;
public FunctionQueue()
{
semaphore = new Semaphore(1); //Setting semaphore value to 1.
}
public void doJob(Object)
{
try
{
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + ": );
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
System.out.println("Job done");
semaphore.release(); //releasing semaphore here
}
}
}
The run method is called from the main class which will create the thread and then execute it.
B)
When we assume that we have no queue maintained by out binary semaphore then out function Ready, Aim and Fire
will not behave properly.
When the Ready function is completed then it should call Aim function which in turns will call Fire function.
If there are no queue then then Release() called on Aim will release "taken" state from all the threads.
After that any thread will be able to call any function, which ultimately fails the requirement.
Concurrency issues will be there if the queue are not maintained.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.