Answered Essay: You have been hired by the CS Department to write code to help synchronize a professor and his/her students dur

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.

Expert 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 %dn", identification);
}

void AnswerDone()
{
        printf("Professor is done with answer for student %dn", 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 %dn ", 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 questionn", identification);
}

void QuestionDone()
{
        printf("Student %d is satisfiedn", identification);
}

void nap()
{
        printf("professor is napping...n");
}

void EnterOffice()
{
        printf("student %d shows up in the officen", identification);
}

void LeaveOffice()
{
        printf("Student %d leaves the officen", 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);
}
Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?