Question
child.c The child's job is to wait for signal numbered SIGUSR1 from its parent (the gameOfficial) This means it is its turn. Then the child
child.c
The child's job is to wait for signal numbered SIGUSR1 from its parent (the gameOfficial) This means it is its turn.
Then the child sends the signal SIGUSR1 to the pinata process.
If the child receives SIGUSR1 (from the pinata) that means it did not bust the pinata. It printf()-s a message of disappointment, and sends SIGUSR1 to its parent say that it has not yet won.
If the child receives SIGUSR2 (from the pinata) that means it did bust the pinata. It printf()-s a boastful message, and sends SIGUSR2 to its parent say that it has won.
Your child program must:
In main(), do:
srand(getpid()); // This seeds the random number generator
Check that there are 3 arguments total on the command line (the program's name and 2 others). If there are not then do:
fprintf(stderr,"Usage: %s", argv[0] ); exit(EXIT_FAILURE);
Install a signal handler for signal SIGINT to be told when to quit. This signal handler should printf() a message that the child process is ending and actually stop the program.
Install a signal handler for signal SIGUSR1.
If this signal comes from its parent then it should printf() a message that acknowledges that it is its turn and it should send SIGUSR1 to the pinata.
If this signal comes from the pinata process then it should printf() a message of disappointment and send signal SIGUSR1 to its parent (the gameOfficial).
Install signal handlers for signal SIGUSR2. This handler should printf() a boastful message and send signal SIGUSR2 to its parent (the gameOfficial).
While the child is waiting to be signal()-ed it should not be doing much of anything. It should run code like:
while (shouldRun) sleep(1);
/* PURPOSE: To simulate the pinata-whacking game. Ignores command line * parameters. Returns EXIT_SUCCESS to OS on success or EXIT_FAILURE * otherwise. */ int main () { // I. Application validity check: // II. Do simulation: // II.A. Get simulation parameters: int numChildren; char line[LINE_LEN]; // II.A.1. Get 'numChildren' (must be greater than or equal to 1): // YOUR CODE HERE // II.B. Prepare game: // II.B.1. Initialize 'childPidArray': childPidArray = (pid_t*)calloc(numChildren,sizeof(pid_t)); // II.B.2. Install signal handlers: struct sigaction sa; // YOUR CODE HERE // II.C. Launch child processes: // II.C.1. Launch pinata process: pinataPid = /* REPLACE THIS ZERO -> */ 0; if (pinataPid == -1) { fprintf(stderr,"Your OS is being fork()-bombed! :( "); exit(EXIT_FAILURE); } if (pinataPid == 0) { // YOUR CODE HERE TO LAUNCH PINATA_PROG_NAME fprintf(stderr,"Could not find program %s! :( ",PINATA_PROG_NAME); exit(EXIT_FAILURE); } // II.C.2. Launch pinata-whacking child process(es): int i; for (i = 0; i < numChildren; i++) { childPidArray[i] = /* REPLACE THIS ZERO -> */ 0; if (childPidArray[i] == -1) { fprintf(stderr,"Your OS is being fork()-bombed! :( "); exit(EXIT_FAILURE); } if (childPidArray[i] == 0) { char numText[LINE_LEN]; snprintf(line,LINE_LEN,"%d",pinataPid); snprintf(numText,LINE_LEN,"%d",i); // YOUR CODE HERE TO LAUNCH CHILD_PROG_NAME WITH 'line' AS THE FIRST ARG AND 'numText' AS THE 2ND ARG fprintf(stderr,"Could not find program %s! :( ",CHILD_PROG_NAME); exit(EXIT_FAILURE); } } // II.D. Play game: // II.D.1. Wait a sec' for all child processes to compose themselves: sleep(1); // II.D.2. Each iteration tells does one turn of one pinata-whacking // child process: int currentChild = 0; while (1) { printf("Child %d's turn: ",currentChild); isWaitingForTurn = 1; // YOUR CODE HERE TO SEND 'SIGUSR1' TO 'childPidArray[currentChild]' while (isWaitingForTurn) sleep(3); if ( !shouldRun ) break; currentChild++; if (currentChild >= numChildren) currentChild = 0; } printf("Child %d won! ",currentChild); // II.E. Clean up after game: // II.E.1. Tell all processes to end themselves: for (currentChild = 0; currentChild < numChildren; currentChild++) { // YOUR CODE HERE TO SEND 'SIGINT' TO 'childPidArray[currentChild]' sleep(1); } // YOUR CODE HERE TO SEND 'SIGINT' TO 'pinataPid' sleep(1); // II.E.2. Clean up memory: free(childPidArray); // III. Finished: return(EXIT_SUCCESS); }
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