Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. [programming question] (80 pts) A university computer science department has

ID: 3735711 • Letter: 2

Question

2. [programming question] (80 pts) 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 available, the student will come back at a later time. For simplicity we assume there are total four students, and each student receives TA's helps at most 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-safe version rand r) 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 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. To simplify the situation, when TA helps a student, only the TA thread invokes sleep () while the student thread does not invoke sleep().

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();

}

Code with no errors

If any queries please get back to me

Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote