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

Project : The Sleeping Teaching Assistant A university computer science departme

ID: 674802 • Letter: P

Question

Project : 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 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 the simplicity we assume there are total 4 students, and each student asks for helps at most two times.

Using POSIX threads, mutex locks, and 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

Using Pthreads (Section 4.4.1), begin by creating n students. Each will run as a separate thread. 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 a chair 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 waiting for help in the hallway. 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 students threads, and the TA providing help to a student in the TA thread, the appropriate threads should sleep (by invoking sleep()) for a random of time (up to three seconds).

Please invoke rand_r() in each thread to retrieve the random number as sleep time. Before any invocation of rand_r(), one should invoke srand(), just one is good enough, to set a proper seed in the main program.

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

POSIX Synchronization

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

#include <pthread.h>

#include<semaphore.h>

pthread_mutex_t mutex_lock;

/*semaphore declarations */

sem_t students_sem; /* ta wait for a student to show up, student notifies ta his/her arrivial */

  

sem_t ta_sem; /** student wait for ta to help, ta notifies student he/she is read to help **/

/** the number of waiting students **/

int waiting_students;

Covering of POSIX mutex locks and semaphores ( sem_init, sem_wait, sem_post) are provided in section 5.9.4. Consult that section for detail.

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

/* the maximum time (int 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

The book I use for this Project is :Operating System Concepts, 9th Edition.

Please help me with this project. I do not know C; I just know Java only.

Best regards,

Explanation / Answer

Here is the code for Student and TA thread problem. Please don't forget to rate if hte answer helped. Thank you very much.

You can save the file in say students.c and compile using

gcc students.c -lpthread -o students

output file name is students. Now execute using

./students

====================

#include <pthread.h>
#include<semaphore.h>
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
pthread_mutex_t mutex_lock;
/*semaphore declarations */
sem_t students_sem; /* ta wait for a student to show up, student notifies ta his/her arrivial */

sem_t ta_sem; /** student wait for ta to help, ta notifies student he/she is read to help **/
/** the number of waiting students **/
int waiting_students;
/* the maximum time (int 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

volatile int seats[NUM_OF_SEATS];
volatile int empty_chair,next_stud;

int occupied=0;

void initialize()
{
sem_init(&ta_sem,1,0);

sem_init(&students_sem,1,0);
pthread_mutex_init(&mutex_lock,NULL);
empty_chair=0;
next_stud=0;
}


void* teacher(void *arg)
{
int help_time;
int v;
printf(" TA thread started");

while(1)
{


sem_wait(&students_sem); //TA waiting for student
help_time = rand() % MAX_SLEEP_TIME + 1;

pthread_mutex_lock(&mutex_lock);
printf(" TA has %d students waiting for help.",occupied);

printf(" TA helping student %d from seat #%d",seats[next_stud],(next_stud+1));
occupied--;
next_stud = (next_stud + 1)%NUM_OF_SEATS;

pthread_mutex_unlock(&mutex_lock);
sleep(help_time);
   printf(" TA is avaiable for help.");
sem_post(&ta_sem); //TA is free

}
pthread_exit(NULL);
}

void* student(void *thread_id)
{

long id=(long)thread_id;
int help_count=0;
int v;
int coding_time;
int can_wait;
printf(" Started student %ld",id);

while(1)
{


printf(" Student %ld coding ...",id);
coding_time= rand() % MAX_SLEEP_TIME + 1;
sleep(coding_time);
printf(" Student %ld going to TA for help #%d .",id,(help_count+1));


pthread_mutex_lock(&mutex_lock);
//printf(" occupied=%d",occupied);
   if(occupied==NUM_OF_SEATS) //Check if all seats are occupied
{
printf(" No seat avaiable for student %ld. Going back to coding.",id);
pthread_mutex_unlock(&mutex_lock);
continue;
}
else
{
seats[empty_chair]=id;
occupied++;
printf(" Student %ld waiting for TA in seat #%d",id,(empty_chair+1));
empty_chair = (empty_chair + 1)%NUM_OF_SEATS;
pthread_mutex_unlock(&mutex_lock);
sem_post(&students_sem);//Tell TA that a student is waiting for help

pthread_mutex_unlock(&mutex_lock);

sem_wait(&ta_sem); //Now wait for TA to help

//sleep(1);
help_count++;
if(help_count==NUM_OF_HELPS) //if already got the maximum number of helps come out
break;


}
}
printf(" Student %ld finished and exiting!",id);
pthread_exit(NULL);
}

int main()
{
pthread_t ta_thread,student_thread[NUM_OF_STUDENTS];
int i=1;
initialize();

pthread_create(&ta_thread,NULL,teacher,NULL);

for(i=1;i<=NUM_OF_STUDENTS;i++)
{
pthread_create(&student_thread[i-1],NULL,student,(void *)i);
}

//sleep(2);

for(i=1;i<=NUM_OF_STUDENTS;i++)
{
pthread_join(student_thread[i-1],NULL);
}

printf(" All students finished work. Stopping TA thread... ");
pthread_cancel(ta_thread);
}

sample output


TA thread started
Started student 1
Student 1 coding ...
Started student 2
Student 2 coding ...
Started student 3
Student 3 coding ...
Started student 4
Student 4 coding ...
Student 3 going to TA for help #1 .
Student 3 waiting for TA in seat #1
   TA has 1 students waiting for help.
   TA helping student 3 from seat #1
Student 1 going to TA for help #1 .
Student 1 waiting for TA in seat #2
Student 4 going to TA for help #1 .
Student 4 waiting for TA in seat #1
Student 2 going to TA for help #1 .
No seat avaiable for student 2. Going back to coding.
Student 2 coding ...
Student 2 going to TA for help #1 .
   TA is avaiable for help.
Student 3 coding ...
No seat avaiable for student 2. Going back to coding.
Student 2 coding ...
   TA has 2 students waiting for help.
   TA helping student 1 from seat #2
Student 3 going to TA for help #2 .
Student 3 waiting for TA in seat #2
Student 2 going to TA for help #1 .
No seat avaiable for student 2. Going back to coding.
Student 2 coding ...
   TA is avaiable for help.
   TA has 2 students waiting for help.
   TA helping student 4 from seat #1
Student 1 coding ...
Student 2 going to TA for help #1 .
Student 2 waiting for TA in seat #1
Student 1 going to TA for help #2 .
No seat avaiable for student 1. Going back to coding.
Student 1 coding ...
   TA is avaiable for help.
   TA has 2 students waiting for help.
   TA helping student 3 from seat #2
Student 4 coding ...
   TA is avaiable for help.
   TA has 1 students waiting for help.
   TA helping student 2 from seat #1
Student 1 going to TA for help #2 .
Student 1 waiting for TA in seat #2
Student 3 finished and exiting!
Student 4 going to TA for help #2 .
Student 4 waiting for TA in seat #1
   TA is avaiable for help.
   TA has 2 students waiting for help.
   TA helping student 1 from seat #2
Student 2 coding ...
   TA is avaiable for help.
Student 2 going to TA for help #2 .
   TA has 1 students waiting for help.
   TA helping student 4 from seat #1
Student 1 finished and exiting!
Student 2 waiting for TA in seat #2
   TA is avaiable for help.
   TA has 1 students waiting for help.
   TA helping student 2 from seat #2
Student 4 finished and exiting!
   TA is avaiable for help.
Student 2 finished and exiting!
All students finished work. Stopping TA thread...