Exec Family System Call

  • The exec() family of functions replaces the current process image with a new process image.
  • One of the practical application of using this family of function is during the use of the fork() API.
  • In the case of fork() API system call, the child process has the same address space as that of the parent process.
  • That is the child processes will run two instances of the same application or a program.
  • Thus having a child process that does execute the same program as that of the parent process does not hold any significance.
  • When a process calls exec family APIs, all code (text), data (initialized and uninitialized), stack and, heap section of calling process is replaced with the executable of the new program.
  • Hence with exec family system call replaces the child process address space with a new process image.
  • The exec subroutine does not create a new process but overlays the current program with a new one, which is called the new process image.

Syntax:

This family has many API with the basic syntax as a path of the program to execute and a variable number of arguments.

int execl (const char *path, const char *arg,...,(char *)NULL
  • The first argument is the path of the file being executed.
  • The second argument is the list of the variable length arguments.
  • It must be terminated with NULL pointer.

Return Value:

It does not return anything on success and -1 on failure.

Diag-1: exec() API family

Sample Program-1:
Old Image: execl.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

int main()
{
  printf("\n Executing the old program ..\n");
  int res = execl ("image", "Hello", NULL);
  if (res == -1)
  {
    printf("\n execl() API failed  %s\n", strerror(errno));
    exit (EXIT_FAILURE);
  }
  return 0;
}

New image program( new_image.c)

#include <stdio.h>

int main( int argc, char *argv[])
{
  printf ("\n New image is executing because of execl() API\n");
  printf ("\n Argument from old image is %s\n", argv[0]);
  return 0;
} 
Output:
[aprakash@wtl-lview-6 execl]$ gcc new_image.c -o image
[aprakash@wtl-lview-6 execl]$ gcc execl.c 
[aprakash@wtl-lview-6 execl]$ ./a.out 

 Executing the old program ..
 New image is executing because of execl() API
 Argument from old image is Hello

Sample Program-2 (Fork with Execl)

This program invokes a fork() call, and if it is a child process, the current image is replaced with a new image. wait() API is used to make sure that the child process is terminated before parent and child process resources are freed. Please refer fork() and wait() for a clear understanding of the below program.

Old image(fork_execl.c)

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
  char *msg = "Hello how are U";
  int result = fork();
  if (result == -1)
  {
          printf("\n Fork failed %s\n", strerror(errno));
          exit (EXIT_FAILURE);
  }

  if (result !=0) /* Parent process */
  {
    int stat_val;

    printf ("\n Waiting for child to finish\n");
    int child _pid = wait (&stat_val);
    if (child _pid == -1)
    {
     printf ("\n There is no child process \n");
     exit (EXIT_FAILURE);
    }

    if (WIFEXITED(stat_val))
       printf ("\n Child terminated Normally\n");
    else
       printf ("\n Child terminated Abnormally");

  }
  else /* Child process */
  {
    result = execl ("fork_new_image","fork_execl", msg, NULL);
    if (result == -1)
    {
        printf ("\n execl failed %s\n", strerror(errno));
        exit (EXIT_FAILURE);
    }
  }
return 0;
}

New image(fork_new_image)

  #include <stdio.h>
  int main (int argc, char *argv[])
  {
    printf ("\n Executing new image\n");
    printf("\n Argument passed is : %s\n", argv[1]);
 
    return 0;
 }
Output:
[aprakash@wtl-lview-6 execl]$ gcc fork_new_image.c -o fork_new_image
[aprakash@wtl-lview-6 execl]$ gcc fork_execl.c 
[aprakash@wtl-lview-6 execl]$ ./a.out 

 Waiting for child to finish
 Executing new image
 Argument passed is : Hello how are U
 Child terminated Normally

Related Posts:



Categories: Operating system (OS)

2 replies

Trackbacks

  1. Index of Operating System - Tech Access
  2. Pipes - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: