You have been hired by the CS Department to write code to help synchronize a pro
ID: 3867327 • Letter: Y
Question
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 #include #include #include #include #include struct student{ int id; int numQuestions; //number of questions to ask int questionNum; //number of questions that have been answered }; int totalStudents; int inOffice, studentZ; //in office - students in office; studentZ keeps track of students enter and leave sem_t readyForQuestion, questionWait, answerWait, capacity; //pthread_cond_t stud, prof; pthread_t professor; pthread_mutex_t question_lock; /*lock, lock2,*/ //int professorWait = 0, studentWait; int identification; void Professor(); void *StartProfessor(); void AnswerStart(); void AnswerDone(); void Student(int id); void * StartStudent(void * student); void QuestionStart(); void QuestionDone(); void nap(); void EnterOffice(); void LeaveOffice(); void Professor() { //pthread_mutex_init(&lock, NULL); //pthread_mutex_init(&lock2, NULL); pthread_mutex_init(&question_lock, NULL); //initialize semaphores //second parameter signifies whether it is shared(1) or not(0) sem_init(&readyForQuestion,1,0); sem_init(&questionWait,1,0); sem_init(&answerWait,1,0); sem_init(&capacity,0,inOffice);//not using yet // pthread_cond_init(&prof, NULL); // pthread_cond_init(&stud, NULL); studentZ = 0; //pthread_mutex_lock(&lock); //pthread_mutex_lock(&lock2); pthread_create(&professor, NULL, StartProfessor, NULL); } //does professor things void * StartProfessor() { while(1) { //if no students nap //if(inOffice == 0) //totalStudents //{ //nap(); //} //pthread_mutex_lock(&lock); //if(inOffice > 0) //{ sem_post(&readyForQuestion); //sem wait for question sem_wait(&questionWait); AnswerStart();//answer start AnswerDone();//answer done sem_post(&answerWait); //pthread_mutex_unlock(&lock2); //} } } void AnswerStart() { //struct student * std = student; printf("Professor starts to answer question for student %d ", identification); } void AnswerDone() { printf("Professor is done with answer for student %d ", identification); } //makes a thread for 1 student void Student(int id) { //printf("you no make student"); struct student * newStd = malloc(sizeof(struct student)); newStd->id = id; newStd->numQuestions = (id % 4) + 1; newStd->questionNum = 0; //EnterOffice(); pthread_t stack; pthread_create(&stack, NULL, (void *) StartStudent, (void *) newStd); } //work for student thread void *StartStudent(void * student) { struct student * std = student; studentZ++; while(std->numQuestions > std->questionNum) { sem_wait(&readyForQuestion); pthread_mutex_lock(&question_lock); identification = std->id; //sem_wait(&readyForQuestion); QuestionStart(); //semaphore answer question (post) sem_post(&questionWait); //sem wait for answer sem_wait(&answerWait); //pthread_mutex_unlock(&lock); //professor starts answering //pthread_mutex_lock(&lock2); //professor done answering QuestionDone(); pthread_mutex_unlock(&question_lock); std->questionNum++; //printf("%d %d %d ", std->questionNum,std->numQuestions, inOffice); if(std->questionNum == std->numQuestions) { LeaveOffice(); studentZ--; if(studentZ == 0) //this is new here nap(); } } } void QuestionStart() { printf("Student %d asks a question ", identification); } void QuestionDone() { printf("Student %d is satisfied ", identification); } void nap() { printf("professor is napping... "); } void EnterOffice() { printf("student %d shows up in the office ", identification); } void LeaveOffice() { printf("Student %d leaves the office ", identification); } int main(int argc, char *argv[]) { totalStudents = atoi(argv[1]); inOffice = atoi(argv[2]); int i; Professor(); for(i = 0 ; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.