Using POSIX threads and mutex locks and semaphores implement a solution that coordinates the activities of the TA and the students
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 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, 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.
Perhaps the best option for simulating students programming—as well as
the TA providing help to a student—is to have the appropriate threads sleep
for a random period of time.
POSIX Synchronization
Answer:
main_1.c
#include <pthread.h> #include <stdio.h> #include <semaphore.h> #include <string.h> #include <errno.h> #include "ta.h" pthread_t ta; pthread_t students[NUM_OF_STUDENTS]; /** * Initialize all relevant data structures and * synchronization objects. */ void init() { int i; if ( pthread_mutex_init(&mutex_lock, NULL) != 0) printf("%s\n",strerror(errno)); /** OS X code */ /** Remove semaphores if they currently exist */ if (sem_unlink("STUDENTS") == -1) printf("%s\n",strerror(errno)); if (sem_unlink("TA") == -1) printf("%s\n",strerror(errno)); /** Initialize the semaphores */ if ( (students_sem = sem_open("STUDENTS", O_CREAT, 0666, 0)) == SEM_FAILED) printf("%s\n",strerror(errno)); if ( (ta_sem = sem_open("TA", O_CREAT, 0666, 0)) == SEM_FAILED) printf("%s\n",strerror(errno)); waiting_students = 0; for (i = 0; i < NUM_OF_STUDENTS; i++) student_id[i] = i; } void create_students() { int i; for (i = 0; i < NUM_OF_STUDENTS; i++) { pthread_create(&students[i], 0, student_loop, (void *)&student_id[i]); } } void create_ta() { pthread_create(&ta, 0, ta_loop, 0); } int main(void) { int i; init(); create_ta(); create_students(); for (i = 0; i < NUM_OF_STUDENTS; i++) pthread_join(students[i], NULL); /* when all students have finished, we will cancel the TA thread */ if (pthread_cancel(ta) != 0) printf("%s\n",strerror(errno)); return 0; }
hangout_1.c/** * Simulate a student hanging out */ #include <stdio.h> void hang_out(int lnumber, int sleep_time) { printf("\tStudent %d hanging out for %d seconds\n",lnumber,sleep_time); sleep(sleep_time); }
help_student/** * Simulate helping a student */ #include <stdio.h> #include "ta.h" void help_student(int sleep_time) { printf("Helping a student for %d seconds waiting students = %d\n",sleep_time, waiting_students); sleep(sleep_time); }
student_1.c/** General structure of a student.*/ #include <pthread.h> #include <stdio.h> #include <time.h> #include <errno.h> #include <string.h> #include "ta.h" void *student_loop(void *param) { int *lnumber = (int *)param; int number = *lnumber; int sleep_time; int times_through_loop = 0; srandom((unsigned)time(NULL)); while (times_through_loop < 5) { sleep_time = (int)((random() % MAX_SLEEP_TIME) + 1); hang_out(*lnumber,sleep_time); /* acquire the mutex lock */ if ( pthread_mutex_lock(&mutex_lock) != 0) printf("StudentA %s\n",strerror(errno)); if (waiting_students < NUM_OF_SEATS) { ++waiting_students; printf("\t\tStudent %d takes a seat waiting = %d\n",*lnumber, waiting_students); if (sem_post(students_sem) != 0) printf("StudentB %s\n",strerror(errno)); if (pthread_mutex_unlock(&mutex_lock) != 0) printf("StudentC %s\n",strerror(errno)); if (sem_wait(ta_sem) != 0) printf("StudentD %s\n",strerror(errno)); printf("Student %d receiving help\n",*lnumber); ++times_through_loop; } else { printf("\t\t\tStudent %d will try later\n",*lnumber); pthread_mutex_unlock(&mutex_lock); } } }
ta.c/**General structure of the teaching assistant.*/ #include <pthread.h> #include <stdio.h> #include <time.h> #include <errno.h> #include <string.h> #include "ta.h" void *ta_loop(void *param) { int sleep_time; /* seed random generator */ srandom((unsigned)time(NULL)); while (1) { /* wait for a student to show up */ if ( sem_wait(students_sem) != 0) printf("%s\n",strerror(errno)); /* acquire the mutex lock */ if (pthread_mutex_lock(&mutex_lock) != 0) printf("%s\n",strerror(errno)); --waiting_students; /* indicate the TA is ready to help a student */ if (sem_post(ta_sem) != 0) printf("%s\n",strerror(errno)); /* release mutex lock */ if (pthread_mutex_unlock(&mutex_lock) != 0) printf("%s\n",strerror(errno)); sleep_time = (int)((random() % MAX_SLEEP_TIME) + 1); help_student(sleep_time); } }
ta.h/** * Header file for dining philosophers */ #include <pthread.h> #include <semaphore.h> /* the maximum time (in seconds) to sleep */ #define MAX_SLEEP_TIME 5 /* number of maximum waiting students */ #define MAX_WAITING_STUDENTS 3 /* number of potential students */ #define NUM_OF_STUDENTS 5 /* number of available seats */ #define NUM_OF_SEATS 3 /* semaphores and mutex lock */ pthread_mutex_t mutex_lock; /* OS X semaphore declarations */ sem_t *students_sem; sem_t *ta_sem; /* the number of waiting students */ int waiting_students; /* student being served */ int student_number; /* the numeric id of each student */ int student_id[NUM_OF_STUDENTS]; /* student function prototype */ void *student_loop(void *param); /* ta function prototype */ void *ta_loop(void *param);
Makefile_1_2_3_4 file
# To run, enter # make all all: sleepingta sleepingta: main.o student.o ta.o hangout.o help_student.o gcc -o sleepingta main.o student.o ta.o hangout.o help_student.o main.o: main.c ta.h gcc -c main.c student.o: student.c ta.h gcc -c student.c ta.o: ta.c ta.h gcc -c ta.c hangout.o: hangout.c gcc -c hangout.c help_student.o: help_student.c gcc -c help_student.c
Comment ( 1 )
Hi! Thanks for the code, but there’s apparently an error and I don’t know how to fix it.
When typing “make all” it says “error in main.c lines 32 and 35 O_CREAT undeclared”.
Can you show me how to fix it?