Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

complete the stuents _ study _ in _ room ( long id ) function:void assess _ security ( ) / / guard assess room security

complete the stuents_study_in_room(long id) function:void assess_security()// 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
guard_state =1; // positive means in the room
int ms = rand_range(&seeds[0], MIN_SLEEP, MAX_SLEEP/2);
printf("\tguard assessing room security for %3d millisecs...
", ms);
millisleep(ms);
printf("\tguard done assessing room security
");
}
void guard_walk_hallway()// guard walks the hallway
{// details of this function are unimportant for the assignment
int ms = rand_range(&seeds[0], MIN_SLEEP, MAX_SLEEP/2);
printf("\tguard walking the hallway for %3d millisecs...
", ms);
millisleep(ms);
}
// this function contains the main synchronization logic for the guard
void guard_check_room()
{
// 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 assess_security() above)
// You will also need to properly maintain the global variable,
// guard_state. 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. num_students), 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 student_study_in_room(long 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, num_students. When done, students leave the
// room.
}
// guard thread function --- NO need to change this function !
void* guard(void* arg)
{
int i; // loop control variable
srand(seeds[0]); // seed the guard thread random number generator
// the guard repeatedly checks the room (limited to num_checks) and
// walks the hallway
for (i =0; i < num_checks; i++){
guard_check_room();
guard_walk_hallway();
}
return NULL; // thread needs to return a void*
}
// student thread function --- NO need to change this function !
void* student(void* arg)
{
long id =(long) arg; // determine thread id from arg
srand(seeds[id]); // seed this threads random number generator
// repeatedly study and do something else
while (1){
student_study_in_room(id);
do_something_else(id);
}
return NULL; // thread needs to return a void*
}
int main(int argc, char** argv)// the main function
{
int n; // number of student threads
pthread_t cthread; // guard thread
pthread_t* sthreads; // student threads
long i; // loop control variable
if (argc <4){
fprintf(stderr, "USAGE: %s num_threads capacity num_checks
", argv[0]);
return 0;
}
// TODO: get three input parameters, convert, and properly store
//using the atoi function to convert ASCII(string in this case) to integer in order to parse command line
int num_threads =atoi(argv[1]);
int capacity = atoi(argv[2]);
int num_checks = atoi(argv[3]);
fprintf("inputs are %d,%d,%d
", num_threads,capacity,num_checks);
// TODO: allocate space for the seeds[] array
// NOTE: seeds[0] is guard seed, seeds[k] is the seed for student k
// TODO: allocate space for the student threads array, sthreads
// Initialize global variables and semaphores
guard_state =0; // not in room (walking the hall)
num_students =0; // number of students in the room
semInitB(&mutex, 1); // initialize mutex
// TODO: complete the semaphore initializations, for all your semaphores
// initialize guard seed and create the guard thread
seeds[0]= START_SEED;
pthread_create(&cthread, NULL, guard, (void*) NULL);
for (i =1; i <= n; i++){
// TODO: create the student threads and initialize seeds[k], for
// each student k
}
pthread_join(cthread, NULL); // wait for guard thread to complete
for (i =0; i < n; i++){
// TODO: cancel each of the student threads (do man on pthread_cancel())
}
// TODO: free up any dynamic memory you allocated
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago