Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There are 2 programs ( mother . c and child.c ) that run 3 processes ( 1 mother, 2 children ) . mother.c installs a
There are programs motherc and child.c that run processes mother, children
mother.c installs a SIGUSR signal handler, and forks and execls both child processes. She then hangs out.
The child processes installs a SIGTERM signal handler, and look for the pin.
When a child finds it the child sends SIGUSR to its parent the mother process
When the mother receives SIGUSR it sends SIGTERM to all of its children, causing them to stop.
The mother then waits for both children, and quits herself.
Mother
forkexecl child
forkexecl child
searchingsearching
SIGUSR
SIGTERM
SIGTERM
stopsstops
stops
Assignment:
mother.c
Start with this code:
#include
#include
#include
#include
#include
const char FEMALENAMEARRAY
Anna
"Betty",
"Catherine",
"Dorothy"
;
const char MALENAMEARRAY
Eric
"Fred",
"Gerald",
"Hal"
;
const int NUMCHILDREN ;
const char childNameArrayCPtrNUMCHILDREN;
pidt childIdArrayNUMCHILDREN;
You may change the names, but please keep both arrays the same length!
main should do the following:
srandgetpid;
This resets the random number generator so that the program does not behave exactly the same each time.
Install an advanced handler to be called when SIGUSR is received. What will it do Keep reading!
printfMother: I will give $ to whomever finds my
"decorative pin in the pile of grass. Go
;
NUMCHILDREN times, make a child process. Put the process id of the child in array childIdArray
The child process should run the program named child In addition to giving the program name twice, pass the name of the child. Assuming int i is the loop variable telling which child to make, then one way to randomly choose the name of a child is with:
const char nameArray;
nameArray i FEMALENAMEARRAY : MALENAMEARRAY;
childNameArrayCPtri
nameArray rand
sizeofFEMALENAMEARRAY sizeofconst char
;
After making her children, the mother process just hangs out:
while some condition
sleep;
What should some condition be Well, you want the loop to stop after the process receives SIGUSR Therefore, what should your SIGUSR handler do
After finishing the loop, have the mother process send SIGTERM to all of her child processes. Have your parent to a wait for each child.
Finish with returnEXITSUCCESS;
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