Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Threads and Deadlock Rationale: To show that threads can be tricky - deadlock st

ID: 3752876 • Letter: T

Question

Threads and Deadlock

Rationale: To show that threads can be tricky - deadlock states are highly possible. 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 getting 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 (although one or both may be modified by you) and do not change the Runner class except, maybe, to add a print statement or two. Important: the synchronized modifiers before void in ping and confirm in the code below prevent threads from executing inside both ping and confirm concurrently. Your solution must support this property.

Explanation / Answer

he 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;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote