Saturday, December 4, 2010

exec() Family System Calls

The exec family of functions shall replace the current process image with a new process image.

execl()

Synopsis

int execl(const char *path, const char *arg0, const char *arg1, const char *arg2, ... const char *argn, (char *) 0);

A command (with the path to the command e.g /bin/ls) and the required arguments are passed to the function."arg0" is the command to be executed. Function arguments are null terminated strings(The list of arguments is terminated by NULL).

Example

#include <unistd.h>
 main()
 {
    execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);
 }


execlp()

A command (path to the command will be resolved by the function itself) and the required arguments are passed to the function."arg0" is the command to be executed.

Example

#include <unistd.h>
 main()
 {
    execlp("ls", "ls", "-r", "-t", "-l", (char *) 0);
 }


execv()

Synopsis

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

Same as the execl function except now the arguments will be passed into a null terminated char pointer array."arg[0]" is the command to be executed.

Example

#include <unistd.h>
 main()
 {
    char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 };
  
    execv("/bin/ls", args);
 
 }

No comments :

Post a Comment