The Sleeping Teaching Assistant A university computer science department has a t
ID: 3606901 • Letter: T
Question
The Sleeping Teaching Assistant 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 has room for only one desk with a chair and computer. There are three 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 available, the student will come back at a later time. Using threads and semaphores, implement a solution in JAVA that coordinates the activities of the TA and the students. 1. Create n students, each will run as a separate thread. 2. Student threads will alternate between programming for a period of time and seeking help from the TA. 3. If the TA is available, students will obtain help 4. If the TA is not available, students will sit in a chair in the hallway (waitList) 5. If no chairs are available, students will resume programming and seek help at a later time 6. If the TA is sleeping, the student will notify TA with a semaphore 7. Use a semaphore to indicate a student in help mode 8. When a TA finishes helping a student, the TA must check to see if there are students waiting for help in the hallway. 9. The TA must help the students waiting in the hallway in the order that they started waiting. 10. If no students are waiting, then the TA can resume napping. The best option for simulating students programming – as well as the TA providing help to a student – is to have appropriate threads sleep for a random period of time. Make sure your output includes when the semaphores acquire permits and release permits, displays current mode (programming or getting help), and TA is sleeping or helping which student. The Sleeping Teaching Assistant 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 has room for only one desk with a chair and computer. There are three 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 available, the student will come back at a later time. Using threads and semaphores, implement a solution in JAVA that coordinates the activities of the TA and the students. 1. Create n students, each will run as a separate thread. 2. Student threads will alternate between programming for a period of time and seeking help from the TA. 3. If the TA is available, students will obtain help 4. If the TA is not available, students will sit in a chair in the hallway (waitList) 5. If no chairs are available, students will resume programming and seek help at a later time 6. If the TA is sleeping, the student will notify TA with a semaphore 7. Use a semaphore to indicate a student in help mode 8. When a TA finishes helping a student, the TA must check to see if there are students waiting for help in the hallway. 9. The TA must help the students waiting in the hallway in the order that they started waiting. 10. If no students are waiting, then the TA can resume napping. The best option for simulating students programming – as well as the TA providing help to a student – is to have appropriate threads sleep for a random period of time. Make sure your output includes when the semaphores acquire permits and release permits, displays current mode (programming or getting help), and TA is sleeping or helping which student. The Sleeping Teaching Assistant 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 has room for only one desk with a chair and computer. There are three 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 available, the student will come back at a later time. Using threads and semaphores, implement a solution in JAVA that coordinates the activities of the TA and the students. 1. Create n students, each will run as a separate thread. 2. Student threads will alternate between programming for a period of time and seeking help from the TA. 3. If the TA is available, students will obtain help 4. If the TA is not available, students will sit in a chair in the hallway (waitList) 5. If no chairs are available, students will resume programming and seek help at a later time 6. If the TA is sleeping, the student will notify TA with a semaphore 7. Use a semaphore to indicate a student in help mode 8. When a TA finishes helping a student, the TA must check to see if there are students waiting for help in the hallway. 9. The TA must help the students waiting in the hallway in the order that they started waiting. 10. If no students are waiting, then the TA can resume napping. The best option for simulating students programming – as well as the TA providing help to a student – is to have appropriate threads sleep for a random period of time. Make sure your output includes when the semaphores acquire permits and release permits, displays current mode (programming or getting help), and TA is sleeping or helping which student.Explanation / Answer
CODE
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
#include "semaphore.h"
#include "stdint.h"
#include "malloc.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define MAX_SLEEP_TIME 3
#define NUM_OF_STUDENTS 4
#define NUM_OF_HELP 2
#define NUM_OF_SEATS 2
pthread_t student_array[NUM_OF_STUDENTS];
pthread_t ta_thread;
pthread_mutex_t mutex_lock;
sem_t students_sem;
sem_t ta_sem;
int waiting_students = 0;
int thread_helped_counter[NUM_OF_STUDENTS];
unsigned int seed = 92;
int sleep_time;
int waiting_queue[NUM_OF_HELP];
void * students(void * param);
void * ta();
void create_students();
void create_ta();
void join_students();
void join_ta();
int value;
int main()
{
sem_init(&students_sem,0,1);
sem_init(&ta_sem,0,1);
create_students();
create_ta();
join_students();
join_ta();
sem_destroy(&students_sem);
sem_destroy(&ta_sem);
}
void join_ta()
{
if(pthread_join(ta_thread, NULL))
{
fprintf(stderr, "Error joining thread ");
}
}
void join_students()
{
int i;
for(i = 0; i < NUM_OF_STUDENTS; i++)
{
if(pthread_join(student_array[i], NULL))
{
fprintf(stderr, "Error joining thread ");
}
}
}
void create_ta()
{
if(pthread_create(&ta_thread, NULL, ta, NULL))
{
fprintf(stderr, "TA thread creation error ");
}
}
void create_students()
{
int i;
for(i = 0; i < NUM_OF_STUDENTS; i++)
{
int *arg = malloc(sizeof(*arg));
*arg = i;
if(pthread_create(&student_array[i], NULL, students, (void *)arg))
{
fprintf(stderr, "Student thread creation error ");
}
}
if(pthread_create(&ta_thread, NULL, ta, NULL))
{
fprintf(stderr, "TA thread creation error ");
}
}
void * students(void * param)
{
int i = *((int *)param);
int sleeptime = (rand_r(&seed)% MAX_SLEEP_TIME)+1;
printf("Student %d programming for %d seconds. ", i, sleeptime);
sleep(sleeptime);
pthread_mutex_lock(&mutex_lock);
if(thread_helped_counter[i] >= NUM_OF_HELP)
{
pthread_mutex_unlock(&mutex_lock);
return NULL;
}
pthread_mutex_unlock(&mutex_lock);
pthread_mutex_lock(&mutex_lock);
sem_getvalue(&ta_sem, &value);
pthread_mutex_unlock(&mutex_lock);
if(value > 0)
{
sem_wait(&ta_sem);
pthread_mutex_lock(&mutex_lock);
if(waiting_students >= 0 && waiting_students < NUM_OF_SEATS)
{
if(waiting_students == 0)
{
waiting_queue[0] = i;
printf(" Student %d takes a seat, # of waiting students = %d ",
waiting_queue[0], waiting_students);
waiting_students++;
}
else if(waiting_students == 1)
{
waiting_queue[1] = i;
printf(" Student %d takes a seat, # of waiting students = %d "
,waiting_queue[1], waiting_students);
waiting_students++;
}
pthread_mutex_unlock(&mutex_lock);
sem_post(&students_sem);
sleep_time = (rand_r(&seed)% MAX_SLEEP_TIME)+1;
sleep(sleep_time);
}
else
{
printf(" waiting list > 2 ");
pthread_mutex_unlock(&mutex_lock);
printf(" Student %d will try later ", i);
sleep((rand_r(&seed)% MAX_SLEEP_TIME)+1);
students(&i);
}
}
else
{
sem_post(&students_sem);
}
students((void*)&i);
}
void * ta()
{
int i,result;
for(i=0;i< 4; i++)
{
result+=thread_helped_counter[i];
}
if(result == 8)
{
return NULL;
}
sem_wait(&students_sem);
pthread_mutex_lock(&mutex_lock);
sleep_time = (rand_r(&seed)% MAX_SLEEP_TIME)+1;
if(waiting_students == 1 && thread_helped_counter[waiting_queue[0]] < 2)
{
waiting_students--;
printf("Helping a student for %d , # of students waiting = %d ",sleep_time, waiting_students);
printf("Student %d recieving help ", waiting_queue[0]);
thread_helped_counter[waiting_queue[0]]++;
}
else if(waiting_students == 2 && thread_helped_counter[waiting_queue[1]] < 2)
{
waiting_students--;
printf("Helping a student for %d , # of students waiting = %d ",sleep_time, waiting_students);
printf("Student %d recieving help ", waiting_queue[1]);
thread_helped_counter[waiting_queue[1]]++;
}
pthread_mutex_unlock(&mutex_lock);
sleep(sleep_time);
sem_post(&ta_sem);
ta();
}
IF ANY QUERIES REGARDING CODE AND EXECUTION PLEASE GET ABCK TO ME
THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.