0% found this document useful (0 votes)
201 views9 pages

Lab Manual Exec System Call: A Family of Six Functions

The document discusses the exec system call in Linux operating systems. It begins by explaining that exec replaces the current process with a new program, unlike fork which creates a new process. It then covers the six variations of the exec call, noting differences in how arguments are passed (as a list vs array) and whether the PATH is searched or environment is replaced. Examples are provided to demonstrate usage of execl, execlp and execv. The naming conventions of the exec calls are also explained.

Uploaded by

Mubeen ul hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views9 pages

Lab Manual Exec System Call: A Family of Six Functions

The document discusses the exec system call in Linux operating systems. It begins by explaining that exec replaces the current process with a new program, unlike fork which creates a new process. It then covers the six variations of the exec call, noting differences in how arguments are passed (as a list vs array) and whether the PATH is searched or environment is replaced. Examples are provided to demonstrate usage of execl, execlp and execv. The naming conventions of the exec calls are also explained.

Uploaded by

Mubeen ul hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Khwaja Fareed University of Engineering and Information Technology

Rahim Yar Khan

OPERATING SYSTEMS LAB SPRING 2019

Lab Manual
Exec System Call
A family of six functions

1 EXEC() SYSTEM CALL

Forking provides a way for an existing process to start a new one, but what about the
case where the new process is not part of the same program as parent process? This
is the case in the shell; when a user starts a command it needs to run in a new
process, but it is unrelated to the shell. This is where the exec system call comes into
play. exec will replace the contents of the currently running process with the
information from a program binary.
1. The exec function is actually a family of six functions each with slightly different
calling conventions and use.
2. Like fork, exec is declared in <unistd.h>.
3. exec completely replaces the calling process’s image with that of the program
be-ing executed.
4. fork creates a new process, and so generates a new PID. exec initiates a new
pro-gram, replacing the original process. Therefore, the PID of an execed
process doesn’t change.
5. If successful, the exec calls do not return as the calling image is lost.
Operating Systems Lab Spring, 2019 KFUEIT, RYK

2 PROTOTYPES OF EXEC()

Prototypes of exec() are:


i n t e x e c l ( c o n s t char path , c o n s t char arg , ...);

i n t e xe c l p ( c o n s t char f i l e , c o n s t char arg , ...);

i n t execv ( c o n s t char path , char c o n s t argv [ ] ) ;

i n t execvp ( c o n s t char f i l e , char c o n s t argv [ ] ) ;

i n t e xe c l e ( c o n s t char path , c o n s t char arg , char c o n s t envp [ ] ) ;

i n t execve ( c o n s t char path , c o n s t char argv [ ] , char c o n s t envp [ ] ) ;

3 NAMING CONVENTION OF EXEC()

The naming convention for the "exec" system calls reflects their functionality:

1. The function starts with exec indicating that one of the function from exec family
is called.
2. The next letter in the call name indicates if the call takes its arguments in a "list
format" (i.e. literally specified as a series of arguments) or as a pointer to an
"array of arguments".

(a) The functions execl, execlp, and execle require each of the command-line
arguments to the new program to be specified as separate arguments,
termi-nated by a NULL pointer.
(b) For the other functions (execv, execvp, execve), we have to build an array
of pointers to the arguments.

3. The presence of a "p" indicates the current PATH string should be used when
the system searches for executable files. (If the executable file is script file, the
shell is invoked to execute the script. The shell is then passed the specified
argument information).
4. The functions whose name end with an e allow to pass array to set new
environ-ment. Otherwise the calling process copy the existing environment for
the new program.

2
Operating Systems Lab Spring, 2019 KFUEIT, RYK

Letters Explanation

l argv is specified as a list of arguments.


v argv is specified as a vector (array of character
pointers).
e environment is specified as an array of charac-
ter pointers.
p PATH is searched for command, and command
can be a shell program

4 EXECL()

int execl(const char path , c o n s t char arg , ...)

1. Takes the path name of an executable program (binary file) as its first argument.

2. The rest of the arguments are a list of command line arguments to the new pro-
gram.
3. The list is terminated with a null pointer.

4. Examples:
e x e c l ( " /bi n/ l s " , " l s " , " l " , " /u s r " , NULL ) ;
e x e c l ( " /home/ l a b /Desktop/a . out " , " . / a . out " , " arg 1 " , " arg 2 " , " arg n " ,NULL)

EXAMPLE 01
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < s y s /wait . h>
# include < s t d i o . h>
# include < e r r n o . h>
# include < s t d l i b . h>

i n t main ( ) {
pid_t childpid = fork();
if(childpid==0){
p r i n t f ( " I am a c h i l d proce with pid = %d \n " , g e t p i d ( ) ) ;
p r i n t f ( " The n e x t s t a t e m e n t i s e x e c l and l s w i l l run \n " ) ;
e x e c l ( " /bi n/ l s " , " l s " , " l " , " /u s r " ,NULL ) ;
printf("Execl failed");
}
else if(childpid>0){
wait (NULL ) ;
p r i n t f ( " \n I am p a r e n t p r o c e s s with pid = %d
and f i n i s h e d w a i t i n g \n " , g e t p i d ( ) ) ;
}

3
Operating Systems Lab Spring, 2019 KFUEIT, RYK

return 0;
}

EXAMPLE 02
Apart from executing the shell commands we can run other executables as well. In this
example we will write a code in C and create an executable named as execExam. Given
below is a simple code in C that prints the arguments passed to it through terminal.
# include < s t d i o . h>
void main ( i n t a r g c , char argv [ ] ) {
int i ;
f o r ( i =0 ; i < a r g c ; i ++ ) {
p r i n t f ( " Argument %d : %s\n " , i , argv [ i ] ) ;
}
}

Now compile the code and create an object file execExam :


gcc o execExam f i l e n a m e . c

Following program will now create a new process using fork system call. Then the
child process will run the executable of the above program using execl().
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < s y s /wait . h>
# include < s t d i o . h>
# include < e r r n o . h>
# include < s t d l i b . h>

i n t main ( ) {
pid_tchildpid = fork();
if(childpid==0){
p r i n t f ( " I am a c h i l d proce with pid = %d \n " , g e t p i d ( ) ) ;
p r i n t f ( " The n e x t statement is e x e c l and execExam
executable
w i l l run \n " ) ;
e x e c l ( " /home/ s a r o s h /Desktop/execExam " , " l s " , " l " , " /u s r " ,NULL ) ;
printf("Execl failed");
}
e l s eif(childpid>0){
wait (NULL ) ;
p r i n t f ( " \n I am p a r e n t p r o c e s s with pid = %d and
f i n i s h e d w a i t i n g \n " , g e t p i d ( ) ) ;
}
r e t u r n 0;
}

5 EXECLP()

i n t e x e c l p ( c o n s t char file, c o n s t char arg , ...)

4
Operating Systems Lab Spring, 2019 KFUEIT, RYK

1. Same as execl(), except that the program name doesn’t have to be a full path
name and it can be a shell program instead of an executable module. The
program directory should be in your PATH variable.
2. The rest of the arguments are a list of command line arguments to the new pro-
gram.
3. The list is terminated with a null pointer.

4. Example:
e x e c l p ( " l s " , " l s " , " l " , " /u s r " , NULL ) ;
e x e c l p ( " c a t " , " c a t " , " f i l e n a m e " , NULL ) ;
e x e c l p ( " . / a . out " , " a . out " , " arg 1 " , NULL ) ;

EXAMPLE 01
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < s y s /wait . h>
# include < s t d i o . h> #
include < e r r n o . h> #
include < s t d l i b . h>

i n t main ( ) {
pid_t childpid = fork();
if(childpid==0){
p r i n t f ( " I am a c h i l d proce with pid = %d \n " , g e t p i d ( ) ) ;
p r i n t f ( " The n e xt s t a t e m e n t i s e x e c l p and l s w i l l run \n " ) ;
e x e c l p ( " l s " , " l s " , " l " , " /u s r " ,NULL ) ;
printf("Execl failed");
}
else if(childpid>0){
wait (NULL ) ;
p r i n t f ( " \n I am p a r e n t p r o c e s s with pid = %d and
f i n i s h e d w a i t i n g \n " , g e t p i d ( ) ) ;
}
return 0;
}

6 EXECV()

i n t execv ( c o n s t char path , char c o n s t argv [ ] ) ;

1. Takes the path name of an executable program (binary file) as it first argument.

2. The second argument is a pointer to a list of character pointers (like argv[]) that
is passed as command line arguments to the new program.
3. The list is terminated with a null pointer.

5
Operating Systems Lab Spring, 2019 KFUEIT, RYK

4. Example:
char a r g s [ ] = { " c a t " , " f i l e n a m e 1 " , NULL };
execv ( " /b in/ c a t " , a r g s ) ;

char a r g s [ ] = { " . / a . out " , " arg1 " , " arg2 " , NULL } ; execv ( "
/home/ l a b /Desktop/a . out " , a r g s ) ;

EXAMPLE 01
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < s y s /wait . h>
# include < s t d i o . h>
# include < e r r n o . h>
# include < s t d l i b . h>

i n t main ( ) {
pid_tchildpid = fork();
if(childpid==0){
p r i n t f ( " I am a c h i l d proce with pid = %d \n " , g e t p i d ( ) ) ;
p r i n t f ( " The n e x t s t a t e m e n t i s execv and ls wil
l run \n " ) ;
char argv [ ] = { " l s " , " l " , " /u s r " ,NULL } ;
execv ( " /b in/ l s " , argv ) ;
printf("Execl failed");
}
e l s eif(childpid>0){
wait (NULL ) ;
p r i n t f ( " \n I am p a r e n t p r o c e s s with pid = %d and
f i n i s h e d w a i t i n g \n " , g e t p i d ( ) ) ;
}
r e t u r n 0;
}

7 EXECVP()

i n t execvp ( c o n s t char f i l e , char c o n s t argv [ ] ) ;

1. Same as execv(), except that the program name doesn’t have to be a full path
name, and it can be a shell program instead of an executable module.
2. The second argument is a pointer to a list of character pointers (like argv[]) that
is passed as command line arguments to the new program.
3. The list of character pointer should be terminated with a null pointer.
4. Example:
char a r g s [ ] = { " c a t " , " f 1 " , " f 2 " , NULL } ;
execvp ( " c a t " , a r g s ) ;

EXAMPLE 01

6
Operating Systems Lab Spring, 2019 KFUEIT, RYK

# include < u n i s t d . h>


# include < s y s / t y p e s . h>
# include < s y s /wait . h>
# include < s t d i o . h>
# include < e r r n o . h>
# include < s t d l i b . h>

i n t main ( ) {
pid_t childpid = fork();
if(childpid==0){
p r i n t f ( " I am a c h i l d proce with pid = %d \n " , g e t p i d ( ) ) ;
p r i n t f ( " The n e x t s t a t e m e n t i s execv and l s w i l l run \n " ) ;
char argv [ ] = { " l s " , " l " , " /u s r " ,NULL } ;
execvp ( " l s " , argv ) ;
printf("Execl failed");
}
else if(childpid>0){
wait (NULL ) ;
p r i n t f ( " \n I am p a r e n t p r o c e s s with pid = %d and
f i n i s h e d w a i t i n g \n " , g e t p i d ( ) ) ;
}
return 0;
}

8 EXECLE()

int execle(const char path , c o n s t char arg , char c o n s t envp [ ] )

1. Same as execl(), except that the end of the argument list is followed by a
pointer to a null-terminated list of character pointers that is passed as the
environment of the new program.
2. Example:
char env [ ] = { " e x p o r t TERM= v t 1 0 0 " , "PATH=/b in : / u s r/b in " , NULL } ;
e x e c l e ( " /b in/ c a t " , " c a t " , " f 1 " , NULL, env ) ;

9 EXECVE()

i n t execve ( c o n s t char path , c o n s t char argv [ ] , char c o n s t envp [ ] ) ;

1. Same as execv(), except that a third argument is given as a pointer to a list of char-
acter pointers (like argv[]) that is passed as the environment of the new program.

2. Example:
char env [ ] = { " e x p o r t TERM= v t 1 0 0 " , "PATH=/b in : / u s r/b in " , NULL } ;
char a r g s [ ] = { " c a t " , " f 1 " , NULL } ; execve ( " /b in/ c a t " , args , env ) ;
Q1: write down the outputs of all examples
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

Q2: write down the working of prototypes of exec ( )


Execl ( ):

execl p ( ):

execv ( ):

execvp ( ):

execle ( ):

execve ( ):

You might also like