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

Read Chapter 7 – Deadlock Avoidance You have been hired by the CS Department to

ID: 2246518 • Letter: R

Question

Read Chapter 7 – 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. Read Chapter 7 – 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. Read Chapter 7 – 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

#include<iostream>

using namespace std;

//AnswerStart(), AnswerDone(), QuestionStart() and QuestionDone()

class Test {

private:

mutex obj;

public:

void AnswerStart() {

unique_lock<mutex> lock(obj);

AnswerStartHelper();

//answer started

while(!AnswerDone())

{

//wait for mutex lock to unlock

}

}

void AnswerDone(){

if(answerDone)

{

unique_lock<mutex> unlock(obj);

return true;

}

return false;

}

void QuestionStart(bool calledFromWithinClass = false) {

unique_lock<mutex> lock(obj);

QuestionStartHelper();

}

void QuestionDone(){

if(questionDone)

{

unique_lock<mutex> unlock(obj);

return true;

}

return false

}

private:

void AnswerStartHelper() {

//started answering question

}

void QuestionStartHelper() {

//once Question Start it go to professor for answer

AnswerStartHelper();

}

}

when QuestionStart function call it wait for to finish it to comlete by taking lock ot it. and AnswerStart also take lock to finish it work. Till then no other student canask question

This is the implementation as per i though about system it can be changed or there are many more ways to achieve synchronization.

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