Featured Post

Monday, August 1, 2016

execl family system calls


         exec() is a process related system call. The exec() system call when called it replaces the process code(and/or environment)  in the address space of present process with new executable binary code specified in exec() function.i.e., by calling either of these functions we can load a executable file of a program into the address space of the current process. The following are the various types of exec() system calls.


  1. execl()
  2. execv()
  3. execle()
  4. execve()
  5. execlp()
  6. execvp()
The prototypes for these calls are:
    
   int execl(fname, arg0 [,arg1, arg2,,,,,,,,argn], NULL)
   
  int execv(fname, argv);
  
  int execle(fname, arg0 [arg1, arg2,......argn], NULL, envp);

  int execve(fname, argv, envp);

  int execlp(fname, arg0 [arg1, arg2,,,,,,argn], NULL);

  int execvp(fname, argv);

 where fname names the executable binary file to be loaded into the address space of a present process.

 arg0 through argn specifies the arguments to be passed to the process. 

envp specifies the environment.

note: all variables are character pointers in the above functions
char *fname, *arg0,*arg1,.........*argn, *envp[], *argv[]
argv[0], arg0 by default name the last path name component of the executable binary file named by
fname.

For execl(), execv(), execle() , execve()  fname must be fully qualified path name of the executable binary file. 
For execvp(), execlp() the PATH variable is used to find the executable binary file.

->exec() system call does  not return any thing on success. 

There is a meaning for each letter added at the end of exec family of functions 

l----> argn is specified as a list of arguments
v---> argv is specified as a vector.
e---> environment is specified as an array of character pointers
p---> user's PATH is searched for command.


/* to demonstrate the use of execl() system call */

#include<stdio.h>
#include<unistd.h>
void main()
{
   printf("\n Before execl() function");
   
    execl("/bin/ls","ls","-l",NULL);
  
     printf("\n after execl function");
}


what is the output you expecting for this code?

Definitely the last printf statement will never get executed. Because after first  printf statement the second statement to be executed is execl() funtion. This execl() function replaces the present process code with the binary executable code of ls. So the third printf statement will never get executed.

OUTPUT:

      Before execl() function
       -rw-rw-r--  1 anil anil 2 nov 24 2014 hello.c
       -rw-rw-r--  2 raju raju 1 dec 21 2014  arm.c
       ---------------------------------------------------
       ---------------------------------------------------


/* to demonstrate the use of execv()  system call*/
        
#include<stdio.h>

#include<unistd.h>

void main()
{
  char *a[3];
  
   a[0]="ls"
   
   a[1]="-l"

   a[2]=0;

   printf("\n Before execv() function");
   
    execv("/bin/ls",a);
  
     printf("\n after execv function");
}


what is the output you expecting for this code?

Definitely the last printf statement will never get executed. Because after first  printf statement the second statement to be executed is execv() funtion. This execv() function replaces the present process code with the binary executable code of ls. So the third printf statement will never get executed.

NOTE: The only difference between execl() and execv() system calls is while the first one takes arguments as a list, the second system call takes arguments as vector.

OUTPUT:

      Before execv() function
       -rw-rw-r--  1 anil anil 2 nov 24 2014 hello.c
       -rw-rw-r--  2 raju raju 1 dec 21 2014  arm.c
       ---------------------------------------------------
       ---------------------------------------------------



/* to demonstrate the use of execlp()  system call*/
        
#include<stdio.h>

#include<unistd.h>

void main()
{

   printf("\n Before execlp() function");
   
    execlp("ls","ls","-l",0);
  
     printf("\n after execlp function");
}

what is the output you expecting for this code?

Definitely the last printf statement will never get executed. Because after first  printf statement the second statement to be executed is execlp() funtion. This execlp() function replaces the present process code with the binary executable code of ls. So the third printf statement will never get executed.

Note: using execlp() system call we don't need to specify the complete path of a file, just provide the file name. The PATH variable checks the executable code of given file name and loads it.

OUTPUT:

        Before execlp() function
       -rw-rw-r--  1 anil anil 2 nov 24 2014 hello.c
       -rw-rw-r--  2 raju raju 1 dec 21 2014  arm.c
       ---------------------------------------------------
       ---------------------------------------------------




 /* To demonstrate the use of execvp() system call */

#include<stdio.h>

#include<unistd.h>

void main()
{
  char *a[3];
  
   a[0]="ls"
   
   a[1]="-l"

   a[2]=0;

   printf("\n Before execlp() function");
   
    execlp("ls",a);
  
     printf("\n after execlp() function");
}

what is the output you expecting for this code?

Definitely the last printf statement will never get executed. Because after first  printf statement the second statement to be executed is execvp() funtion. This execvp() function replaces the present process code with the binary executable code of ls. So the third printf statement will never get executed.

Note: using execvp() system call we don't need to specify the complete path of a file, just provide the file name. The PATH variable checks the executable code of given file name and loads it.

OUTPUT:

        Before execlp() function
       -rw-rw-r--  1 anil anil 2 nov 24 2014 hello.c
       -rw-rw-r--  2 raju raju 1 dec 21 2014  arm.c
       ---------------------------------------------------
       ---------------------------------------------------
                                                                                                                              cjuschools.blogspot.in

excle() and excve() system calls:
        
               Before going to write lets write a program to print environment variables of a program

/* printing environment variables using environ variable */
/* environdemo.c*/

#include<stdio.h>

#include<unistd.h>

extern char **environ;  /* environ is a predefined extern variable */

  void main()
  {
     int i;

     for(i=0;environ[i];i++)
    {
        printf("%s\n",environ[i]);
     }
  }

Ouput:
  
       execute the above code and it will display all the environment variables available in a process
      
                                 
   When a child process is created the address space from parent to child i.e., all the environment variables will be copied to child process. There are certain situations where we need to copy minimum environment variables from parent to child. exece() and execve() system calls are used to for this purpose.



/* To demonstrate the use of execle() system call */
/* excledemo.c*/

#include<stdio.h>

#include<unistd.h.

void main()
{
   char *x[3];

   int i;

   x[0]="abc=/tmp"; /*environment variables */

   x[1]="xyz=/bin"; /*environment variables*/

   x[2]=0;

   if(fork()==0)
    {
       execle("./environdemo", "environdemo",(char *)0, x); /* supplying environment variables(x)                                                                                 to  execle() system call*/
    }
      wait(&i);
}

Output;
      /home/anil/~$ cc excledemo.c
      /homr/anil/~$ ./a.out
      /home/anil/~$


Now go back to previous program environdemo.c and execute it again

/home/anil/~$ cc environdemo.c
/home/anil/~$ ./a.out

now it display environment variables like /tmp and /bin environment variables but not all environment variables.


/* to demonstrate the use of execve() system call */
/* execve()demo.c */

#include<stdio.h>

#include<unistd.h.

void main()
{
   char *x[3],*y[2]; /as we need to supply the arguments as a vector we used extra pointer variable*/

   int i;

   x[0]="abc=/tmp"; /*environment variables */

   x[1]="xyz=/bin"; /*environment variables*/

   x[2]=0;

   y[0]="environdemo";

   y[1]=0;

   if(fork()==0)
    {
       execve("./environdemo", y, x);   /* supplying environment variables(x) to 
            execve() system call, y is vector containing the arguments to be supplied*/
    }
      wait(&i);
}

Output;
      /home/anil/~$ cc excvedemo.c
      /homr/anil/~$ ./a.out
      /home/anil/~$


Now go back to previous program environdemo.c and execute it again

/home/anil/~$ cc environdemo.c
/home/anil/~$ ./a.out

now it display environment variables like /tmp and /bin environment variables but not all environment variables. 
 
                       cjuschools.blogspot.incjuschools.blogspot.in

Unix operating system Linux operating system shell programming bourn shell kourne shell c shell difference between unix and linux operating system , history of UNIX, red hat operating system. UNIX OS architecture , kernel utility programs , file handling process handling inter process communication pipes shared memory message queues socket programming TCP/IP , UDP programming , One to One chat Application , Basic shell program to add two numbers , echo function , while loops clear gt lt le ge ne eq if fi while do done case array in unix strings , cut copy paste cat dir pwd getcwd chdir command read write stat lseek fstat flock mechanism.even or add program vi editor command ni vi editor command mode esc mode insert mode aa dd x :q :wq :wSystem calls. es,mkdir,chdir,open read pipe,shmget,shmat ,msgctl,msgsnd msgrecv, system calls. execl familyof system calls, environ variables, printing environment variables, execl(),execv(),execlp(),execvp(),execle(),execve() system calls. differnce between execl() and execv() system call, difference between execlp and execvp system calls, difference between execle() and execve() system calls, e,l,v,p in exec() system call, environ(),path variable , process handling in c , file locking demo

   




No comments:

Post a Comment