fork and exec method use together to execute Shell Commands | Operating System

A parent process should create a child process which will execute command “ls -l” using execlp() and will create its child process. The newly created child process should execute system call command “cat hello.txt” 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.

fork and exec method use together to execute Shell Commands | Operating System

 

Solution : In previous post we have use system() function to execute Shell Commands throught C program. In this program I have used execlp() method to execute Linux /Unix Commands .In this solution we have also use waitpid() method in which each parent will wait for its child util child execution.This program uses fork() and execlp() together with waitpid() function as each parent should wait for its child execution. Problem given with following images which will clearify the question.

execlp():In this function p stands for this execlp method it accept program name.

Syntax : int execlp(const char *file, const char *arg, …….);

waitpid():This function can be used to wait for a specific child process to exit  with repect to Child pid.


Syntax: pid_t waitpid(pid_t pid, int *child_status, int options);

Code :-

/*Program From Nullpointer.in*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid1,pid2,pid3;
pid1=fork();
if(pid1 == 0){
pid2=fork();
printf("n");
if(pid2==0){
pid3=fork();
if(pid3==0){
printf("n****I AM pid3 CHILD PROCESS for whoami Command****n");
printf("The process id is %d and ppid is %d nn whoami output:nn",getpid(),getppid());
execlp("whoami","whoami",(char *)0);
printf("n");
}
else{
waitpid(pid3,0,0);
printf("n****I AM pid 2 CHILD PROCESS for cat Command****n");
printf("The process id is %d and ppid is %d nn cat cat.txt output:nn",getpid(),getppid());
execlp("cat","cat","hello.txt",(char *)0);
}
}
else
{
waitpid(pid2,0,0);
printf("n****I AM pid 1 CHILD PROCESS for ls Command****n");
printf("The process id is %d and ppid is %d nn ls -l output:nn",getpid(),getppid());
execlp("ls","ls","-l",(char *)0);
}
}
else{
waitpid(pid1,0,0);
printf("n======>I AM THE PARENT PROCESS<======n");
printf("The process id is %d and gid is %d nn",getpid(),getuid());
}
return 0;
}

Output on Shell/Terminal :




****I AM pid3 CHILD PROCESS for whoami Command****
The process id is 29068 and ppid is 29067

whoami output:

om

****I AM pid 2 CHILD PROCESS for cat Command****
The process id is 29067 and ppid is 29066

cat cat.txt output:

Hello Programmer,this Test File for cat command...!!!

****I AM pid 1 CHILD PROCESS for ls Command****
The process id is 29066 and ppid is 29065

ls -l output:

total 80
-rwxrwxr-x. 1 om om 6482 Nov 23 17:16 a.out
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Desktop
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Documents
drwxr-xr-x. 3 om om 4096 Nov 23 15:46 Downloads
-rw-rw-r--. 2 om om 583 Nov 23 15:50 fork.c
-rw-rw-r--. 1 om om 115 Nov 23 15:33 fork.out
-rw-rw-r--. 1 om om 54 Nov 23 16:52 hello.txt
drwxrwxr-x. 2 om om 4096 Nov 23 12:59 java
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Music
-rw-r--r--. 1 om om 1215 Nov 23 2012 myfork.c
-rw-rw-r--. 1 om om 11747 Nov 23 16:41 myforkoutput
-rw-rw-r--. 1 om om 957 Nov 23 16:55 perfect.txt
-rw-rw-r--. 2 om om 583 Nov 23 15:50 pfork.c
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Pictures
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Public
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Templates
drwxr-xr-x. 2 om om 4096 Nov 23 12:29 Videos

======>I AM THE PARENT PROCESS<======
The process id is 29065 and gid is 1000