Question: There are 2 programs ( mother . c and child.c ) that run 3 processes ( 1 mother, 2 children ) . mother.c installs a

There are 2 programs (mother.c and child.c) that run 3 processes (1 mother, 2 children).
mother.c installs a SIGUSR1 signal handler, and fork()s and execl()s 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 SIGUSR1 to its parent (the mother process).
When the mother receives SIGUSR1, it sends SIGTERM to all of its children, causing them to stop.
The mother then wait()s for both children, and quits herself.
Mother
|
|fork()/execl() child0
|---------------->|
||
|fork()/execl()| child1
|-----------------|----------->|
|||
|||
|||
|||
|(searching)(searching)
|||
|||
| SIGUSR1||
|<----------------||
|||
| SIGTERM ||
|---------------->||
|||
| SIGTERM ||
|-----------------|----------->|
|||
|(stops)(stops)
|
(stops)
Assignment:
mother.c
Start with this code:
#include
#include
#include
#include
#include
const char* FEMALE_NAME_ARRAY[]
={"Anna",
"Betty",
"Catherine",
"Dorothy"
};
const char* MALE_NAME_ARRAY[]
={"Eric",
"Fred",
"Gerald",
"Hal"
};
const int NUM_CHILDREN =2;
const char* childNameArrayCPtr[NUM_CHILDREN];
pid_t childIdArray[NUM_CHILDREN];
You may change the names, but please keep both arrays the same length!
main() should do the following:
srand(getpid());
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 SIGUSR1 is received. What will it do? Keep reading!
printf("Mother: \"I will give $20 to whomever finds my "
"decorative pin in the pile of grass. Go!\"
");
NUM_CHILDREN 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 ==0)? FEMALE_NAME_ARRAY : MALE_NAME_ARRAY;
childNameArrayCPtr[i]
= nameArray[ rand()%
(sizeof(FEMALE_NAME_ARRAY)/ sizeof(const char*))
];
After making her children, the mother process just hangs out:
while (/* some condition */)
sleep(1);
What should /* some condition */ be? Well, you want the loop to stop after the process receives SIGUSR1. Therefore, what should your SIGUSR1 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 return(EXIT_SUCCESS);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!