Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the functions of the exec family of system calls like exec and execve?

I have been following a system programming course recently and I came through the system calls exec() and execve(). So far I cannot find any difference between these two, Even the Wikipedia does not give a clear explanation, so is there a difference between exec() and execve().

And someone please could give brief descriptions about exec family system calls such as execl(), execv(), execle(), execvp().

like image 703
buddhi weerasinghe Avatar asked Dec 29 '13 08:12

buddhi weerasinghe


People also ask

What is the difference between exec and execve?

The only difference between the above system calls is with the parameters. Is that the case? If so, is the ultimate outcome of all the exec family system calls to execute a program (with different parameters)? Actually, the only system call is execve(2) and all other exec* functions are wrapping it.

What does the exec () family of system calls do?

The exec family of system calls replaces the program executed by a process. When a process calls exec, all code (text) and data in the process is lost and replaced with the executable of the new program.

What is the difference between exec and system?

With system() you can invoke any command, whereas with exec(), you can only invoke an executable file. Shell scripts and batch files must be executed by the command shell. Basically they are entirely different used for different purposes. Moreover exec() replaces the calling process, and does not return.

Why is exec () system call used?

The exec() system call is used to replace the current process image with the new process image. It loads the program into the current space, and runs it from the entry point. So the main difference between fork() and exec() is that fork starts new process which is a copy of the main process.


2 Answers

There is no exec system call -- this is usually used to refer to all the execXX calls as a group. They all do essentially the same thing: loading a new program into the current process, and provide it with arguments and environment variables. The differences are in how the program is found, how the arguments are specified, and where the environment comes from.

  • The calls with v in the name take an array parameter to specify the argv[] array of the new program. The end of the arguments is indicated by an array element containing NULL.

  • The calls with l in the name take the arguments of the new program as a variable-length argument list to the function itself. The end of the arguments is indicated by a (char *)NULL argument. You should always include the type cast, because NULL is allowed to be an integer constant, and default argument conversions when calling a variadic function won't convert that to a pointer.

  • The calls with e in the name take an extra argument (or arguments in the l case) to provide the environment of the new program; otherwise, the program inherits the current process's environment. This is provided in the same way as the argv array: an array for execve(), separate arguments for execle().

  • The calls with p in the name search the PATH environment variable to find the program if it doesn't have a directory in it (i.e. it doesn't contain a / character). Otherwise, the program name is always treated as a path to the executable.

  • FreeBSD 5.2 added another variant: execvP (with uppercase P). This is like execvp(), but instead of getting the search path from the PATH environment variable, it's an explicit parameter to the function:

int execvP(const char *file, const char *search_path, char *const argv[]); 
like image 83
Barmar Avatar answered Sep 21 '22 08:09

Barmar


Use man exec and read:

The execv(), execvp(), and execvpe() functions provide an array of pointers to  null-terminated strings that represent the argument list 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.  

execv

int execv(const char *path, char *const argv[]); 

So you pass an array as parameters

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

Almost the same, but not as an array, but rather as a list of values (strings), followed by an array the designates the environment.

Here:

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

You are calling a file, without path, so it expects you to be already in the right path before calling.

Last but not least:

int execve(const char *filename, char *const argv[],                   char *const envp[]); 

Similar to previous one, but now you have two arrays, for arguments and environment variables.

like image 34
Noam Rathaus Avatar answered Sep 18 '22 08:09

Noam Rathaus