Question
A university computer science department has a teaching assistant (TA) who helpsundergraduate students with their programming assignments during regular office hours. The TAs office is
A university computer science department has a teaching assistant (TA) who helpsundergraduate students with their programming assignments during regular office hours. The TAs office is rather smalland the TA can help only one student at a time in the office. There are two chairs in the hallway outside the officewhere students can sit and wait if the TA is currently helping another student. When there are no students who needhelp during office hours, the TA sits at the desk and takes a nap. If a student arrives during office hours and finds the TAsleeping, the student must awaken the TA to ask for help. If a student arrives and finds the TA currently helping anotherstudent, the student sits on one of the chairs in the hallway and waits. If no chairs are available, the student will comeback at a later time. For simplicity we assume there are total four students, and each student receives TAs helps atmost two 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 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 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 may return 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 three seconds). Instead of invoking the random number generator rand() which is not thread-safe, each thread should invoke the thread-safeversion rand_r().
- rand_r() computes a sequence of pseudo-random integers in the range [0, RAND_MAX]. If you want a valuebetween 1 and n inclusive, use (rand_r(&seed) % n) + 1.
- It is recommended to use a different seed value for rand_r() in each thread so that each thread can get adifferent 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_cancel() and then the entire program terminates.
POSIX Synchronization You need the following POSIX mutex locks and (unnamed) semaphores: #include
/* mutex declarations, a global variable */ pthread_mutex_t mutex_lock; /* protect the global variable waiting_student */
/* semaphore declarations, global variables */ 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, a global variable */ int waiting_students;
Other than the above global variables, you are not allowed to have any additional global variables for synchronization purpose, e.g., students_finished, is_ta_sleeping, or variables to that effect. It is OK, but not recommended, to have additional global variables to record information (e.g., thread ids), as long as they are not for synchronization purpose.
Coverage of POSIX mutex locks and unnamed semaphores (sem_init, sem_wait, sem_post) are provided in Section 5.9.4. Consult that section for details.
The following is a sample output:
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
PLEASE NOTE:
1. Each invocation the program always prints out CS149 SleepingTA from FirstName LastName only once. Take screenshots of the program execution (including CS149 SleepingTA from ). 2. Any API in a multi-threaded application must be thread-safe (e.g., on Linux rand() vs rand_r()). Invoking non thread-safe API is subject to deduction. 3. You must utilize array for various data structures, and utilize loop to remove repeating code. Any repetitive variable declaration and/or code are subject to deduction.
CS149 Sleeping TA fron FirstNane LastName Student 2 programning for 2 seconds Student 1 programning for 3 seconds student programming for 3 seconds Student 3 programming for 3 seconds Student 2 takes a seat, of waiting students 1 Helping a student for 3 seconds, of waiting students Student 2 receiving help Student 2 programming for 3 seconds student 1 takes a seat, of waiting students 1 Student takes a seat, of waiting students 2 Student 3 will try later Student 3 programming for 3 seconds Student 2 wull try later student 2 programming for 1 seconds Helping a student for 3 seconds, of waiting students 1 Student 1 receiving help Student 1 prograrning for 3 seconds Student 3 takes a seat of waiting students 2 Student 2 will try later student 2 programming for 3 seconds Student 1 will try later Student 1 progranning for 3 seconds Helping a student for 3 seconds, of watting students 1 student receiving help student e programming for 3 seconds Student 2 takes a seat, of waiting students 2 Student 0 will try later student programming for 3 seconds student 1 will try later student 1 programming for 3 seconds Helping a student for 3 seconds, of waiting students 1 Student 3 receiving help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started