Question
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
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.
- I posted a couple of examples on the lab 4 section. Please study these files.
- If you wish you can modify one of example files to 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: The root P1 has two children P2 and P3.
*/
#include
#include
type*/
int main()
{
pid_t pid;
printf("%d ",getpid()); /*This Will print the root*/
pid = fork();
printf("%d ",getpid());
if(pid == 0) {
/*Do Nothing*/
}
else {
fork();
printf("%d ",getpid());
}
return 0;
}
- There are two deliverables for this lab:
- The C code for lab 4.
- Print the output in a text file named output.txt or take a screenshot for the result.
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