Question: Fork () in Linux Outline Write a C program that uses the fork() system call to create a total of 9 processes (including the parent
Fork () in Linux
Outline
Write a C program that uses the fork() system call to create a total of 9 processes (including the parent process). Your program must print the pids of all the processes created to a file.
- To get the pid of the current process use the getpid() function.
-
- Print the output in a text file named output.txt or take a screenshot for the result.
- Create 9 processes.
/**
* You can verify your answer by counting
* the number of unique processes which are output
* by the call to getpid() - which is 3 unique processes.
* Note: The statement 'printf("%d ",getpid());' prints
* the pid of the current process.
* Process tree: In the current program, the root P1 has a child P2,
* and P2 has a child P3.
*/
#include
#include /* This header file has the definition for pid_t
type*/
int main()
{
pid_t pid;
printf("%d ",getpid()); /*This Will print the root*/
pid = fork();
printf("%d ",getpid());
if(pid == 0) {
fork();
printf("%d ",getpid());
}
else {
/*Do Nothing*/
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
