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

Devise an OpenMP solution for the Queue Scheduling problem described in Task 1.3

ID: 3911130 • Letter: D

Question

Devise an OpenMP solution for the Queue Scheduling problem described in Task 1.3   

Please use OpenMP directives and OpenMP synchronization primitives ONLY as appropriate.

Task 1.3: Queue Scheduling Multi-Threaded Programming

You are 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 (i) no more than a certain number of students can be in the office at the same time because the office has limited capacity, (ii) only one person is speaking at a time, (iii) each student question is answered by the professor, (iv) no student asks another question before the professor is done answering the previous one, and (v) once a student finishes asking all his/her questions, he/she must leave the office to make room for other students waiting outside the professor’s office.

You are to provide the following functions:

     Professor(). This functions starts a thread that runs a loop calling AnswerStart() and AnswerDone(). See
     below for the specification of these two functions. AnswerStart() blocks when there are no students
     around.

     Student(int id). This function creates a thread that represents a new student with identifier id that asks
     the professor one or more questions (the identifier given to your function can be expected to be
     greater or equal to zero and the first student's id is zero).

     First, each student needs to enter the professor’s office by calling EnterOffice(). If the office is already full, the student must

     wait. After a student enters the office, he/she loops running the code QuestionStart() and QuestionDone() for the
     number of questions that he/she wants to ask. The number of questions is determined by calculating
     (student identifier modulo 4 plus 1). That is, each student can ask between 1 and 4 questions,
     depending on the id. For example, a student with id 2 asks 3 questions, a student with id 11 asks 4
     questions and a student with id 4 asks a single question. Once the student has got the answer for all
     his/her questions, he/she must call LeaveOffice(). As a result, another student waiting on EnterOffice()
     may be able to proceed.

    AnswerStart(). The professor starts to answer a question of a student. Print ...
                   Professor starts to answer question for student x.

    AnswerDone(). The professor is done answering a question of a student. Print ...
                   Professor is done with answer for student x.

     EnterOffice(). It is the student’s turn to enter the professor’s office to ask questions. Print …
                   Student x enters the office.

     LeaveOffice(). The student has no more questions to ask, so he/she leaves the professor’s office. Print …

                   Student x leaves the office.

     QuestionStart(). It is the turn of the student to ask his/her next question. Print ...
                   Student x asks a question.

         Wait to print out the message until it is really that student's turn.

     QuestionDone(). The student is satisfied with the answer to his most recent question. Print ...
                   Student x is satisfied.

Since professor considers it rude for a student not to wait for an answer, QuestionDone() should not print anything until the professor has finished answering the question.

A student can ask only one question each time. i.e., a student should not expect to ask all his/her questions in a contiguous batch. In other words, once a student gets the answer to one of his/her questions, he/she may have to wait for the next turn if another student starts to ask a question before he/she does.

In the above list, x is a placeholder for the student identifier.

Your program must accept one command line parameter that represents the total number of students coming to the professor’s office, and a second command line parameter that represents the capacity of the professor’s office (i.e., how many students can be in the office at the same time). For simplicity, you can assume that the Student threads are created at the ascending order of their identifiers.

Your program must validate the command line parameters to make sure that they are numeric values.

Your program must be able to run properly with any reasonable number of students (e.g., 200) and room capacity (e.g., 8, 20, 50).

Your program must show randomness of events. For example, groups of students entering office at various points in the simulation.

Your program must reach a completion state and terminate gracefully. A proper message should be output to indicate end of simulation,

One acceptable output of your program is (assuming 3 students and a room capacity of 2):

Student 0 enters the office.

Student 1 enters the office.
Student 1 asks a question.

Professor starts to answer question for student 1.
Professor is done with answer for student 1.
Student 1 is satisfied.

Student 0 asks a question.

Professor starts to answer question for student 0.
Professor is done with answer for student 0.
Student 0 is satisfied.

Student 0 leaves the office.
Student 2 enters the office.
Student 1 asks a question.

Professor starts to answer question for student 1.
Professor is done with answer for student 1.
Student 1 is satisfied.

Student 2 asks a question.

Professor starts to answer question for student 2.
Professor is done with answer for student 2.
Student 2 is satisfied.

Student 1 leaves the office.
Student 2 asks a question.

Professor starts to answer question for student 2.
Professor is done with answer for student 2.
Student 2 is satisfied.

Student 2 asks a question.

Professor starts to answer question for student 2.
Professor is done with answer for student 2.
Student 2 is satisfied.

Student 2 leaves the office.

Explanation / Answer

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#include <time.h>

#include <stdlib.h>

#include <semaphore.h>

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 ; i < totalStudents; i++)

{

Student(i);

}

pthread_exit(NULL);

}

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