Answered step by step
Verified Expert Solution
Question
1 Approved Answer
complete the stuents _ study _ in _ room ( long id ) function:void assess _ security ( ) / / guard assess room security
complete the stuentsstudyinroomlong id function:void assesssecurity guard assess room security
details of this function are unimportant for the assignment
NOTE: we have own the mutex when we first enter this routine
guardstate ; positive means in the room
int ms randrange&seeds MINSLEEP, MAXSLEEP;
printftguard assessing room security for d millisecs...
ms;
millisleepms;
printftguard done assessing room security
;
void guardwalkhallway guard walks the hallway
details of this function are unimportant for the assignment
int ms randrange&seeds MINSLEEP, MAXSLEEP;
printftguard walking the hallway for d millisecs...
ms;
millisleepms;
this function contains the main synchronization logic for the guard
void guardcheckroom
TODO: complete this routine to execute the behavior of the guard,
with proper synchronization. You will need to determine how many
students are in the room to perform the appropriate action.
Depending on the number of students, you will either:
properly wait to enter, if some students are there
clear out the room, if capacity or more number of students
assess the security of the room, if no students are there
see function assesssecurity above
You will also need to properly maintain the global variable,
guardstate. Once you have performed the appropriate action,
with proper synchronization, the guard will leave the room.
Remember, that whenever you access or change a global variable
eg numstudents you need to insure you are doing so in a
mutually exclusive fashion, for example, by calling
semWait&mutex
this function contains the main synchronization logic for a student
void studentstudyinroomlong id
TODO: complete this routine to execute the behavior of a student,
with proper synchronization. You will need to determine if the
guard is in the room. You will also need to synchronize with the
guard, to clear out the room, and, to allow a possible waiting
guard to enter. At the proper place, you will call the function
study above. You will also need to properly maintain the
global variable, numstudents. When done, students leave the
room.
guard thread function NO need to change this function
void guardvoid arg
int i; loop control variable
srandseeds; seed the guard thread random number generator
the guard repeatedly checks the room limited to numchecks and
walks the hallway
for i ; i numchecks; i
guardcheckroom;
guardwalkhallway;
return NULL; thread needs to return a void
student thread function NO need to change this function
void studentvoid arg
long id long arg; determine thread id from arg
srandseedsid; seed this threads random number generator
repeatedly study and do something else
while
studentstudyinroomid;
dosomethingelseid;
return NULL; thread needs to return a void
int mainint argc, char argv the main function
int n; number of student threads
pthreadt cthread; guard thread
pthreadt sthreads; student threads
long i; loop control variable
if argc
fprintfstderr "USAGE: s numthreads capacity numchecks
argv;
return ;
TODO: get three input parameters, convert, and properly store
using the atoi function to convert ASCIIstring in this case to integer in order to parse command line
int numthreads atoiargv;
int capacity atoiargv;
int numchecks atoiargv;
fprintfinputs are ddd
numthreads,capacity,numchecks;
TODO: allocate space for the seeds array
NOTE: seeds is guard seed, seedsk is the seed for student k
TODO: allocate space for the student threads array, sthreads
Initialize global variables and semaphores
guardstate ; not in room walking the hall
numstudents ; number of students in the room
semInitB&mutex, ; initialize mutex
TODO: complete the semaphore initializations, for all your semaphores
initialize guard seed and create the guard thread
seeds STARTSEED;
pthreadcreate&cthread, NULL, guard, void NULL;
for i ; i n; i
TODO: create the student threads and initialize seedsk for
each student k
pthreadjoincthread NULL; wait for guard thread to complete
for i ; i n; i
TODO: cancel each of the student threads do man on pthreadcancel
TODO: free up any dynamic memory you allocated
return ;
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