// program description:// This program written in BACI simulates the studying and// drinking coffee of students.int amtOfStudents; // how many students there areint mugsPerPot; // how many mugs each pot fillsint timesToFill; // how many times to fill pot in simulationint potToFillNext; // which pot to fill nextint fillCount =0; // how many times has pot been filled constint amtOfPots = 5; // how many pots are thereint mugsLeft[ amtOfPots ];int gettingFilled[ amtOfPots ];semaphore outputMutex; // one person can write to screensemaphore potNeedsFilling; // signal when student should fillsemaphore potMutex; // only one should get the pot5semaphore checkMutex; // for checking if coffee is leftsemaphore filledPot; // for checking if pot has coffeesemaphore confirm; // for changing which pot to fill// student who fills the coffee potvoid studentFiller() {int potToFill;// continue to fill as long as the user wantedwhile ( 1 && fillCount < timesToFill ) {// sleep until the pot needs filling and then wait on pot accesswait( potNeedsFilling );potToFill = potToFillNext;signal( confirm );wait( potMutex );mugsLeft[ potToFill ] = mugsPerPot; // fill up the potfillCount = fillCount + 1; // increment fill countwait( outputMutex );cout << "Pot " << potToFill << " filled" << endl;signal( outputMutex );gettingFilled[ potToFillNext ] = 0; // pot getting filledsignal( potMutex ); // release access to the potsignal( filledPot ); // signify that the pot was filled}}// normal studying studentvoid student( int id ) {int i; // used in for loopint temp;// continue to drink coffee as long as the user wantedwhile ( 1 && fillCount < timesToFill ) {temp = -1;// wait for access to the pot6wait( potMutex );// if the pot is empty and it is not currently getting filledfor ( i = 0; i < amtOfPots; i = i + 1 ) {if ( mugsLeft[ i ] == 0 && gettingFilled[ i ] == 0 ) {wait( outputMutex );cout << "Student " << id << " had the pot filled" << endl;signal( outputMutex );gettingFilled[ i ] = 1; // show pot about to fillpotToFillNext = i;signal( potNeedsFilling ); // wake up the pot fillerwait( confirm ); // ensure stnt has pot number}}// release access to the potsignal( potMutex );// if the last filling ended up being the last one then breakif ( fillCount >= timesToFill ) {signal( filledPot );break;}// wait for access to the potwait( potMutex );// as long as the pot is empty keep loopingdo {for ( i = 0; i < amtOfPots && temp == -1; i = i + 1 ) {if ( mugsLeft[ i ] > 0 ) {temp = i;}}signal( potMutex ); // release access to the potif ( fillCount < timesToFill ) {wait( filledPot ); // wait until pot has stuff in it}else {signal( filledPot );7break;}wait( potMutex ); // wait on the pot again}while ( mugsLeft[ temp ] == 0 );// drink a mug of coffeemugsLeft[ temp ] = mugsLeft[ temp ] - 1;if ( mugsLeft[ temp ] != 0 ) {signal( filledPot ); // check if pot empty for next guy}wait( outputMutex );if ( fillCount < timesToFill ) {cout << "Student " << id << " drank a cup of coffee" << endl;}signal( outputMutex );// release access to the potsignal( potMutex );}}// main functionvoid main() {int i; // used in for loopinitialsem( outputMutex, 1 ); // initialize output sempahoreinitialsem( potNeedsFilling, 0 ); // does the pot need fillinginitialsem( potMutex, 1 ); // access to the coffee potinitialsem( checkMutex, 1 ); // for checking mugsLeftinitialsem( filledPot, 1 ); // is coffee leftinitialsem( confirm, 1 ); // is coffee leftcout << "How many students should there be ( no more than 17 ): ";cin >> amtOfStudents;cout << "How many mugs should each pot of coffee fill: ";cin >> mugsPerPot;8cout << "How many times should the coffee pot be filled: ";cin >> timesToFill;// fill up all the pots before startingfor ( i = 0; i < amtOfPots; i = i + 1 ) {mugsLeft[ i ] = mugsPerPot;gettingFilled[ i ] = 0;}// begin the appropriate number of studentscobegin {studentFiller();for ( i = 0; i < amtOfStudents; i = i + 1 ) {student( i );}}cout << "Simulation Finished!" << endl;}