For more info visitBefore getting into the family structure of EXEC, let's first understand, what is exec. exec is a command of unix which overlays the currently running process area with the details of a new process, i.e. as soon as we run a new process using exec , the process area used by the running process in the memory is replaced by the new process. A child process is not created when exec is used.
We all know that unix shell provides an environment for running processes, but at the same time, this is also true that it itself is a process. Try running exec with any command or a script name from the shell prompt, you will notice that the shell shuts down abruptly. This is because the process area of the shell, which contains the details of currently running shell process, is replaced by a new process. The process doesn't return anything after successful execution of exec command, because the process itself is replaced by a new process in the memory. Normally, we use exec and its family in child processes, so that the parent process keeps on running smoothly.
This was about exec command which we use directly in unix. Now we also have some library functions and a system call in exec family. Infact, exec command use these library functions and system call to get its work done.
EXEC FAMILY:
There are six members in exec family, out of which five are library functions and one is a system call. Ultimately, all the library functions use the system call for their needs. The five library functions are
execl(char * pathname , const char *arg1.......const char *argn , (char *)0)
execv(char *pathname , char *const arg[])
execlp(char *filename , const char *arg1.......const char *argn , (char *)0)
execvp(char *filename , char *const arg[])
execle(char *pathname , const char *arg1.......const char *argn , (char *)0 , char * const env[])
execve(char *pathname , char *const arg[] , char * const env[])
Out of these six members, the last one, which is shown in bold, is a system call. We can use any of these members in our programs depending on our needs. There are some slight differences among the members.
execl: The first member, which is execl, takes the full pathname of the script or command as its first argument, the arguments to be passed for that script or command as its subsequent arguments, and at last, it also accepts a null pointer which marks the end of arguments. The following program explains, how execl can be used.
Suppose we have a simple script
myscript.sh
#!/bin/ksh
echo "Hello World"
Now, we will write a simple c program to understand the usage of execl
#include
int main()
{
execl("/home/user1/buff/myscript.sh","myscript.sh",(char *)0);
exit(0);
}
Compile and run this program. Output will be
Hello World.
execv: There is only a minor difference between execl and execv. After seeing the following example, you will get to know on your own.
#include
int main()
{
char *arg[]={"myscript.sh",(char *)0};
execv("/home/user1/buff/myscript.sh",arg);
exit(0);
}
Did you notice the difference? In execv, we can pass the arguments for the script in an array, instead of passing them individually.
execlp: It's same as execl with the only difference that we can pass the script name, instead of full pathname. The only thing is that, we will have to set the path of the directory in which the script to be run using exec is residing. execlp will automatically search the PATH environment for the full pathname of the script. Below is the example for execlp.
#include
int main()
{
execlp("myscript.sh","myscript.sh",(char *)0);
exit(0);
}
execvp: It it same as execv with the only difference that it takes the full pathname of the script directly from PATH environment variable. Only replace the individual arguments given in the execlp function by an array argument. I am not giving the example for this function, as it is very simple.
execle: The only difference between this function and all other functions is that it takes an extra environment variable in the parameters passed to it. Normally, when a child spawns, all the environment variables from parent are passed to the child. Sometimes, we may not want to pass all the environment variables, or we may only want to pass child specific environment variables. For doing this, we use execle function. Let's see an example for this function.
#include
int main()
{
char * env[]={"PATH=/home/user1/buff"};
execle("/home/user1/buff/myscript.sh","myscript.sh",(char *)0,env);
exit(0);
}
Here we are going to do a slight change in myscript.sh to see the change due to env parameter. The changed script is given below.
#!/bin/ksh
echo $PATH
echo "Hello World"
The output will be
/home/user1/buff
Hello World
The
PATH variable for the child script contains only that value which we had passed in the
env parameter.
execve: This is the last and the most important member of the exec family. It's the only system call in the family. Every other member of the family uses this system call to get its work done. The syntax and working is same as execle, with the only difference that it takes arguments for the script or command to be run, in an array.
No comments:
Post a Comment