Exec System Call in C

29/12/2020
Chưa phân loại
The exec family has many functions in C. These C functions are basically used to run a system command in a separate process that the main program and print the output.

In this article, I am going talk about the exec family of functions and show you how to use each one of these exec family function in C. So, let’s get started.

C System Functions in Exec Family:

The exec function families are defined in the header unistd.h. So, you must use this header on the C program where you want to use these functions.

The available exec functions along with their function parameters are given below:

  • int execl(const char *path, const char *arg, …, NULL);
  • int execlp(const char *file, const char *arg, …, NULL );
  • int execv(const char *path, char *const argv[]);
  • int execvp(const char *file, char *const argv[]);
  • int execle(const char *path, const char *arg, …, NULL, char * const envp[] );
  • int execve(const char *file, char *const argv[], char *const envp[]);

Let’s see what each of these functions do and how to use them.

execl() System Function:

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. If any error occurs, then execl() returns -1. Otherwise, it returns nothing.

Syntax:

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

An example of the execl() system function is given below:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/ls";
  char *arg1 = "-lh";
  char *arg2 = "/home";
 
  execl(binaryPath, binaryPath, arg1, arg2, NULL);
 
  return 0;
}

I ran the ls -lh /home command using execl() system function. As you can see, the correct result is displayed.

execlp() System Function:

execl() does not use the PATH environment variable. So, the full path of the executable file is required to run it with execl(). execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is not needed.

Syntax:

int execlp(const char *file, const char *arg,, NULL );

We can rewrite the execl() example using the execlp() system function as follows:

#include <unistd.h>
 
int main(void) {
  char *programName = "ls";
  char *arg1 = "-lh";
  char *arg2 = "/home";
 
  execlp(programName, programName, arg1, arg2, NULL);
 
  return 0;
}

I only passed the command name ls, not the full path /bin/ls. As you can see, I got the same output as before.

execv() System Function:

In execl() function, the parameters of the executable file is passed to the function as different arguments. With execv(), you can pass all the parameters in a NULL terminated array argv. The first element of the array should be the path of the executable file. Otherwise, execv() function works just as execl() function.

Syntax:

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

We can rewrite the execl() example as follows:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/ls";
  char *args[] = {binaryPath, "-lh", "/home", NULL};
 
  execv(binaryPath, args);
 
  return 0;
}

As you can see, I am getting the correct output.

execvp() System Function:

Works the same way as execv() system function. But, the PATH environment variable is used. So, the full path of the executable file is not required just as in execlp().

Syntax:

int execvp(const char *file, char *const argv[]);

We can rewrite the execv() example as follows:

#include <unistd.h>
 
int main(void) {
  char *programName = "ls";
  char *args[] = {programName, "-lh", "/home", NULL};
 
  execvp(programName, args);
 
  return 0;
}

As you can see, the correct output is displayed.

execle() System Function:

Works just like execl() but you can provide your own environment variables along with it. The environment variables are passed as an array envp. The last element of the envp array should be NULL. All the other elements contain the key-value pairs as string.

Syntax:

int execle(const char *path, const char *arg,, NULL, char * const envp[] );

An example of the execle() system function is given below:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/bash";
  char *arg1 = "-c";
  char *arg2 = "echo "Visit $HOSTNAME:$PORT from your browser."";
  char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL};
 
  execle(binaryPath, binaryPath,arg1, arg2, NULL, env);
 
  return 0;
}

I passed two environment variables HOSTNAME and PORT to the execle() function. As you can see, I can access them from the executable /bin/bash.

execve() System Function:

Just like execle() you can provide your own environment variables along with execve(). You can also pass arguments as arrays as you did in execv().

Syntax:

int execve(const char *file, char *const argv[], char *const envp[]);

The execle() example can be rewritten as follows:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/bash";
  char *const args[] = {binaryPath, "-c", "echo "Visit $HOSTNAME:$PORT
    from your browser."", NULL};
  char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL};
 
  execve(binaryPath, args, env);
 
  return 0;
}

As you can see, we get the same output as in the execle() example.

So, that’s how you use the exec function family in C for system programming in Linux. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

ZFS Snapshots Tutorial

Snapshots are important whether you are running a simple virtual machine on your home computer or if it’s an enterprise...
28/12/2020

Linux System Call Tutorial with C

In our last article on Linux System Calls, I defined a system call, discussed the reasons one might use them in a program,...
29/12/2020

Python Virtualenv Tutorial

The headaches of dependency management are common to developers. One errant update requires hours of research to correct. ...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024