Menu

[solved]-Task 13 Queue Scheduling Multi Threaded Programming Weight Write Code Help Synchronize Pro Q39039633

Task 1.3: Queue Scheduling Multi-Threaded Programming – Weight:You are to write code to help synchronize a professor and his/herstudents during office hours. The professor, of course, wants totake a nap if no students are around to ask questions; if there arestudents who want to ask questions, they must synchronize with eachother and with the professor so that (i) no more than a certainnumber of students can be in the office at the same time becausethe office has limited capacity, (ii) only one person is speakingat a time, (iii) each student question is answered by theprofessor, (iv) no student asks another question before theprofessor is done answering the previous one, and (v) once astudent finishes asking all his/her questions, he/she must leavethe office to make room for other students waiting outside theprofessor’s office. //Already finished but I’m finding some issueshere and there You are to provide the following functions:Professor(). This functions starts a thread that runs a loopcalling AnswerStart() and AnswerDone(). See below for thespecification of these two functions. AnswerStart() blocks whenthere are no students around. Student(int id). This functioncreates a thread that represents a new student with identifier idthat asks the professor one or more questions (the identifier givento your function can be expected to be greater or equal to zero andthe first student’s id is zero). First, each student needs to enterthe professor’s office by calling EnterOffice(). If the office isalready full, the student must wait. After a student enters theoffice, he/she loops running the code QuestionStart() andQuestionDone() for the number of questions that he/she wants toask. The number of questions is determined by calculating (studentidentifier modulo 4 plus 1). That is, each student can ask between1 and 4 questions, depending on the id. For example, a student withid 2 asks 3 questions, a student with id 11 asks 4 questions and astudent with id 4 asks a single question. Once the student has gotthe answer for all his/her questions, he/she must callLeaveOffice(). As a result, another student waiting onEnterOffice() may be able to proceed. AnswerStart(). The professorstarts to answer a question of a student. Print … Professorstarts to answer question for student x. AnswerDone(). Theprofessor is done answering a question of a student. Print …Professor is done with answer for student x. EnterOffice(). It isthe student’s turn to enter the professor’s office to askquestions. Print … Student x enters the office. LeaveOffice(). Thestudent has no more questions to ask, so he/she leaves theprofessor’s office. Print … Student x leaves the office.QuestionStart(). It is the turn of the student to ask his/her nextquestion. Print … Student x asks a question. Wait to print outthe message until it is really that student’s turn. QuestionDone().The student is satisfied with the answer to his most recentquestion. Print … Student x is satisfied. Since professorconsiders it rude for a student not to wait for an answer,QuestionDone() should not print anything until the professor hasfinished answering the question. A student can ask only onequestion each time. i.e., a student should not expect to ask allhis/her questions in a contiguous batch. In other words, once astudent gets the answer to one of his/her questions, he/she mayhave to wait for the next turn if another student starts to ask aquestion before he/she does. In the above list, x is a placeholderfor the student identifier. Your program must accept one commandline parameter that represents the total number of students comingto the professor’s office, and a second command line parameter thatrepresents the capacity of the professor’s office (i.e., how manystudents can be in the office at the same time). For simplicity,you can assume that the Student threads are created at theascending order of their identifiers. Your program must validatethe command line parameters to make sure that they are numericvalues. Your program must be able to run properly with anyreasonable number of students (e.g., 200) and room capacity (e.g.,8, 20, 50). Your program must show randomness of events. Forexample, groups of students entering office at various points inthe simulation. Your program must reach a completion state andterminate gracefully. A proper message should be output to indicateend of simulation, One acceptable output of your program is(assuming 3 students and a room capacity of 2): Student 0 entersthe office. Student 1 enters the office. Student 1 asks a question.Professor starts to answer question for student 1. Professor isdone with answer for student 1. Student 1 is satisfied. Student 0asks a question. Professor starts to answer question for student 0.Professor is done with answer for student 0. Student 0 issatisfied. Student 0 leaves the office. Student 2 enters theoffice. Student 1 asks a question. Professor starts to answerquestion for student 1. Professor is done with answer for student1. Student 1 is satisfied. Student 2 asks a question. Professorstarts to answer question for student 2. Professor is done withanswer for student 2. Student 2 is satisfied. Student 1 leaves theoffice. Student 2 asks a question. Professor starts to answerquestion for student 2. Professor is done with answer for student2. Student 2 is satisfied. Student 2 asks a question. Professorstarts to answer question for student 2. Professor is done withanswer for student 2. Student 2 is satisfied. Student 2 leaves theoffice./////////////////////////////////////////////////////////////////////My code has an issue with it now made some small tweaks to betteradjust to an OpenMP but I’m stuck.

#include #include #include #include #include #include intclassmate, totalRoom, studentCount; void * Professor();AnswerStart() and AnswerDone() void * Student(void *id); voidAnswerStart(int jd); void AnswerDone(int jd); void EnterOffice(intid); void LeaveOffice(int id); void QuestionStart(int id); voidQuestionDone(int id); //Semaphore operation solution. sem_tClassQs; //sem_t ClassAs; sem_t ProfsResponse; sem_tStudentsResponse; sem_t WaitRoom; int main(int argc, char *argv[]){ int m; pthread_t pthreads; pthread_tstudentThreads[studentCount]; int jds[studentCount]; studentCount =atoi(argv[1]); totalRoom = atoi(argv[2]); sem_init(&ClassQs, 0,1); sem_init(&ProfsResponse, 0, 0);sem_init(&StudentsResponse, 0, 0); sem_init(&WaitRoom, 0,totalRoom); pthread_create(&pthreads, NULL, Professor, NULL);for (m = 0; m < studentCount; m++) { jds[m] = m; pthread_create(&studentThreads[m], NULL, Student, (void *)&jds[m]); } for( m = 0; m < studentCount; m++) { pthread_join(studentThreads[m], NULL); } return 0; } void * Professor() { while(1) {sem_post(&StudentsResponse); sem_wait(&ProfsResponse);AnswerStart(classmate); AnswerDone(classmate);sem_post(&StudentsResponse); } } void * Student(void *id) { intjd = *((int *) id); int qu = (jd % 4) + 1; sem_wait(&WaitRoom);EnterOffice(jd); int a; for(a = 0; a < qu; a++) {sem_wait(&ClassQs); classmate = jd;sem_wait(&StudentsResponse); QuestionStart(jd);sem_post(&ProfsResponse); sem_wait(&StudentsResponse);QuestionDone(jd); sem_post(&ClassQs); }sem_post(&WaitRoom); LeaveOffice(jd); } //Print statements.void AnswerStart(int jd) { printf(“Professor starts to answerquestion for student %d.n”, jd); } void AnswerDone(int jd) {printf(“Professor is done with answer for student %d.n”, jd); }void EnterOffice(int id) { printf(“Student %d enters theoffice.n”, id); } void LeaveOffice(int id) { printf(“Student %dleaves the office.n”, id); } void QuestionStart(int id) {printf(“Student %d asks a question.n”, id); } voidQuestionDone(int id) { printf(“Student %d is satisfied.n”, id);}

Expert Answer


Answer to Task 1.3: Queue Scheduling Multi-Threaded Programming – Weight: You are to write code to help synchronize a professor an… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *