Practice creating processes and signal handling

Assignment Help C/C++ Programming
Reference no: EM133334746

Systems

Assignment: Purpose:

To practice creating processes and signal handling.

Computing
Please ssh into one of the following:

cdmlsrvprd01.dpu.depaul.edu
or use your own Un*x machine (e.g. Linux or MacOS)

Overview:
We will finish two programs:

caller.c will make NUM_CHILDREN child processes. Each child process will run responder. Then parent process will send either SIGUSR1 or SIGUSR2 a randomly-chosen child. The child will either respond with SIGUSR1 or SIGUSR2. If the parent receives a different signal than it sent then it records that as a "flip". The parent then signals a different child process. It continues doing this for 60 seconds. Then it tells all child processes to stop by sending them SIGTERM. At the end it prints each flip probability of each child, and how many times the child actually did flip its signal.

Each child process runs responder. It is given a probability on its command line. When it is signaled with either SIGUSR1 or SIGUSR2, it sends the other signal to its parent with the given probability. It stops altogether when it receives SIGTERM.

Assignment 1:

/*-------------------------------------------------------------------------*
 *---									---*
 *---		caller.c						---*
 *---									---*
 *---	    This file defines a program that launches child processes	---*
 *---	and sends signals to them.					---*
 *---									---*
 *---	----	----	----	----	-----	----	----	----	---*
 *---									---*
 *---	Version 1a					Joseph Phillips	---*
 *---									---*
 *-------------------------------------------------------------------------*/

/*
 *	Compile with:
 *	$ g++ caller.c -o caller
 */
#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<signal.h>
#include	<unistd.h>
#include	<wait.h>

const int	NUM_CHILDREN	= 4;

const int	NUM_SECS_TO_RUN	= 60;

const int	TEXT_LEN	= 16;

#define		CHILD_PROCESS	"responder"

pid_t 		childPidArray[NUM_CHILDREN];

float		probabilityArray[NUM_CHILDREN];

int		countArray[NUM_CHILDREN];

int		shouldRun	= 1;

int		lastSignal	= SIGUSR1;

// PURPOSE: To respond to SIGALRM by telling the process running this program
// to stop. Ignores 'sigNum'. No return value.
void sigAlarmHandler (int sigNum
)
{
// YOUR CODE HERE
}

// PURPOSE: To send 'sigNum' to the child indexed by 'childIndex'.
// No return value.
void signalChild (int childIndex,
int sigNum
)
{
sleep(1);
printf("Sending %s to %d\n",
((sigNum == SIGUSR1) ? "SIGUSR1" : "SIGUSR2"),
childIndex
);
// YOUR CODE HERE
}

// ONE OR TWO FUNCTIONS HERE

// PURPOSE: To install the signal handler(s). No parameters. No return
// value.
void installSignalHandlers
()
{
struct sigaction act;

// YOUR CODE HERE
}

// PURPOSE: To initialize 'probabilityArray[]' and 'countArray[]'. No
// parameters. No return value.
void initializeProbAndCountArrays
()
{
int index;

for (index = 0; index < NUM_CHILDREN; index++)
{
probabilityArray[index] = ((float)(rand() % 256)) / 256.0;
countArray[index] = 0;
}
}

// PURPOSE: To launch 'NUM_CHILDREN' child processes, each of which runs
// program "responder". No parameters. No return value.
void launchChildren ()
{
int index;

// YOUR CODE HERE
}

// PURPOSE: To send 'SIGTERM' to all 'NUM_CHILDREN' child processes. No
// parameters. No return value.
void tellChildrenToStop
()
{
int index;

for (index = 0; index < NUM_CHILDREN; index++)
{
// YOUR CODE HERE
printf
("Child %d: prob %g, count %d\n",
index,probabilityArray[index],countArray[index]
);
}
}

int main ()
{
// I. Application validity check:

// (Nothing to do)

// II. Run program:
srand(getpid());

// II.A. Install signal handler(s):
installSignalHandlers();

// II.B. Initialize arrays and launch children:
initializeProbAndCountArrays();
launchChildren();

// II.C. Send initial signals:
// YOUR CODE HERE
signalChild(rand() % NUM_CHILDREN,lastSignal);

// II.D. Do the program:
while (shouldRun)
sleep(1);

// II.E. Tell children to stop:
tellChildrenToStop();

// III. Finished:
return(EXIT_SUCCESS);
}

a. sigAlarmHandler()
Change shouldRun so that this program leaves the while loop and main(), and quits.

b. signalChild()
Make this function send signal sigNum to the child indexed by childIndex.

c. Either 1 or 2 functions to handle SIGUSR1 and SIGUSR2

These function(s) must be advanced handlers. It/They must:

Use childPidArray[] to find the index of the child. (Call it childIndex.)
Increment countArray[childIndex] if the signal that was receive was not lastSignal
Do this:
int nextChildIndex;

printf("Received %s from %d\n\n",
((sigNum == SIGUSR1) ? "SIGUSR1" : "SIGUSR2"),
childIndex
);

do
{
nextChildIndex = rand() % NUM_CHILDREN;
}
while (nextChildIndex == childIndex);

lastSignal = theSignalThatWasJustReceived;
signalChild(nextChildIndex,lastSignal);

d. installSignalHandlers()
Installs all signal handlers. Be careful! Some are simple, some are advanced.

e. initializeProbAndCountArrays()
Initializes probabilityArray[] and countArray[]. Already done for you.

f. launchChildren()
Have a loop that makes NUM_CHILDREN child processes and puts their process ids in childPidArray[]. Each child process should run CHILD_PROCESS with the flip probability given as a command line argument. To convert the floating point number to a string, say:

char text[TEXT_LEN];

snprintf(text,TEXT_LEN,"%g",probabilityArray[index]);

g. tellChildrenToStop()
Has a loop that sends SIGTERM to all child processes. (Important! Do not leave any Zombies, now!)

h. main()
Mostly written for you except that you must tell the Operating System to send SIGALRM to you NUM_SECS_TO_RUN seconds in the future.

Assignment 2: responder.c

/*-------------------------------------------------------------------------*
*--- ---*
*--- responder.c ---*
*--- ---*
*--- This file defines a program that responds to SIGUSR1 and ---*
*--- SIGUSR2 from its parents. ---*
*--- ---*
*--- ---- ---- ---- ---- ---- ---- ---- ---- ---*
*--- ---*
*--- Version 1a Joseph Phillips ---*
*--- ---*
*-------------------------------------------------------------------------*/

