Question

A university computer science department has a teaching assistant (TA) who helps undergraduate students with their...

A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA’s office is rather small and the TA can help only one student at a time in the office. There are two chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. 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 hours and finds the TA sleeping, the student must awaken the TA to ask for help. If a student arrives and finds the TA currently helping another student, the student sits on one of the chairs in the hallway and waits. If no chairs are availlable, the student will come back at a later time. For simplicity we assume there are a total of 4 students, and each student asks for help at most 2 times.

Using C and POSIX threads, mutex locks, and unnamed semaphores, implement a solution that coordinates the activities of the TA and the students. Details for this assignment are provided below.

The Students and the TA

Use Pthreads to create student threads. The TA will run as a separate thread as well. Student threads will alternate between programming for a period of time and seeking help from the TA. If the TA is available, they will obtain help. Otherwise, they will either sit in one of the chairs in the hallway or, if no chairs are available, will resume programming and will seek help at a later time. If a student arrives and notices that the TA is sleeping, the student must notify the TA using a semaphore. When the TA finishes helping a student, the TA must check to see if there are students sitting on the chairs in the hallway waiting for help. If so, the TA must help each of these students in turn. If no students are present, the TA returns to napping.

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>

/* mutex declarations */

pthread_mutex_t mutex_lock;

/* 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*/

/* the number of waiting students */
int waiting_students;

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

/* the maximum time (in seconds) to sleep */

#define MAX_SLEEP_TIME 3

/* number of potential students */

#define NUM_OF_STUDENTS 4

#define NUM_OF_HELPS 2

/* number of available seats */

#define NUM_OF_SEATS 2

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
A university computer science department has a teaching assistant (TA) who helps undergraduate students with their...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT