The exec() in C

In the previous article ‘The form() in C’ we’ve seen how we can create child processes in C. But we’ve seen that all the child processes have same instructions or source code as their parents. Yes we can you the conditions to execute specific instructions in parent and in child, but that’s not the best way to do it.
Here in this article we shall see how to make a process to run another process in it’s child process.
We achieve this in C with family of exec functions. Exec family functions replace the current running process with new process.
(We call it family of functions because there are many exec functions with different postfix which provide different functonalities).
Yeah you read it right. The purpose of exec family functions is not to create a children but to call another process. So we just call the fork() to create child and then in child process we call exec() to replace it with the process we want.
Working
Let’s see an example.
this is the code from previous article of fork(). Now to replace the child process with new process we use exec() in child process.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>int main() {
int pid;
pid = fork();if (pid == 0) {
printf("Hello from child(PID = %d)\n", getpid());
printf("Hello again.\n" );
} else {
printf("Hello from parent(PID = %d)\n", getpid());
}
}
We shall use execlp() function (one of the function from exec family). The syntax of that function is given below.
int execlp(const char *file, const char *arg, ...);The fileagument is the path name of an executable file to be executed. arg is the string we want to appear as argv[0] in the executable. Usually, argv[0] is just the file name of the executable, normally it's set to the same as file. After that we pass list of arguments and this list ends wil NULL argument so that the function know that argumetns are ended. Hence the above code will be changed to:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>int main() {
int pid;
pid = fork();if (pid == 0) {
printf("Hello from child(PID = %d)\n", getpid());
execlp("ls", "ls", "-al", NULL);
printf("Hello again.\n" );
} else {
printf("Hello from parent(PID = %d)\n", getpid());
}
}
In above code, in child process we call execlp(). Then it will replace the child process with ls process. Then ls will be executed. We are passing arguments to the ls functions which are -al.
And note that we also have another printf() after execlp(), but that won’t get executed because after execlp() the complete process gets replaces with new process.
The output of the program is given below:
sumeet at friday in ~/Projects/tmp
$ ./main
Hello from parent(PID = 5383)
Hello from child(PID = 5384)sumeet at friday in ~/Projects/tmp
$ total 60
drwxr-xr-x 4 sumeet sumeet 4096 Feb 4 18:48 .
drwxr-xr-x 17 sumeet sumeet 4096 Feb 3 21:27 ..
-rwxr-xr-x 1 sumeet sumeet 16808 Feb 4 18:48 main
-rw-r--r-- 1 sumeet sumeet 340 Feb 4 18:48 main.c
As we can see that parent and child process both got executed but the child process has also executed ls process. The statement(s) after execlp() also not got executed.
Exec family
Exec is a function of family and we’ve seen one function from the family which was execlp().
Actually there isn’t a function named exec(). There are many functions in exec family with different characters connected to the exec word.
After exec we can only use l or v. These characters are used to specify the way we are passing the arguments to the process we are calling. Example we are calling ls process with -al arguments. l stands for list here we pass arguments saperated with comma after the process name. Example execlp("ls", "ls", "-al", NULL);. v stands for vector and here we pass a saperate array of arguments.
After that p and e are optional arguments. We use p to seach file in PATH variable and then we are not need to provide fill file path. Example, both the commands below are same, one is with p and another is without.
execlp("ls", "ls", "-al", NULL);execl("/bin/ls", "ls", "-al", NULL);
The e character is used to pass the different environment to the new process which will be the array of character pointers.

execl, execlp, execle, execv, execvp, execvpe



