Sunteți pe pagina 1din 7

exec family of functions

Simplistically, in UNIX, you have the concept of processes and programs. A process is something
in which a program executes.The simple idea behind the UNIX "execution model" is that there
are two operations you can do.

The first is to fork(), which creates a brand new process containing a duplicate of the current
program, including its state. There are a few differences between the processes which allow them
to figure out which is the parent and which is the child.

The second is to exec() , which replaces the program in the current process with a brand new
program.

From those two simple operations, the entire UNIX execution model can be constructed. The use
of fork() and exec() exemplifies the spirit of UNIX in that it provides a very simple way to start
new processes.

The fork() call makes a near duplicate of the current process, identical in almost every way
(not everything is copied over, for example, resource limits in some implementations, but the
idea is to create as close a copy as possible). One process calls fork() while two processes
return from it - sounds bizarre but it's really quite elegant
The new process (called the child) gets a different process ID (PID) and has the the PID of the
old process (the parent) as its parent PID (PPID).

Because the two processes are now running exactly the same code, they need to be able to tell
which is which - the return code of fork() provides this information - the child gets 0, the parent
gets the PID of the child (if the fork() fails, no child is created and the parent gets an error
code). That way, the parent knows the PID of the child and can communicate with it, kill it, wait
for it and so on (the child can always find its parent process with a call to getppid()).

The exec() call replaces the entire current contents of the process with a new program. It
loads the program into the current process space and runs it from the entry point.

The initial argument for these functions is the name of a file that is to be executed The const char
*arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of
as arg0, arg1, ..., argn.
Together they describe a list of one or more pointers to null terminated strings that represent the
argument list available to the executed program. The first argument, by convention, should point
to the filename associated with the file being executed. The list of arguments must be terminated
by a null pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated
strings that represent the argument lis available to the new program. The first argument, by
convention, should point to the filename associated with the file being executed.
The array of pointers must be terminated by a null pointer.
The execle() and execvpe() functions allow the caller to specify the Environment of th executed
program via the argument envp. The envp Argument is an array of pointers to null-terminated
strings and must Be terminated by a null pointer. The other functions take the Environment for
the new process image from the external variable Environ in the calling process.

In C It can be used to run a C program by using another C program. It comes under the header
file unistd.h. There are many members in the exec family which are shown below with examples.

 execvp: Using this command, the created child process does not have to run the same
program as the parent process does. The exec type system calls allow a process to run any
program files, which include a binary executable or a shell script.

Syntax:
int execvp (const char *file, char *const argv[]);

file: points to the file name associated with the file being executed.
argv: is a null terminated array of character pointers.

Let us see a small 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 .

//EXEC.c
#include<stdio.h>
#include<unistd.h>

int main()
{
int i;

printf("I am EXEC.c called by execvp() ");


printf("\n");

return 0;
}
//execDemo.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
/*All statements are ignored after execvp() call as
this whole
process(execDemo.c) is replaced by another process
(EXEC.c)
*/
printf("Ending-----");
return 0;
}

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 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:
int execv(const char *path, char *const argv[]);

path:Should point to the path of the file being executed.


argv[]: is a null terminated array of character pointers.
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.cand we will replace the execDemo.c with EXEC.c by calling
execv() function in execDemo.c .

//EXEC.c

#include<stdio.h>
#include<unistd.h>

int main()
{
int i;

printf("I am EXEC.c called by execv() ");


printf("\n");
return 0;
}

//execDemo.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);

/*All statements are ignored after execvp() call as this


whole
process(execDemo.c) is replaced by another process
(EXEC.c)
*/
printf("Ending-----");

return 0;
}

After running the executable file of execDemo.c by using command. /excDemo, we get the
following

Output:

I AM EXEC.c called by execv()

 execlp and execl : These two also serve the same purpose but the syntax of of them are
a bit different which is as shown below:

Syntax:
 int execlp(const char *file, const char *arg,.../* (char *) NULL */);

 int execl(const char *path, const char *arg,.../* (char *) NULL */);

file:file name associated with the file being executed

const char *arg and ellipses: describe a list of one or more pointers to null-terminated
Strings that represent the argument list available to the executed program.

//EXEC.c

#include<stdio.h>
#include<unistd.h>

int main()
{
int i;

printf("I am EXEC.c called by execlp() ");


printf("\n");
return 0;
}

//execDemo.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execlp(args[0],args);

/*All statements are ignored after execlp() call as this


whole
process(execDemo.c) is replaced by another process
(EXEC.c)
*/
printf("Ending-----");
return 0;
}
After running the executable file of execDemo.c by using command. /excDemo, we get the
following

Output:

I AM EXEC.c called by execlp()

 execvpe and execle : These two also serve the same purpose but the syntax of them
are a bit different from all the above members of exec family. The synatxes of both of them
are shown below:

Syntax:
 int execvpe(const char *file, char *const argv[],char *const envp[]);

Syntax:

 int execle(const char *path, const char *arg, .../*, (char *) NULL,
 char * const envp[] */);

The syntaxes above shown has one different argument from all the above exec members,
i.e.
char * const envp[]: allow the caller to specify the environment of the executed program
via the argument.

envp: This argument is an array of pointers to null-terminated strings and must be


terminated by a null pointer. The other functions take the environment for the new process
image from the external variable environ in the calling process

Functions in the exec() family have different behaviors:

 l : arguments are passed as a list of strings to the main()


 v : arguments are passed as an array of strings to the main()
 p : path/s to search for the new running program
 e : the environment can be specified by the caller
You can mix them, therefore you have:

 int execl(const char *path, const char *arg, ...);


 int execlp(const char *file, const char *arg, ...);
 int execle(const char *path, const char *arg, ..., char * const envp[]);
 int execv(const char *path, char *const argv[]);
 int execvp(const char *file, char *const argv[]);
 int execvpe(const char *file, char *const argv[], char *const envp[]);

For all of them the initial argument is the name of a file that is to be executed.

S-ar putea să vă placă și