In: Computer Science
Write a C program that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes.
Child
PID
PPID X
0
13654 13653 5
1
13655 13653 10
2
13656 13653 15
3
13657 13653 20
4
13658 13653 25
5
13659 13653 30
6 13660
13653 35
7
13661 13653 40
8
13662 13653 45
9
13663 13653 50
#include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main() { // here only main process executes. No childs // get pid of main printf("%10s%10s%10s%10s\n", "Child", "PID", "PPID", "X"); int x =0; for (int i = 0; i < 10; i++) { if(fork() == 0) { printf("%10d%10d%10d%10d\n", i, getpid(), getppid(), 5 * (i+1)); return 0; } } for (int i = 0; i < 10; i++) { wait(NULL); } return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.