![]() |
VOOZH | about |
The exec family of functions is defined in the unistd.h header file. The exec functions are used to replace the current running process with a new process. It can be used to run a C program by using another C program.
There are many members in the exec family, which are shown below with examples.
Syntax:
where,
Example: To show how to use execvp() function in C. We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execvp() function in execDemo.c .
Now,create an executable file of EXEC.c using command
Now, create an executable file of execDemo.c using command
After running the executable file of execDemo.cby using command ./excDemo, we get the following output:
I AM EXEC.c called by execvp()
When the file execDemo.c is compiled, as soon as the statement execvp(args[0],args) is executed, this very program is replaced by the program EXEC.c. "Ending-----" is not printed because as soon as the execvp() function is called, this program is replaced by the program EXEC.c.
execv : This is very similar to execvp() function in terms of syntax as well. The syntax of execv() is as shown below:
Syntax:
Where,
Let us see a small example to show how to use execv() function in C. This example is similar to the example shown above for execvp() . We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execv() function in execDemo.c .
Now,create an executable file of EXEC.c using command
Now, create an executable file of execDemo.c using command
After running the executable file of execDemo.c by using command ./excDemo, we get the following output:
I AM EXEC.c called by execv()
These two also serve the same purpose but the syntax of them are a bit different which is as shown below:
Syntax:
Where,
These are also part of exec family of functions and they also serve the same purpose but the syntax of them are a bit different from all the above members of exec family. The syntaxes of both of them are shown below :
Syntax:
Where,
execvpe() and execle()