Question: Working on a project and we need to create four child processes using the fork system call in C code. The four child processes are

Working on a project and we need to create four child processes using the fork system call in C code. The four child processes are to be created one at a time and each is to be put to sleep for 10 seconds following their creation. Once all four child processes are created, the parent is then to be put to sleep for 10 seconds. After this, all five processes are to be terminated.

I'm having difficulty figuring out how to get all four children created and to stay alive until the parent process wakes up at the end. The code I have thus far is the following:

NUM_CHILDREN = 4;

/* Start children creation */

for (i = 0; i < NUM_CHILDREN; ++i){

if((pids[i] = fork()) < 0) {

perror("Error");

abort();

} else if (pids[i] == 0) {

printf("Child process %d is born, pid is %d ", i+1, getpid());

sleep(10);

}

}

sleep(10);

Any help would be greatly appreciated. Thanks.

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!