Computer Science, asked by rahultundra, 8 months ago

A university computer science department has a teaching assistant (TA) whohelps
undergraduate students with their programming assignments duringregular office hours. The
TA’s office is rather small and has room for only one
desk with a chair and computer. There are three chairs in the hallway outsidethe office where
students can sit and wait if the TA is currently helping anotherstudent. When there are no
students who need help during office hours, the
TA sits at the desk and takes a nap. If a student arrives during office hoursand finds the TA
sleeping, the student must awaken the TA to ask for help. If astudent arrives and finds the TA
currently helping another student, the student sit on one of the chair in hallway and wait. If no chairs are available the student will come back at last position using posix thread,mutex locks,semaphores and implement a solution that coordinatea the activities of TA and the students.

Answers

Answered by Varsha2711
0

Answer:

To simulate students programming in student threads, and the TA providing help to a student in the TA thread, the appropriate threads should sleep (by invoking sleep() ) for a random period of time (up to 3 seconds). Instead of invoking the random number generator rand() which is not thread-safe, each thread should invoke the thread-safe version rand_r().

1) rand_r() computes a sequence of pseudo-random integers in the range [0, RAND_MAX]. If you want a value between 1 and n inclusive, use (rand_r(&seed) % n) + 1.

2) It is recommended to use a different seed value for rand_r() in each thread so that each thread can get a different sequence of pseudo-random numbers.

For simplicity, each student thread terminates after getting help twice from the TA. After all student threads terminate, the program cancels the TA thread by calling pthread_cance() and then the entire program terminates.

POSIX Synchronization

You need the following mutex locks and (unmade POSIX) semaphores:

#include <pthread.h>

#include <semaphore.h>

pthread_mutex_t mutex_lock; //mutex declaration

/* semaphore declarations */

sem_t students_sem; //ta waits for a student to show up, student notifies ta his/her arrival

sem_t ta_sem; //student waits for ta to help, ta notifies student he/she is ready to help

int waiting_students; //the number of waiting students

Based on aforementioned requirements, your C program should have the following #defines:

#define MAX_SLEEP_TIME 3 //the maximum time(in seconds) to sleep

#define NUM_OF_STUDENTS 4 //number of potential students

#define NUM_OF_HELPS 2

#define NUM_OF_SEATS 2 //number of available seats

Similar questions