Question: A teacher sets up office hours to answer questions relating to course materials and assignments. The teacher meets one student at a time. There are

A teacher sets up office hours to answer questions relating to course materials and assignments. The teacher meets one student at a time. There are n chairs outside the office where students can sit and wait if the teacher is currently helping another student. The teacher helps students in a first-come-first-serve way. The interactions between students and the teachers are possibly in four cases: (1) When there are no students who need help during office hours, the teacher goes back to own work. (2) If a student arrives during office hours and finds the teacher on own work, the student raises the
teacher attention (which the teacher will respond) to ask for help. (3) If a student arrives and finds the teacher currently helping another student, the student sits on one of the chairs and waits. (4) If no chairs are available, the student will go study and come back at a later time. You will use threads to simulate the interactions between students and the teacher. The synchronization primitives such as mutex locks, semaphores, or conditional variables in pthread library in C can be used to implement a solution that coordinates the activities of the teacher and the students. A FIFO queue can be used for the seats taken by students. For this project, you will implement only locks and condition variables.
The project will use two types of threads, the Students and the Teacher. Each student will run as a separate thread. The total numbers of students should be input from command line. The teacher will run as a separate thread. A student thread alternates between studying for a period of time and seeking help from the teacher. If the teacher is available, one student will obtain help. Otherwise, they will either sit in a chair or, if no chairs are available, will resume study and will seek help at a later time. The number of chairs should be input from command line. If a student arrives and
notices that the teacher is on his own work, the student calls the teacher. When the teacher finishes helping a student, the teacher checks to see if there are students on chairs waiting for help. If so, the teacher continues helping each of these students in turn. If no students are present (all chairs are empty), the teacher returns to his own work. The code will read four integers from command line. They are: the number of students, the number of chairs, and the left and right of the interval for the random time period, which will be used in mytime (int left, int right). If input is wrong, prompt should be given for try again with correct format.
(1) To simulate student studying for a while in student threads, and the time teacher providing help to a student in the
teacher thread, a sleep function for a random time period should be used. A customized function called int mytime (int left, int right) is provided for this purpose. mytime (int left, int right) produces a random number within a given interval between left and right. It calls rand() for a random number. To have different random number sequences for each execution, one should invoke srand(z) to set a proper seed z in the main program before any invocation of rand().(2) Each student thread terminates after getting help twice from the teacher. In main program, don't forget join student
threads. After all student threads terminate, the main program cancels the teachers thread by calling pthread_cancel() and then entire program terminates.
(3) In the code, print out necessary info about the activities during the execution of the code. (3.1) Before sleep, print out "Student (or Teacher) to sleep X sec;".(3.2) After wake up from sleep, print out "Student (or Teacher) Id wake up;".(3.3) For either the student or teacher thread, before calling mutex locks, or wait on semaphores, or conditional variables, printout "Student (or Teacher) will call mutex_lock / sem_wait / conditional variables cond_wait ".(3.4) For either the student or teacher thread, after calling to unlock mutex locks, or post on semaphores, or conditional variables, printout "Student (or Teacher) call mutex_unlock / sem_post / conditional variables cond_signal ".
mytime.h
#include
#include
int mytime (int left, int right){
int time =0;
time = left + rand()%(right - left);
// printf("random time is %d sec
", time);
return time;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!