/*
* Compile with:
* $ gcc responder.c -o responder
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <wait.h>

int shouldRun = 1;

float flipProb = 0.0;

void sigTermHandler (int sigNum
)
{
// YOUR CODE HERE
}

// YOUR CODE HERE


int main (int argc,
char* argv[]
)
{
struct sigaction act;

srand(getpid());

// YOUR CODE HERE


while (shouldRun)
sleep(1);

return(EXIT_SUCCESS);
}

a. sigTermHandler()
Change shouldRun so that this program leaves the while loop and main(), and quits.

b. Write 1 or 2 simple signal handlers to handle SIGUSR1 and SIGUSR2.

Do this:
float thisProb= ((float)(rand() % 256)) / 256.0;
If (flipProb > thisProb) then the signal has flipped. Print "Flipped!\n" and send the opposite signal to the parent process.
If (flipProb <= thisProb) then the signal has not flipped. Print "Same\n" and send the same signal to the parent process.

c. main()
This should do several things:

Make sure that the command line argument for the flip probability is given on the command line. If it is not, then do: fprintf(stderr,"Missing probability argument"); exit(EXIT_FAILURE);
Convert the command line argument from a string into a float, and put its value in flipProb. The value of flipProb should be between [0.0 .. 1.0]. If it is not, then do: fprintf(stderr,"Bad probability argument"); exit(EXIT_FAILURE);
Install all signal handlers.

Reference no: EM133334746

Questions Cloud

What are the global implications for the product or service : What are the global implications for the product or service you are marketing? What are the global implications of your marketing strategy and recommendations?
Fluoridzated water available in community water : Healthy People 2020 set a goal that 80% of the people in the U.S. have an optimal amount of fluoridzated water available in their community's water.
What would be an excellent example of an offer letter : What would be an excellent example of an offer letter to Alana Perez offering her the role and stating the pay and benefits What would be appropriate pay
Evaluate david approach to starting his carpet-cleaning : Evaluate David's approach to starting his carpet-cleaning business. Why was he not able to reach his goal of $120,000 in sales?
Practice creating processes and signal handling : CSC 407 Systems, DePaul University To respond to SIGALRM by telling the process running this program - Practice creating processes and signal handling
Discuss what clinical flowchart : Discuss what a clinical flowchart would be. Explain how a clinical flowchart can contribute to improved functionality in a healthcare setting.
What are false positives and false negatives : What are false positives and false negatives? How do these occur and why are they problematic?What are some of the advantages of group decision making
Improve health status and health system : Why is community and constituency engagement important in initiatives to improve health status and the health system?
Develop a short policy analysis related to a public health : Develop a short, written policy analysis related to a public health issue of your choice. Refer to the resource "Guidelines for Preparing a Brief Policy

Reviews

len3334746

2/9/2023 11:48:06 PM

We will finish two programs: caller.c will make NUM_CHILDREN child processes. Each child process will run responder. Then parent process will send either SIGUSR1 or SIGUSR2 a randomly-chosen child. The child will either respond with SIGUSR1 or SIGUSR2. If the parent receives a different signal than it sent then it records that as a "flip". The parent then signals a different child process. It continues doing this for 60 seconds. Then it tells all child processes to stop by sending them SIGTERM. At the end it prints each flip probability of each child, and how many times the child actually did flip its signal. Each child process runs responder. It is given a probability on its command line. When it is signaled with either SIGUSR1 or SIGUSR2, it sends the other signal to its parent with the given probability. It stops altogether when it receives SIGTERM.

Write a Review

C/C++ Programming Questions & Answers

  Determines how many breaths a person has had in their life

Develop and test a program that determines how many breaths and how many heartbeats a person has had in their life. The average respiration (breath) rate of people varies with age.

  Write a program that accepts as input a string variable

Write a program that accepts as input a string variable called strFirstName, then another string variable called strLastName, and an integer variable called intAge.

  Differentiate between declaration and definition in cpp

Differentiate between declaration and definition in C++. What is cloning? 3) Describe the main characteristics of static functions.

  Write a c program that prompts the user to enter some data

Write a C program that prompts the user to enter some data regarding some clients. The user should prompt for client account number (a UNIQUE positive integer between 1 and 1000).

  Calculate distances and map a route

The GPS navigation system uses the graph and geometric algorithms to calculate distances and map a route. One of the geometric problems is the closest-pair problem. Given a set of points, the closest-pair problem is to find the two points that are..

  Create class has three pieces of information as data members

Create a class called Date in C++ that includes three pieces of information as data members: month (type int), day (type int) and yaer (type int).

  Prompts the user to enter an integer

Write the code that prompts the user to enter an integer between 1 and 20 (including 1 and 20), reads the value using cin, and then prints the value that they entered in a statement that begins with "You entered a ". Save this version in a separate l..

  Hierarchical control using player/stage simulator

In the Nested Hierarchical Controller, the PLAN module consists of a Mission Planner, a Navigator, and a Pilot. The Pilot connects with the ACT module to execute the drive and steering commands that cause the robot to move along a path segment.

  Generate an icmp echo request packet

You need two VMs on the same LAN. From VM A, you ping an IP X. This will generate an ICMP echo request packet. If X is alive, the ping program will receive

  Write a programme on credit card number check

Credit Card Number Check. The last digit of a credit card number is the check digit, which protects againsttranscription errors such as an error in a single digit or switching two digits

  Calculate the task start and finish times

Using the chart given in the attachment(Personnel_Shortfall.docx), with durations shown in weeks, if there are 16 total people available for the project (assume all 16 are qualified to work on any of the tasks and assume all tasks start and finish..

  Implement a class which uses a vector to store the queue

Implement a class which uses a vector to store the queue. Be mindful of performance, such that if the queue is empty, the size of the underlying vector is "rese

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd