• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to use execl (example included)

by admin

execl is one of the families of exec calls that act as a front end to the execve. The following screenshot refers to man execl:

using execl

The arguments for these seven exec functions are difficult to remember. The letters in the function names help somewhat. The letter p means that the function takes a filename argument and uses the PATH environment variable to find the executable file. The letter l means that the function takes a list of arguments and is mutually exclusive with the letter v, which means that it takes an argv[] vector. Finally, the letter e means that the function takes an envp[] array instead of using the current environment.

Differences among the seven exec functions

In the post “Using execve” we saw how it can be used to launch a new process and also pass arguments to it. execl also launches a new process replacing the current one. The syntax of execl is:

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

Arguments:
path: Path to the executable that needs to he executed by execl.
arg…: Series of pointers to the arguments that need be passed to executable.

In execve we had to pass an array of pointers as arguments, but in execl we can directly pass the pointers as arguments. These arguments should be NULL terminated.

Example

1. Let us write a simple program to print the arguments passed to it.

# vi hello.c
#include <stdio.h>
main(int argc,char *argv[],char *envp[]){

printf("Filename: %s\n",argv[0]);
printf("%s %s\n",argv[1],argv[2]);
}

2. By convention the first argument should always be the filename and we will be following the same. Let us compile this and name the executable “hello”

# cc hello.c -o hello

3. Now let us write a program to run the executable “hello” using execl.

# vi execl.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main() {
char *temp,*temp1,*temp2;
temp="hello";  //filename
temp1="Funny"; 
temp2="world";

execl("hello",temp,temp1,temp2,NULL);
printf("Error");
}

4. Compile the code and execute it:

# cc execl.c -o execl
./execl 

Output:

Filename: hello
Funny world

Thus the program could successfully run the executable “hello” and also pass the arguments to it. Also, note that execl did not return to the calling function, else it would have printed the “Error” statement after the call to execl.

Filed Under: C, Linux

Some more articles you might also be interested in …

  1. dvc: Data Version Control (like git for data)
  2. make: Nothing to be done for `default’
  3. tar Command Examples in Linux
  4. showmount Command Examples in Linux
  5. virt-xml-validate Command Examples in Linux
  6. diff: command not found
  7. What are sparse files in Linux
  8. service Command Examples in Linux
  9. CentOS / RHEL : How to exclude kernel or other packages from getting updated using YUM Versionlock Plugin
  10. c99 – Compiles C programs according to the ISO C standard

You May Also Like

Primary Sidebar

Recent Posts

  • “glab issue” Command Examples
  • “glab auth” Command Examples
  • “glab alias” Command Examples
  • gixy Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright