Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete the piata and the child in C. An adult (the gameOfficial ) lets some number of children take turns hitting a pinata. With

Please complete the piata and the child in C. An adult (the gameOfficial) lets some number of children take turns hitting a pinata. With probability 1/20 (as computed by a pinata process) the pinata will bust, and the winning child get all the candy.

In this assignment we'll make 3 programs:

  1. A pinata program that waits until it receives SIGUSR1. It will then
    • send SIGUSR1 with probability 19/20, or
    • send SIGUSR2 with probability 1/20
    to the process that signalled it.
  2. A child program who hits the pinata.
    • It waits for its parent process (the gameOfficial) to send it SIGUSR1 and tell the child that it is that child's turn.
    • It then sends SIGUSR1 to the pinata-processes.
    • If it gets back SIGUSR1 it knows it failed to break the pinata, and it sends SIGUSR1 back to the gameOfficial.
    • However, if it gets back SIGUSR2 is knows it did break the pinata, and it sends SIGUSR2back to the gameOfficial.
  3. A gameOfficial program.
    • It asks the user for:
      • the number of pinata-whacking children to make
    • It makes all the processes
    • It regulates the game, and
    • It tells the processes to end after the game is finished.

Assignment:

I have written much of the gameOfficial program for you. All you have to do is fill in the juicybits.

gameOfficial.c

/*--------------------------------------------------------------------------* *---- ----* *---- gameOfficial.c ----* *---- ----* *---- This program controls version 2.0 of the pinata-whacking ----* *---- simulator. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *---- ----* *---- Version 2.0 Joseph Phillips ----* *---- ----* *--------------------------------------------------------------------------*/ /*----* *----* Common include sequence: *----*/ #include #include #include #include #include #include /*----* *----* Declaration of constants: *----*/ #define LINE_LEN 16 #define PINATA_PROG_NAME "pinata" #define CHILD_PROG_NAME "child" /*----* *----* Definition of global vars: *----*/ int shouldRun = 1; int isWaitingForTurn; pid_t* childPidArray; pid_t pinataPid; /*----* *----* Definition of global fncs: *----*/ /* PURPOSE: To change the global state so that the program knows both that * the current child process has finished its turn, and that it the game * is over (that child won). Ignores parameters. No return value. */ void turnOverStopGame (int sig, siginfo_t* info, void* data ) { shouldRun = 0; isWaitingForTurn = 0; } /* PURPOSE: To change the global state so that the program knows that the * current child process has finished its turn, but that it the game is * not yet over (that child lost). Ignores parameters. No return value. */ void turnOver(int sig, siginfo_t* info, void* data ) { isWaitingForTurn = 0; } /* PURPOSE: To reap all child processes that have already finished. Ignores * parameters. No return value. */ void child (int sig, siginfo_t* info, void* data ) { int status; pid_t finishedId; // YOUR CODE HERE } /* 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); } 
  1. pinata.c

    The pinata's job is to wait for a child to whack it and/or to wait to be told to stop:

    • Children whack the pinata by sending it SIGUSR1.
    • The gameOfficial tells the pinata to stop by sending SIGINT.

    Upon receipt of SIGUSR1 the pinata sees if it broke:

    • There is a 19/20 chance that the pinata survives, this causes SIGUSR1 to be sent back to the sender.
    • There is a 1/20 chance that the pinata breaks, this causes SIGUSR2 to be sent back to the sender.
    Hint: This code might be useful:
     int isBroken = (rand() % 20) == 19; int signalToSend = (isBroken ? SIGUSR2 : SIGUSR1); 

    Upon receipt of SIGINT the program should do:

     printf("Pinata stopping "); fflush(stdout); 
    and end.

    Your pinata program must:

    1. In main(), do:
      srand(getpid()); // This seeds the random number generator 
    2. Install a signal handler for SIGINT that causes the program to end. This signal handler should printf() a message that the pinata process is ending.
    3. Install a signal handler for SIGUSR1. The signal handler should see if the pinata broke by computing (rand() % 20) == 19.
      • If it is true then the pinata did break and SIGUSR2 should be sent back to the sender.
      • If it is false then the pinata survived and SIGUSR1 should be sent back to the sender.
    4. While the pinata is waiting to be signal()-ed it should not be doing much of anything. It should run code like:
      while (shouldRun) sleep(1); 
  2. 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:

    1. In main(), do:
      srand(getpid()); // This seeds the random number generator 
    2. 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); 
    3. 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.
    4. 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).
    5. Install signal handlers for signal SIGUSR2. This handler should printf() a boastful message and send signal SIGUSR2 to its parent (the gameOfficial).
    6. 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); 

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

In a hypothesis test, what does the power of the test measure?

Answered: 1 week ago