Process creation using exec family functions

The exec family of system calls are used to replace the process image.

It means, when you create a new process using fork(), the child process will be similar to parent process.

How do you change the child process?

We change them by using exec() system calls. We shall see how we do that.

Below are the system calls for exec() family.

int execl(char* path, char* arg0, char* arg1,…, char* argn, NULL)
int execlp(char* file, char* arg0, char* arg1,…, char* argn, NULL)
int execv(char* path, char* argv[])
int execvp(char* file, char* argv[])

As you can see above, “exec” is appended with “l” or “p” or “v” or “e”.

e: It is an array of pointers that points to environment variables.
l: l is for the command line arguments passed a list to the function.
p: p is the path environment variable.
v: v is for the command line arguments.

execl stands for execute and leave. It means the process will get executed and terminated by execl.

How does exec() will work on a high level?

exec() will load the program into the memory. It will replace the current process image with a new one.

Notes:

1. exec() will not return anything upon success.
2. It will return some value, if it fails.
3. The arguments for exec() will be passed in argv[] of main().
4. Arguments passed via exec()appear in the argv[] of the main() function.
5. Below is the header file used for exec family functions.

#include <unistd.h>

Example 1: Simple execl example

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com

#include <unistd.h>

int main()
{ 
	printf("Files in Directory are:\n");
	execl("/bin/ls","ls", "-l",0);
}

Output:

Files in Directory are:
total 168
drwx------@  4 aj staff    128 Jul 12  2018 Applications
drwx------@ 25 aj staff    800 Apr  7 09:02 Desktop
drwx------@ 27 aj staff    864 Mar 26 07:44 Documents
drwx------@ 11 aj  staff    352 Apr  7 11:07 Downloads

Example 2: Simple execlp example

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com

// int execlp(const char *file, const char *arg, ...);
// it will search for the command from the path mentioned in PATH variable 
// else the default path ":/bin:/usr/bin"
#include <unistd.h>

int main()
{ 
  execlp("ls", "ls", "-l", NULL);
/*
*  execlp will search for the executable name "ls"
*  in the PATH variable and if in PATH variable is not defined
*  it will check in the default path ":/bin:/usr/bin".
*/
}

Example 3: Simple execvp example

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com

// int execvp(const char *file, char *const argv[]);
// it will search for the command from the path mentioned in PATH variable 
// else the default path ":/bin:/usr/bin"

// file: it is the program to execute
// argv: argument vector.
// The first argument, should point to the filename of the file being executed.
// The array of pointers must be terminated by a NULL pointer. 
#include <unistd.h>

int main()
{ 
  char *const cmd[] = {"ls", "-l", NULL};
  execvp(cmd[0], cmd);

}

Example 4: Simple execv example

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com

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

// it will search for the command from the path mentioned in PATH variable 
// else the default path ":/bin:/usr/bin"

// path: it is the program to execute
// argv: argument vector.
// The first argument, should point to the filename of the file being executed.
// The array of pointers must be terminated by a NULL pointer. 

#include <unistd.h>

int main()
{ 
  char *const argv[] = {"/bin/ls", "-l", NULL};
  execv(argv[0], argv);

}
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *