Difference Between System And Execl Call In C

Programs interact with the operating system by making system calls, which are utilized to execute commands within a process. The Application Program Interface (API) facilitates access to the operating system's services for user programs. Instead of replacing the current process's image, a system call serves as a bridge between a process and the operating system.

Syntax:

It has the following syntax:

Example

int system(const char *command);

Parameters:

Input: A string that includes the instruction to be conveyed to the command processor (shell).

Output: A string comprising the directive to be transmitted to the command processor (shell).

Return Value:

The implementation-specified value, typically the exit status of the command, will be provided by system upon successful execution.

The command will yield a result of -1 if it is unable to execute or if it is terminated by a signal.

Example:

Let's consider an example to demonstrate the system function in the C programming language.

Example

#include<stdlib.h>

#include<stdio.h>

int main()

{

    // Execute the "ls -l" command using system()

    int result = system("ls -l");

    // Check the result of the system call

    if (result == -1) 

    {

        // An error occurred

        perror("Error executing command");

    }

    else

    {

        // Print the exit status of the Command

        printf("Command executed with exit status: %d\n", result);

    }

    return 0;

}

Output:

Output

-rwxrwxrwx 1 root  root    455 May 12 18:17 main.c

Command executed with exit status: 0

Explanation:

  • Include Headers The system and printf are among the functions provided by the standard library headers (stdlib.h and stdio.h) included in this program.
  • Main Function It serves as the program's entry point. Here is where the program execution begins.
  • Execute Command using system The Command is executed as an argument to the system method. In this instance, it runs the "ls -l" command, which provides a long formatted list of all the files in the current directory. The status of the executed Command's exit is returned by system.
  • Check Result and Handle Errors The program verifies the outcome of the system call. An error occurs, and an error message is printed to the standard error stream using the perror function if the result is -1. If the result is not -1, the Command was executed successfully, and then printf is used to print the exit status.
  • Return from Main The main function returns 0 to signify that the program has run successfully.
  • What is the execl?

The execl group of functions belongs to the exec family, which is responsible for substituting the existing process image with a new one. Various versions of the execl function exist (such as execl, execle, execlp, etc.), all designed for a common objective.

Syntax:

It has the following syntax:

Example

int execl(const char *path, const char *arg0, ..., (char *)NULL);

Parameters:

  • path: the path to the executable file.
  • arg0,...: The new program's command-line parameters.
  • (char *)NULL: A sentinel value indicating the argument list end.
  • Return Value:

  • If execl is successful, the new program is run in the current process; it does not return.
  • It returns -1 if there is an error.
  • Example:

Let's consider an example to demonstrate the excel function in C programming.

Example

#include <stdio.h>

#include <unistd.h>

int main()

{

    printf("the step before the execl call\n");

    execl("/bin/ps", "ps", NULL);

    printf("step after execl call");

}

Output:

Output

the step before the execl call

PID TTY          TIME CMD

  13619 pts/37   00:00:00 dash

13626 pts/37   00:00:00 ps

Explanation:

  1. Include Headers

The software integrates the execl function along with standard headers for Unix system calls (unistd.h) and input and output (stdio.h).

  1. Entry Point

It marks the beginning point of program execution.

  1. Display Output Prior to 'execl'

The statement preceding the execl function displays a notification.

The 'execl' System Call

This method initiates the launching of a new process as defined by the Command parameter instead of the currently active one. In this case, it runs the "ps" command, which presents a roster of the ongoing processes. The path to the executable ("/bin/ps") serves as the initial parameter; the Command ("ps") is the second parameter; and the third argument, NULL, signifies the conclusion of the parameter series.

The subsequent code in the original software will not be run if execl effectively substitutes the current process with the "ps" command. If the execl operation fails (due to the absence of the specified executable), the program will continue execution from the subsequent line. In this scenario, it will return -1.

  1. Display Output Post 'execl'

This line will only be executed if the execl function fails. The message printed indicates the step following the execl call.

Key differences between System and Execl call in C

There exist distinct variations between system and excel functions in the C programming language. Here are some primary variances between these function calls:

1. Purpose

The system function carries out shell commands within C programs.

The execl function is utilized to replace the current process image with a completely new one.

2. Input

The system function takes a string parameter representing a command to be executed in the shell.

The execl function takes in an executable path, its filename, and a set of arguments.

3. Control Flow

system: Upon completion of the shell command, the control flow reverts back to the C program, involving a distinct shell process.

The execl function does not return if it is successful; instead, the new executable takes the place of the current running process. If an error occurs, the function returns -1.

4. Shell involvement

system: This function employs the shell, potentially introducing extra processing demands and security vulnerabilities.

The shell is bypassed when using execl. The specified executable is loaded directly into the current process.

5. Flexibility

system: It offers limited control over the intricacies of process execution, making it particularly suitable for straightforward shell commands.

The execl function provides a higher level of control over the execution of a process, allowing for the precise specification of the executable and its corresponding arguments.

6. Return Value

The system function returns the exit status of a shell command or a value defined by the implementation if the command cannot be executed.

The execl function does not provide a return value upon success, instead returning -1 to indicate an error occurrence.

7. Error Handling

system: It provides minimal error handling capabilities and might not offer detailed error messages.

The execl function offers a higher level of error detail. In case of an error, it returns -1, allowing us to utilize perror for additional error information.

Input Required

This code uses input(). Please provide values below: