Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Looking for a solution to the following code: A mother kangaroo has lost some number of her baby kangroos (joeys) in a shopping mall. To

Looking for a solution to the following code:

A mother kangaroo has lost some number of her baby kangroos ("joeys") in a shopping mall. To be easy to find she will stay outside. Each joey was lost in its own unique location, and thus must make its own way out of the mall.

To find its way out of the mall, a joey must hop along the corridor it is in and then choose the proper direction when the corridor ends (either left or right) by sending either LEFT_TURN_SIGNAL (SIGUSR1) or RIGHT_TURN_SIGNAL (SIGUSR2) to the mall. If it chooses incorrectly then the mall will instantly tell it whether or not it has made the correct turn.

-If it has, then it hops along the next corridor until it has to make another decision -If it has not, then it chooses the other direction.

In this we'll make 3 programs:

1. A mamaKangaroo program which will: a. Ask for the number of joeys to make b. Ask for a random number seed c. Install a SIGCHLD handler d .Start a mall process e .Start the joey process(es) f. Wait for all joeys to find their way back to her pouch g. Send SIGINT to the mall process, printf() a parting message, free() its memory, and then quit.

2. A joey program that guesses at its initial direction (which it keeps in nextTurn). Thereafter, a. When it receives CORRECT_TURN_SIGNAL it printf()s a happy message, hops down the next hall, chooses a next direction (either LEFT_TURN_SIGNAL or RIGHT_TURN_SIGNAL), and sends that to the mall process. b. When it receives WRONG_TURN_SIGNAL it printf()s a chastised message, chooses the other direction (opposite of what is currently in nextTurn), and sends that to the mall process. c. When it receives SIGINT it printf()s a celebratory message, and goes on to stop the program.

3. A mall program that waits until it receives either LEFT_TURN_SIGNAL (SIGUSR1) or RIGHT_TURN_SIGNAL (SIGUSR2) from a joey process. It will then compare that move with the move the joey should have taken. -If the joey took the wrong move then it: a. printf()s a message that tells this. b. sends WRONG_TURN_SIGNAL back to the joey process. -If the joey took the right move then it: a. printf()s a message that tells this. b. increments joeysNumSuccessfulTurns[joey] c. If joeysNumSuccessfulTurns[joey] is greater than or equal to NUM_TURNS_MUST_SUCCESSFULLY_MAKE then it has made it out of the mall. It printf()s a message that tells this and sends SIGINT to the joey process. d. However, if joeysNumSuccessfulTurns[joey] is less than NUM_TURNS_MUST_SUCCESSFULLY_MAKE then it still has at least one more turn to make. It printf()s a message telling how many more moves the joey should make and sends CORRECT_TURN_SIGNAL back to the joey process. When all of the joeys have made it out of the mall then the mama kangagroo process will send this mall process SIGINT. This mall process will then printf() a parting message, free() its memory, and then quit.

When all of the joeys have made it out of the mall then the mama kangagroo process will send this mall process SIGINT. This mall process will then printf() a parting message, free() its memory, and then quit.

4. These programs will work correctly for one 1 joey process. They will work correctly most of the time for 2 joey processes. Sometimes they will work correctly, and sometimes they will not, for 3 joey processes. For 4 joey processes or more the joey processes will finish while others will be awaiting feedback from the mall process. Meanwhile, the mall process will be awaiting choices from some of the joey processes.

Why?

Sample Output (1):

$ ./mamaKangaroo 1 1 Joey # 0: Which direction? Decisions, Decisions! I'll guess left. Mall: Joey # 0 made the wrong turn! (left) Joey # 0: Oops! Let us try the other direction: right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 7 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 6 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 0 made the wrong turn! (left) Joey # 0: Oops! Let us try the other direction: right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 5 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 0 turned correctly! (left) Mall: Joey # 0 must make 4 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 0 turned correctly! (left) Mall: Joey # 0 must make 3 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 0 made the wrong turn! (left) Joey # 0: Oops! Let us try the other direction: right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 2 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 1 more correct turn. Joey # 0: Yay! On to the next turn; I'll guess right. Mall: Joey # 0 made the wrong turn! (right) Joey # 0: Oops! Let us try the other direction: left. Mall: Joey # 0 turned correctly! (left) Mall: Joey # 0 got out of the mall and returned to its mama! Joey # 0: Yay! I'm safely in mommy's pouch! Mama: Joey #0 just entered my pouch! Mall: All the joeys have found their mama! Now the mall can finally close! 

Protocol Analysis Of Sample Output (1):

 mama mall joey | | fork()/exec() +-------------------->>| | | | | | fork()/exec() | +----------------------|--------------------->>| | | | | | LEFT_TURN_SIGNAL | Joey # 0: Which direction? Decisions, Decisions! I'll guess left. | |<<---------------------+ | | | | | WRONG_TURN_SIGNAL | Mall: Joey # 0 made the wrong turn! (left) | +--------------------->>| | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Oops! Let us try the other direction: right. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (right) | +--------------------->>| Mall: Joey # 0 must make 7 more correct turns. | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Yay! On to the next turn; I'll guess right. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (right) | +--------------------->>| Mall: Joey # 0 must make 6 more correct turns. | | | | | LEFT_TURN_SIGNAL | Joey # 0: Yay! On to the next turn; I'll guess left. | |<<---------------------+ | | | | | WRONG_TURN_SIGNAL | Mall: Joey # 0 made the wrong turn! (left) | +--------------------->>| | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Oops! Let us try the other direction: right. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (right) | +--------------------->>| Mall: Joey # 0 must make 5 more correct turns. | | | | | LEFT_TURN_SIGNAL | Joey # 0: Yay! On to the next turn; I'll guess left. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (left) | +--------------------->>| Mall: Joey # 0 must make 4 more correct turns. | | | | | LEFT_TURN_SIGNAL | Joey # 0: Yay! On to the next turn; I'll guess left. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (left) | +--------------------->>| Mall: Joey # 0 must make 3 more correct turns. | | | | | LEFT_TURN_SIGNAL | Joey # 0: Yay! On to the next turn; I'll guess left. | |<<---------------------+ | | | | | WRONG_TURN_SIGNAL | Mall: Joey # 0 made the wrong turn! (left) | +--------------------->>| | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Oops! Let us try the other direction: right. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (right) | +--------------------->>| Mall: Joey # 0 must make 2 more correct turns. | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Yay! On to the next turn; I'll guess right. | |<<---------------------+ | | | | | CORRECT_TURN_SIGNAL | Mall: Joey # 0 turned correctly! (right) | +--------------------->>| Mall: Joey # 0 must make 1 more correct turn. | | | | | RIGHT_TURN_SIGNAL| Joey # 0: Yay! On to the next turn; I'll guess right. | |<<---------------------+ | | | | | WRONG_TURN_SIGNAL | Mall: Joey # 0 made the wrong turn! (right) | +--------------------->>| | | | | | LEFT_TURN_SIGNAL | Joey # 0: Oops! Let us try the other direction: left. | |<<---------------------+ | | | | | SIGINT | Mall: Joey # 0 turned correctly! (left) | +--------------------->>| Mall: Joey # 0 got out of the mall and returned to its mama! | | | | | SIGCHLD | |<<--------------------|-----------------------* Joey # 0: Yay! I'm safely in mommy's pouch! | | (joey process ends) | | | | | SIGINT | Mama: Joey #0 just entered my pouch! +-------------------->>| | | | * Mall: All the joeys have found their mama! Now the mall can finally close! | (mall process ends) | | * (mamaKangaroo process ends) 

Sample Output (2):

$ ./mamaKangaroo 2 1 Joey # 1: Which direction? Decisions, Decisions! I'll guess right. Joey # 0: Which direction? Decisions, Decisions! I'll guess left. Mall: Joey # 0 made the wrong turn! (right) Mall: Joey # 1 made the wrong turn! (left) Joey # 1: Oops! Let us try the other direction: left. Joey # 0: Oops! Let us try the other direction: right. Mall: Joey # 0 turned correctly! (left) Mall: Joey # 0 must make 7 more correct turns. Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 7 more correct turns. Joey # 1: Yay! On to the next turn; I'll guess left. Joey # 0: Yay! On to the next turn; I'll guess right. Mall: Joey # 0 made the wrong turn! (left) Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 6 more correct turns. Joey # 1: Oops! Let us try the other direction: right. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 1 made the wrong turn! (left) Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 6 more correct turns. Joey # 0: Oops! Let us try the other direction: right. Joey # 1: Yay! On to the next turn; I'll guess right. Mall: Joey # 0 made the wrong turn! (right) Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 5 more correct turns. Joey # 1: Oops! Let us try the other direction: left. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 1 made the wrong turn! (left) Mall: Joey # 0 turned correctly! (left) Mall: Joey # 0 must make 5 more correct turns. Joey # 0: Oops! Let us try the other direction: right. Joey # 1: Yay! On to the next turn; I'll guess left. Mall: Joey # 0 made the wrong turn! (left) Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 4 more correct turns. Joey # 1: Oops! Let us try the other direction: right. Joey # 0: Yay! On to the next turn; I'll guess left. Mall: Joey # 1 made the wrong turn! (left) Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 4 more correct turns. Joey # 0: Oops! Let us try the other direction: right. Joey # 1: Yay! On to the next turn; I'll guess left. Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 3 more correct turns. Mall: Joey # 0 made the wrong turn! (left) Joey # 0: Yay! On to the next turn; I'll guess left. Joey # 1: Oops! Let us try the other direction: right. Mall: Joey # 1 made the wrong turn! (left) Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 3 more correct turns. Joey # 0: Oops! Let us try the other direction: right. Joey # 1: Yay! On to the next turn; I'll guess right. Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 2 more correct turns. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 2 more correct turns. Joey # 0: Yay! On to the next turn; I'll guess right. Joey # 1: Yay! On to the next turn; I'll guess right. Mall: Joey # 1 turned correctly! (right) Mall: Joey # 1 must make 1 more correct turn. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 must make 1 more correct turn. Joey # 0: Yay! On to the next turn; I'll guess right. Joey # 1: Yay! On to the next turn; I'll guess left. Mall: Joey # 1 made the wrong turn! (right) Joey # 0: Oops! Let us try the other direction: left. Mall: Joey # 0 made the wrong turn! (left) Mall: Joey # 1 turned correctly! (left) Mall: Joey # 1 got out of the mall and returned to its mama! Joey # 0: Yay! I'm safely in mommy's pouch! Joey # 1: Oops! Let us try the other direction: right. Mall: Joey # 0 turned correctly! (right) Mall: Joey # 0 got out of the mall and returned to its mama! Joey # 1: Yay! I'm safely in mommy's pouch! Mama: Joey #0 just entered my pouch! Mama: Joey #1 just entered my pouch! Mall: All the joeys have found their mama! Now the mall can finally close! 

kangarooHeaders.h

