Create Childs which use System Call Unix Linux Commands C program | Operating System

A parent process should create a child process which will execute system call command “ls”
and will create its child process. The newly created child process should execute system call command “cat” and create a child process. The newly created process should execute system call”whoami” and then terminate. Every parent process should wait for terminating its child process.

Solution :-
Following program demostrate you how to create processes using fork() method and how to  use system call to execute commands on shell terminal. Using simple wait() method parent wait for child execution till partent pause state. just try it out yourself/

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
pid_t pid;
char cmd[256];
pid=fork(); //Child create by fork() method
if(pid == 0)
{
printf("n****I AM THE CHILD PROCESS for ls Command****n");
printf("The process id is %d and ppid is %d nn ls -l output:nn",getpid(),getppid());
sprintf(cmd,"ls -l");
system(cmd);
printf("n");
pid=fork();
wait(NULL);
if(pid==0)
{
printf("n****I AM THE CHILD PROCESS for cat Command****n");
printf("The process id is %d and ppid is %d nn cat cat.txt output:nn",getpid(),getppid());
sprintf(cmd,"cat cat.txt ");
system(cmd);
pid=fork();
wait(NULL);
if(pid==0)
{
printf("n****I AM THE CHILD PROCESS for whoami Command****n");
printf("The process id is %d and ppid is %d nn whoami output:nn",getpid(),getppid());
sprintf(cmd,"whoami");
system(cmd);
printf("n");

}
}
}
else if(pid>0)
{
wait(NULL);
printf("n======>I AM THE PARENT PROCESS<======n");
printf("The process id is %d and gid is %d nn",getpid(),getuid());

}
return 0;
}