Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between system() and execl() call

  • Difficulty Level : Hard
  • Last Updated : 27 Apr, 2022

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!