Program to replace process image using execl( )

There is a family of function which can be used for replacing the current process with a new process. They differ in the number of arguments and the way they start a new process. The various functions are execl, execlp, execle, execv and execvp. In this post we will write a Program to replace process image using execl( )

We will discuss the use of execl and the others can be used on similar lines to replace process image. The syntax for execl is

int execl(const char *path, const char *arg0, …, (char *)0);

The 1st argument is the PATH for the program while the last argument will be NULL.

Program: Program to replace process image using execl( ). Process ps will replace the image of current process

int main()
    printf("Before execl\n");
    printf("After execlp\n");


$gcc -o exec execl.c


Program to replace process image using execl( )

How it Works?

The program prints the first message and then calls execl. execl searches for the program ps in the PATH variable and executes it replacing the original program. Hence the second message doesn’t get printed. Also, It can be seen in the output that the name of the process running is ps and not the user process exec.

Normally the exec() of function is to replace the image of a child process. By default the child process duplicates the parent which means that the child will have exactly the same code as the parent. Now, if you want a different code in child then use execl() function.


Viva Questions on Program to replace process image using execl( )

Q1. exec() function returns only if ______________
Q2. The statements that appear after exec() function also executes – True/False.
Q3. How is excel() function different from system() function?

Relevant Programs

Program to create a child process using fork() system call
Process Synchronization using Semaphores
Inter-process communication using pipe() function
Difference between excel() and system() functions

Leave a Reply