Deadlock Avoidance You have been hired by the CS Department to write code to hel
ID: 3776303 • Letter: D
Question
Deadlock Avoidance
You have been hired by the CS Department to write code to help synchronize a professor and his/her students during office hours. The professor, of course, wants to take a nap if no students are around to ask questions; if there are students who want to ask questions, they must synchronize with each other and with the professor so that
only one person is speaking at any one time,
each student question is answered by the professor, and
no student asks another question before the professor is done answering the previous one.
You are to write four procedures: AnswerStart(), AnswerDone(), QuestionStart() and QuestionDone().
The professor loops running the code: AnswerStart(); give answer; AnswerDone(). AnswerStart doesn’t return until a question has been asked. Each student loops running the code: QuestionStart(); ask question; QuestionDone(). QuestionStart() does not return until it is the student’s turn to ask a question. Since professors consider it rude for a student not to wait for an answer, QuestionEnd() should not return until the professor has finished answering the question. You can use a command line interface for this program. You are free to make other design choices and be creative in your implementation. You may use any programming language of your choice.
Explanation / Answer
package org.chegg.qa;
class QuestionAndAnswers {
int n;
boolean valueSet = false;
synchronized void questionStart(int n) {
if (valueSet)
try {
questionDone();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("Student Asking question...");
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
System.out.print(".........");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
notify();
}
public void questionDone() {
System.out.println();
System.out.println("Question Done");
}
synchronized int answerStart() {
if (!valueSet)
try {
answerDone();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Proffessor Answering...");
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
System.out.print("....");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
valueSet = false;
notify();
return n;
}
public void answerDone() {
System.out.println();
System.out.println("Answer Done");
}
}
class Proffesor implements Runnable {
QuestionAndAnswers queandans;
Proffesor(QuestionAndAnswers queandans) {
this.queandans = queandans;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while (true) {
queandans.questionStart(i++);
}
}
}
class Students implements Runnable {
QuestionAndAnswers queandans;
Students(QuestionAndAnswers queandans) {
this.queandans = queandans;
new Thread(this, "Consumer").start();
}
public void run() {
while (true) {
queandans.answerStart();
}
}
}
class NewClass {
public static void main(String args[]) {
QuestionAndAnswers queandans = new QuestionAndAnswers();
new Proffesor(queandans);
new Students(queandans);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.