Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the different versions of exec used for in C and C++?

Tags:

c++

c

exec

These are all the versions of exec that can be used in C (and C++)

execl execle execlp execv execve execvp 

What's the difference between them? How do you know which one to use?

like image 904
node ninja Avatar asked Apr 24 '11 09:04

node ninja


People also ask

What does exec () do in C?

The exec family of functions replaces the current running process with a new process. It can be used to run a C program by using another C program. It comes under the header file unistd.

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.

Why do we use exec?

Exec functions are used when you want to execute (launch) a file (program). and how does it work. They work by overwriting the current process image with the one that you launched. They replace (by ending) the currently running process (the one that called the exec command) with the new process that has launched.

What are the parameters of the exec () system call?

In execl() system function takes the path of the executable binary file (i.e. /bin/ls) as the first and second argument. Then, the arguments (i.e. -lh, /home) that you want to pass to the executable followed by NULL. Then execl() system function runs the command and prints the output.


2 Answers

The differences are combinations of:

  1. L vs V: whether you want to pass the parameters to the exec'ed program as

    • L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe()
    • V: as an array of char* execv(), execve(), execvp(), and execvpe()

    The array format is useful when the number of parameters that are to be sent to the exec'ed process are variable -- as in not known in advance, so you can't put in a fixed number of parameters in a function call.

  2. E: The versions with an 'e' at the end let you additionally pass an array of char* that are a set of strings added to the spawned processes environment before the exec'ed program launches. Yet another way of passing parameters, really.

  3. P: The versions with 'p' in there use the environment variable PATH to search for the executable file named to execute. The versions without the 'p' require an absolute or relative file path to be prepended to the filename of the executable if it is not in the current working directory.

like image 145
Christo Avatar answered Sep 23 '22 20:09

Christo


Opengroup are one of the best general references for core c/c++ functions.

The docs for exec* are here: http://pubs.opengroup.org/onlinepubs/009695399/functions/environ.html

like image 31
IanNorton Avatar answered Sep 23 '22 20:09

IanNorton