Open In App

Difference between system() and execl() call

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process and an operating system and it does not replace the image of the current process.

Syntax:

#include<stdio.h>
#include<stdlib.h>
int system(const char *command);

Below is the C program to implement the system().

C




// C program to implement
// system()
#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("step before system call\n");
    system("ps");
    printf("step after system call\n");
}


Output:

Output system()

execl() call replaces the image of the current process with a new process image specified by the path i.e. the current process code gets replaced by a new process code.

Syntax:

#include<stdio.h>
#include<unistd.h>
int execl(const char *path, const char *arg, . . . /* (char *) NULL */); 

Below is the C program to implement the execl().

C




// C program to implement
// execl()
#include <stdio.h>
#include <unistd.h>
int main()
{
    printf("step before execl call\n");
    execl("/bin/ps", "ps", NULL);
    printf("step after execl call");
}


Output:

Output execl()

Difference in working of system() and execl() call

In system() call:

  1. The code system() does not replace the image of the current process which is a.out.
  2. So, the first statement gets printed ie “step before system call” the other process ps is created using system() call which continues independently printing the output of the ps command while a.out also continues and prints using the second printf() statement ie “step after system call”.
  3. As seen from the out of ps command there were two processes a.out (having PID 2556) and ps (having PID 2558).

In execl() call:

  1. The statement printf(“Step before execl call\n”); is printed and then execl() is called which replaces the current process image with the new process i.e. ps.
  2. Hence, the output of the ps command is printed and not the second printf() statement.
  3. The output of ps also shows the entry for ps only and not a.out (the original process) because the image of a.out is replaced by the image of ps process.

system() vs execl()

S No. system() execl()
1. system() call creates a new process. execl() call does not create a new process.
2. It does not replace the image of the current process. It replaces the image of the current process.
3. System call provides an interface between a process and an operating system. It return any value.
4. System calls are available as assembly language instructions.

It takes two arguments:

  1. The path of the file to execute.
  2. Pointers to NULL-terminated character strings
5. System calls are made when a process in user mode requires access to a resource. It loads the program into the current process space and runs it from the entry point.

The system() and execl() calls are both used to execute external programs or commands from within a C program. However, there are some differences between these two system calls.

The system() call takes a command string as its argument and executes it as a shell command. It returns an integer value indicating the success or failure of the command execution. The system() call provides a simple way to execute shell commands, but it has some limitations. For example, it does not provide fine-grained control over the execution of the command, and it may not be suitable for executing complex or long-running commands.

On the other hand, the execl() call is used to replace the current process image with a new process image. It takes the path of the executable file as its first argument, followed by a list of command-line arguments. The new process image is loaded and executed, and the old process is terminated. The execl() call provides a finer level of control over the execution of the external program and can be used to execute complex or long-running commands.

Here are some key differences between system() and execl():

  1. system() executes the command as a shell command, while execl() executes a binary executable file.
  2. system() returns an integer value indicating the success or failure of the command execution, while execl() does not return a value.
  3. system() is simpler to use and can be used to execute simple shell commands, while execl() provides finer control over the execution of the external program and can be used to execute complex or long-running commands.

Overall, the choice between system() and execl() depends on the specific requirements of the program and the level of control needed over the execution of the external program.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads