Lab Problem: The code below attempts to implement an authentication protocol bet
ID: 3748763 • Letter: L
Question
Lab Problem: The code below attempts to implement an authentication protocol between two objects, called Boy and Gir1. The protocol is asynchronous and works as follows: each "pings" the other when the other is pinged, it responds with a "confirm When the code is executed the following is output Starting...1 Girl (ping): pinging Boy Boy (ping): pinging Girl due to a deadlock condition: both Boy and Girl are made to wait in ping and no one can invoke confirm. You may have to run the code several times before you see this Rework the code to get output that has both the girl and the boy etting confirmation an example looks like this starting.. .1 Girl (ping): pinging Boy Boy (ping) pinging Girl Girl (ping): asking Boy to confirm Boy (confirm): confirm to Girl Girl (ping): got confirmation Boy (ping) asking Girl to confirm Girl (confirm): confirm to Boy Boy (ping): got confirmation The girl and the boy must get confirmation on every run. Try not to blow this off by serializing the code (for example, do not have the Girl autheticate the Boy then have the Boy authenticate the Girl). Leave the ping and the confirm methods in the Monitor and do not change the Runner class except, maybe, to add a print statement.Explanation / Answer
The above-mentioned code need changes to make confirmation ping, as the condition is a deadlock condition no one is able to confirm each other's ping.
Solution: To remove a deadlock the simple approch is to lock the resource in use so that no one else can use it hence we need to add methods of Lock(), Unlock() and isLocked() too so that we can give confirmation to the girl or boy Ping. remove lock when you are done.
Please find below the updated code and comments for why we need them
class Monitor{
String name;
public volatile boolean locked = false;
public pitor(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void ping(pitor p) {
try {
while (this.isLocked()) { //checking the condition of object if its locked or unlocked
wait(); //if unlocked let the object wait until the first object work is completed
}
} catch(InterruptedException e) { //exception handling
Thread.currentThread().interrupt();
}
System.out.println(this.name + " (ping): pinging " + p.getName()); // ping object
p.lock(); // locking up the object
System.out.println(this.name + " (ping): asking " + p.getName() + " to confirm"); // asking to confirm
p.confirm(this);
System.out.println(this.name + " (ping): got confirmation"); // got confirmed message
p.unlock(); // unlock all and notify all threads about the update of state
notifyAll();
}
// confirmation message to the other object
public synchronized void confirm(pitor p) {
System.out.println(this.name + " (confirm): confirm to " + p.getName());
}
//locking up the object just by making its locked status true
public synchronized void lock() {
System.out.println(this.name + " (lock)");
this.locked = true;
}
// unlocking by making locked status false
public synchronized void unlock() {
System.out.println(this.name + " (unlock)");
this.locked = false;
}
//check the locked status
public boolean isLocked() {
System.out.println(this.name + " (isLocked): " + this.locked);
return this.locked;
}
}
**please copy the code first in notepad++ or notepad then copy to any IDE to avoid copying errors.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.