/*--------------------------------------------------------------------------* *---- ----* *---- kangarooHeaders.h ----* *---- ----* *---- This file declares constants and objects common to the ----* *---- Unix-signalling kangaroo simulation programs. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* // // Common include sequence: // #include #include #include #include #include #include #include // // Declaration of constants: // #define LINE_LEN 16 #define MALL_PROG_NAME "mall" #define JOEY_PROG_NAME "joey" #define MAMAS_NUM_JOEYS_CMD_LINE_INDEX ((int)1) #define MAMAS_RAND_SEED_CMD_LINE_INDEX ((int)2) #define MALLS_NUM_JOEYS_CMD_LINE_INDEX ((int)1) #define MALLS_RAND_SEED_CMD_LINE_INDEX ((int)2) #define MIN_MALL_CMD_LINE_PARAMS \ (MALLS_RAND_SEED_CMD_LINE_INDEX + 1) #define JOEYS_INDEX_CMD_LINE_INDEX ((int)1) #define JOEYS_MALL_PID_CMD_LINE_INDEX ((int)2) #define JOEYS_RAND_SEED_CMD_LINE_INDEX ((int)3) #define MIN_JOEY_CMD_LINE_PARAMS \ (JOEYS_RAND_SEED_CMD_LINE_INDEX + 1) #define MIN_NUM_LEGAL_JOEYS ((int)1) #define NUM_TURNS_MUST_SUCCESSFULLY_MAKE ((int)8) #define LEFT_TURN_SIGNAL SIGUSR1 #define RIGHT_TURN_SIGNAL SIGUSR2 #define WRONG_TURN_SIGNAL SIGUSR1 #define CORRECT_TURN_SIGNAL SIGUSR2 

mall.c

/*--------------------------------------------------------------------------* *---- ----* *---- mall.c ----* *---- ----* *---- This program implements the mall process, which gives the ----* *---- joeys processes feedback on how to leave. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* // // Header inclusion: // #include "kangarooHeaders.h" // // Definition of global vars: // // PURPOSE: To hold the number of joeys the user requested to make. int numJoeys = MIN_NUM_LEGAL_JOEYS - 1; // PURPOSE: To tell if the mama is still looking for joeys. int shouldStillRun = 1; // PURPOSE: To encode the unique sequence of left and right turns each joey // make to leave the mall. // 0 bit = left // 1 bit = right // Bits read from least to most significant. int* joeysPaths = NULL; // PURPOSE: To keep track of the number of successful turns each joey has // already made. int* joeysNumSuccessfulTurns = NULL; // PURPOSE: To hold the process ids of the joey processes. pid_t* joeysPids = NULL; // // Definition of global fncs: // // PURPOSE: To initialize most global structures given the 'argc' command // line parameters given in 'argv[]'. No return value. void initializeMostGlobals (int argc, char* argv[] ) { // I. Application validity check: if (argc < MIN_MALL_CMD_LINE_PARAMS) { fprintf(stderr,"Usage: %s ",MALL_PROG_NAME); exit(EXIT_FAILURE); } // II. Initialize globals: // II.A. Initialize 'numJoeys': numJoeys = 0; // <-- REPLACE with code that gets integer from 'argv[MALLS_NUM_JOEYS_CMD_LINE_INDEX]' if (numJoeys < MIN_NUM_LEGAL_JOEYS) { fprintf(stderr,"Illegal number of joeys. "); exit(EXIT_FAILURE); } // II.B. Initialize random number seed: int randNumSeed = 0; // <-- REPLACE with code that gets integer from 'argv[MALLS_RAND_SEED_CMD_LINE_INDEX]' srand(randNumSeed); // II.C. Initialize 'joeysPaths' and 'joeysNumSuccessfulTurns': joeysNumSuccessfulTurns = (int*)calloc(sizeof(int),numJoeys); joeysPaths = (int*)calloc(sizeof(int),numJoeys); joeysPids = (pid_t*)calloc(sizeof(pid_t),numJoeys); int numChoices = (1 << NUM_TURNS_MUST_SUCCESSFULLY_MAKE); int joey; for (joey = 0; joey < numJoeys; joey++) joeysPaths[joey] = rand() % numChoices; // III. Finished: } // PURPOSE: To return the index of the joey process 'joeyPid' in array // 'joeysPids[]'. int getJoeyIndex (int joeyPid ) { // I. Application validity check: // II. Find index of/for 'joeyPid': int joey; // II.A. Look for 'joeyPid' in 'joeysPids[]': for (joey = 0; joey < numJoeys; joey++) if ( (joeysPids[joey] == joeyPid) || (joeysPids[joey] == 0) ) break; // II.B. Complain if run out of array positions: if (joey >= numJoeys) { fprintf(stderr, "More joey Pids that number of joeys given on command line! " ); exit(EXIT_FAILURE); } // II.C. Record 'joeyPid' if not currently in 'joeysPids[]': if (joeysPids[joey] != joeyPid) joeysPids[joey] = joeyPid; // III. Finished: return(joey); } // PURPOSE: To return '1' ('true') if the joey 'joeyIndex' made the correct // turn with turn signal 'turnSignal', or to return '0' ('false') // otherwise. int wasJoeysTurnCorrect (int joeyIndex, int turnSignal ) { // I. Application validity check: // II. Return value: int didJoeyTurnRight = (turnSignal == RIGHT_TURN_SIGNAL); int shouldHaveTurnedRight = (joeysPaths[joeyIndex] >> joeysNumSuccessfulTurns[joeyIndex] ) & 0x1; return(didJoeyTurnRight == shouldHaveTurnedRight); }  // YOUR CODE HERE to add either two advanced signal handlers, for // 'LEFT_TURN_SIGNAL' and 'RIGHT_TURN_SIGNAL' respectively, or add one that // can handle them both. // // If the joey took the wrong move then it: // printf()s a message that tells this. // sends WRONG_TURN_SIGNAL back to the joey process. // else if the joey took the correct move then it: // printf()s a message that tells this. // increments joeysNumSuccessfulTurns[joey] // If joeysNumSuccessfulTurns[joey] is greater than or equal to NUM_TURNS_MUST_SUCCESSFULLY_MAKE then // It printf()s a message that tells that it is out, // Sends SIGINT to the joey process. // else // It printf()s a message telling how many more moves the joey should make // Sends CORRECT_TURN_SIGNAL back to the joey process. // // You can get the index of a joey with: // int joeyIndex = getJoeyIndex(joeyProcessId) // // You can see if the Joey's turn was correct with: // if ( wasJoeysTurnCorrect(joeyIndex,receivedSignal) ) // goodMove(); // else // badMove();   // YOUR CODE HERE to add a SIGINT handler. It can be simple. // It printf()s a message and changes the global state so that the // while-loop in main() quits.  // PURPOSE: To do the program given the 'argc' command line parameters // pointed to by 'argv[]'. Returns 'EXIT_SUCCESS' to OS when program // naturally finishes. int main (int argc, char* argv[] ) { // I. Application validity check (done by 'initializeMostGlobals()'): // II. Sheppard joeys out of mall: // II.A. Initialize: initializeMostGlobals(argc,argv); // YOUR CODE HERE to install your 'LEFT_TURN_SIGNAL' and 'RIGHT_TURN_SIGNAL' handler(s) // YOUR CODE HERE to install your 'SIGINT' handler // II.B. Sheppard joeys out of mall: while (shouldStillRun) sleep(1); // II.C. Clean up afterward: free(joeysPids); free(joeysNumSuccessfulTurns); free(joeysPaths); // III. Finished: return(EXIT_SUCCESS); } 

joey.c

/*--------------------------------------------------------------------------* *---- ----* *---- joey.c ----* *---- ----* *---- This program implements the joey processes, which try to ----* *---- leave the mall. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* // // Header inclusion: // #include "kangarooHeaders.h" // // Definition of global vars: // // PURPOSE: To tell if the mama is still looking for joeys. int shouldStillRun = 1; // PURPOSE: To tell which joey this joey is. int joeyIndex; // PURPOSE: To tell the process id of the mall process. pid_t mallPid; // PURPOSE: To hold what the next turn will be. int nextTurn; // PURPOSE: To initialize most globals given the 'argc' command line // arguments pointed to by 'argv[]'. No return value. void initializeMostGlobals (int argc, char* argv[] ) { // I. Application validity check: if (argc < MIN_JOEY_CMD_LINE_PARAMS) { fprintf(stderr,"Usage: %s ",JOEY_PROG_NAME); exit(EXIT_FAILURE); } // II. Initialize globals: // II.A. Initialize 'joeyIndex': joeyIndex = 0; // <-- REPLACE with code that gets integer from 'argv[JOEYS_INDEX_CMD_LINE_INDEX]' // II.B. Initialize 'mallPid': mallPid = 0; // <-- REPLACE with code that gets integer from 'argv[JOEYS_MALL_PID_CMD_LINE_INDEX]' // II.C. Initialize random number generator: int seed = 0; // <-- REPLACE with code that gets integer from 'argv[JOEYS_RAND_SEED_CMD_LINE_INDEX]' srand(seed); // III. Finished: } // PURPOSE: To handle 'CORRECT_TURN_SIGNAL'. Ignores 'sig'. No return value. void correctTurnSignalHandler (int sig ) { // I. Application validity check: // II. nextTurn = ((rand() % 2) == 1) ? LEFT_TURN_SIGNAL : RIGHT_TURN_SIGNAL; printf("Joey # %d: Yay! On to the next turn; I'll guess %s. ", joeyIndex, (nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right" ); fflush(stdout); // YOUR CODE HERE to send 'nextTurn' to 'mallPid' // III. Finished: } // PURPOSE: To handle 'WRONG_TURN_SIGNAL'. Ignores 'sig'. No return value. void wrongTurnSignalHandler (int sig ) { // I. Application validity check: // II. nextTurn = (nextTurn == LEFT_TURN_SIGNAL) ? RIGHT_TURN_SIGNAL : LEFT_TURN_SIGNAL; printf("Joey # %d: Oops! Let us try the other direction: %s. ", joeyIndex, (nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right" ); fflush(stdout); // YOUR CODE HERE to send 'nextTurn' to 'mallPid' // III. Finished: } // PURPOSE: To note that the program may now finish (after receiving // 'SIGINT'). Ignores 'sig'. No return value. void sigIntHandler (int sig ) { printf("Joey # %d: Yay! I'm safely in mommy's pouch! ",joeyIndex); fflush(stdout); shouldStillRun = 0; } // PURPOSE: To install 'correctTurnSignalHandler()' as the signal handler for // 'CORRECT_TURN_SIGNAL' and 'wrongTurnSignalHandler()' as the handler for // 'WRONG_TURN_SIGNAL'. No parameters. No return value. void installTurnHandlers () { // I. Application validity check: // II. Install the handler: // YOUR CODE HERE // III. Finished: } // PURPOSE: To install 'sigIntHandler()' as the handler for 'SIGINT'. No // parameters. No return value. void installSigIntHandler () { // I. Application validity check: // II. Install handler: // II.A. Set up struct to specify the new action. // YOUR CODE HERE // III. Finished: } int main (int argc, char* argv[] ) { // I. Application validity check (done by 'initializeMostGlobals()'): // II. Try to leave mall: // II.A. Initialize globals: initializeMostGlobals(argc,argv); installTurnHandlers(); installSigIntHandler(); // II.B. Try to leave mall: nextTurn = ((rand() % 2) == 1) ? LEFT_TURN_SIGNAL : RIGHT_TURN_SIGNAL; printf("Joey # %d: Which direction? Decisions, Decisions! I'll guess %s. ", joeyIndex, (nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right" ); fflush(stdout); // YOUR CODE HERE to send 'nextTurn' to 'mallPid' while (shouldStillRun) sleep(1); // III. Finished: return(EXIT_SUCCESS); } 

mamaKangaroo.c

/*--------------------------------------------------------------------------* *---- ----* *---- mamaKangaroo.c ----* *---- ----* *---- This program simulates a mama kangaroo waiting for her ----* *---- joeys using UNIX signaling. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* // // Header inclusion: // #include "kangarooHeaders.h" // // Definition of global vars: // // PURPOSE: To hold the random number seed the user requested. int randomNumSeed; // PURPOSE: To hold the number of joeys the user requested to make. int numJoeys = MIN_NUM_LEGAL_JOEYS - 1; // PURPOSE: To hold the number of joeys that have not yet found their way // back to their mama's pouch. int numActiveJoeys = 0; // PURPOSE: To hold the process ids of the joey processes. pid_t* joeyPidArray = NULL; // PURPOSE: To hold the process id of the mall process. pid_t mallPid = -1; // // Definition of global fncs: // // PURPOSE: To initialize 'numJoeys' from the 'argc' commamnd line // parameters give in 'argv[]'. No return value. void initializeNumJoeys (int argc, char* argv[] ) { // I. Application validity check: // II. Initialize 'numJoeys': char line[LINE_LEN]; if (argc > MAMAS_NUM_JOEYS_CMD_LINE_INDEX) numJoeys = 0; // <-- REPLACE with code that gets integer from 'argv[MAMAS_NUM_JOEYS_CMD_LINE_INDEX]' while (numJoeys < MIN_NUM_LEGAL_JOEYS) { printf("Please enter the number of joeys that" " get lost in the shopping mall: " ); fgets(line,LINE_LEN,stdin); numJoeys = 0; // <-- REPLACE with code that gets integer from 'line' } // III. Finished: } // PURPOSE: To initialize 'randomNumSeed' from the 'argc' commamnd line // parameters give in 'argv[]'. No return value. void initializeRandomNumSeed (int argc, char* argv[] ) { // I. Application validity check: // II. Initialize 'randomNumSeed': char line[LINE_LEN]; if (argc > MAMAS_RAND_SEED_CMD_LINE_INDEX) randomNumSeed = 0; // <-- REPLACE with code that gets integer from 'argv[MAMAS_RAND_SEED_CMD_LINE_INDEX]' else { printf("Please enter the random number seed: "); fgets(line,LINE_LEN,stdin); randomNumSeed = 0; // <-- REPLACE with code that gets integer from 'line' } // III. Finished: } // PURPOSE: To handle signal 'SIGCHLD'. 'sig' is ignored because it is known // to be 'SIGCHLD'. No return value. void sigChldHandler (int sig ) { // I. Application validity check: // II. Search for the joey with process id equal to 'childPid': int status; pid_t childPid; int joey;  // YOUR CODE HERE that uses a 'while()' loop testing 'waitpid()' to // get *all* finished children. Each should be tested against // 'joeyPidArray[joey]' to see if it is that particular joey. // Decrements 'numActiveJoeys' for each stopped joey.  // III. Finished: } // PURPOSE: To install 'sigChldHandler()' as the SIGCHLD handler, which is // able to distinguish among which of the children has finished. No // parameters. No return value. void installSigChldHandler () { // I. Application validity check: // II. Install the handler: // YOUR CODE HERE // III. Finished: } // PURPOSE: To start the mall process. No parameters. No return value. void startMallProcess () { // I. Application validity check: // II. Start the mall process: mallPid = 0; // <-- REPLACE with code that returns pid of new process if (mallPid == -1) { fprintf(stderr,"Can't make any new processes! :( "); exit(EXIT_FAILURE); } if (mallPid == 0) { char numJoeysStr[LINE_LEN]; char randNumSeedStr[LINE_LEN]; snprintf(numJoeysStr,LINE_LEN,"%d",numJoeys); snprintf(randNumSeedStr,LINE_LEN,"%d",randomNumSeed); // YOUR CODE HERE that runs 'MALL_PROG_NAME' with command line parameters 'numJoeysStr' and 'randNumSeedStr'. fprintf(stderr,"Could not find the mall! :( "); exit(EXIT_FAILURE); } // III. Finished: } // PURPOSE: To start the joey processes. No parameters. No return value. void startJoeyProcesses () { // I. Application validity check: // II. Start the joey processes: int joey; joeyPidArray = (pid_t*)calloc(sizeof(pid_t),numJoeys); for (joey = 0; joey < numJoeys; joey++) { joeyPidArray[joey] = 0; // <-- REPLACE with code that returns pid of new process if (joeyPidArray[joey] == -1) { fprintf(stderr,"Can't make any new processes! :( "); exit(EXIT_FAILURE); } if (joeyPidArray[joey] == 0) { char indexStr[LINE_LEN]; char mallPidStr[LINE_LEN]; char randNumSeedStr[LINE_LEN]; snprintf(indexStr,LINE_LEN,"%d",joey); snprintf(mallPidStr,LINE_LEN,"%d",mallPid); snprintf(randNumSeedStr,LINE_LEN,"%d",randomNumSeed + joey); // YOUR CODE HERE that runs 'JOEY_PROG_NAME' with command line parameters 'indexStr', 'mallPidStr' and 'randNumSeedStr'. fprintf(stderr,"Could not find the joey! :( "); exit(EXIT_FAILURE); } numActiveJoeys++; } // III. Finished: } int main (int argc, char* argv[] ) { // I. Application validity check: // II. Do simulation: // II.A. Get simulation parameters: initializeNumJoeys(argc,argv); initializeRandomNumSeed(argc,argv); // II.B. Setup handlers: installSigChldHandler(); // II.C. Initialize processes: startMallProcess(); sleep(1); startJoeyProcesses(); // II.D. To do the simulation by waiting until all joeys have returned: while (numActiveJoeys > 0) sleep(1); // II.E. Clean up after game: // II.E.1. Stop the mall process: // YOUR CODE HERE to send 'SIGINT' to 'mallPid' // II.E.2. Release mem: free(joeyPidArray); // III. Finished: return(EXIT_SUCCESS); } 

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

Step: 3

blur-text-image

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

=+by your instructor) in the program you have received.

Answered: 1 week ago

Question

Discuss the key components of PMSs.

Answered: 1 week ago

Question

What is an interface? What keyword is used to define one?

Answered: 1 week ago

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago