137k views
5 votes
Write a C program using the fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. For example, if 8 is passed as a parameter on the command line, the child process will output 8, 4, 2, 1. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a positive integer is passed on the command line. Following are some running examples, assuming the compiled program named b.out:

User Maxtuzz
by
5.1k points

1 Answer

5 votes

Answer:

Program using the fork()

Step-by-step explanation:

// code to create three child

// process of a parent

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

// Driver code

int main()

{

int pid, pid1, pid2;

// variable pid will store the

// value returned from fork() system call

pid = fork();

// If fork() returns zero then it

// means it is child process.

if (pid == 0) {

// First child needs to be printed

// later hence this process is made

// to sleep for 3 seconds.

sleep(3);

// This is first child process

// getpid() gives the process

// id and getppid() gives the

// parent id of that process.

printf("child[1] --> pid = %d and ppid = %d\\",

getpid(), getppid());

}

else {

pid1 = fork();

if (pid1 == 0) {

sleep(2);

printf("child[2] --> pid = %d and ppid = %d\\",

getpid(), getppid());

}

else {

pid2 = fork();

if (pid2 == 0) {

// This is third child which is

// needed to be printed first.

printf("child[3] --> pid = %d and ppid = %d\\",

getpid(), getppid());

}

// If value returned from fork()

// in not zero and >0 that means

// this is parent process.

else {

// This is asked to be printed at last

// hence made to sleep for 3 seconds.

sleep(3);

printf("parent --> pid = %d\\", getpid());

}

}

}

return 0;

}