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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. How to Add or Remove Ports when firewalld is Disabled
  2. How to Integrate CentOS/RHEL system into an AD Domain with LDAP/Kerberos/SSSD
  3. Linux OS Service ‘sshd’
  4. How to disable Ctrl+Alt+Del causing system reboot in CentOS/RHEL 6
  5. How to Use iptables instead of firewalld on CentOS/RHEL 7 and 8
  6. Beginners Guide to C Structures: Definition, creation and manipulation
  7. groupmems Command Examples in Linux
  8. CentOS/RHEL: /tmp mount point not automatically mounting when added in /etc/fstab
  9. How to Access VNC Server Through A Web Browser in CentOS/RHEL
  10. CentOS / RHEL 7 : Configuring NTP using chrony

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright