Os Lab 09
Os Lab 09
Operating Systems
Experiment 09
C Programming in Linux & exec*() System Call Variants
C language is like Latin- it is finite and has not changed for years. C is built right into the core of
UNIX and Linux. C remains particularly popular in the world of Unix-like operating systems, and,
for example, most of the Linux kernel (i.e., the core of the operating system) is written in C.
The “int main(int argc, char *argv[])” part is the start of the actual program. This is
an entry point.
The “int argc” is an argument to the function “main” which is an integer of the number
of character string arguments passed in “char *argv[]” (a list of pointers to character
strings) that might be passed at the command line when we run it.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <stdio.h>
#include <stdlib.h>
There can be no return from a successful exec because the calling process image is
overlaid by the new process image.
Commonly a process generates a child process because it would like to transform the
child process by changing the program code the child process is executing.
The text, data and stack segment of the process are replaced and only the u (user) area of
the process remains the same.
#include <unistd.h>
'e' indicates the programmer will construct (in the array/vector format) and pass their
own environment variable list.
'p' indicates the current PATH string should be used when the system searches for
executable files.
NOTE:
In the four system calls where the PATH string is not used (execl, execv, execle, and
execve) the path to the program to be executed must be fully specified.
execl
This system call is used when the number of arguments to be passed to the program to be
executed is known in advance
execv
This system call is used when the numbers of arguments for the program to be executed
is dynamic.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
DESCRIPTION
Where argc is the argument count and argv is an array of character pointers to the
arguments themselves. In addition, the following variable:
file
It is the filename of the file that contains the executable image of the new process.
path
It identifies the location of the new process image within the hierarchical file system (HFS).
1. If the path argument contains a slash (/), it is assumed that either an absolute or a
relative pathname has been specified.
2. If the path argument does not contain a slash, the directories specified by the PATH
environment variable are searched in an attempt to locate the file.
argv
It is a pointer to an array of pointers to null-terminated character strings. A NULL pointer
is used to mark the end of the array. Each character string pointed to by the array is used
to pass an argument to the new process image. The first argument, argv[0], is required
and must contain the name of the executable file for the new process image.
.
arg0, ..., NULL
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
It is a variable length list of arguments that are passed to the new process image. Each
argument is specified as a null-terminated string, and the list must end with a NULL
pointer.
1. The first argument, arg0, is required and must contain the name of the executable
file for the new process image.
2. If the new process image is a normal C main program, the list of arguments will
be passed to argv as a pointer to an array of strings. The number of strings in the
array is passed to the main() function as argc.
3rd Argument:
envp
It is a pointer to an array of pointers to null-terminated character strings. A NULL pointer
is used to mark the end of the array. Each character string pointed to by the array is used
to pass an environment variable to the new process image.
This system call simply replaces the current process with a new program -- the pid
does not change
The exec () is issued by the calling process and what is expected is referred to as the new
program -- not the new process since no new process is created
It is important to realize that control is not passed back to the calling process unless an
error occurred with the exec () call
In the case of an error, the exec () returns a value back to the calling process
The following example illustrates the use of execv to execute the ls shell command:
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
main()
{
pid_t pid;
char *const parmList[] = {"/bin/ls", "-l", NULL};
The following example illustrates the use of execl to execute another program (sum.c)
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Program 9-5: To add two numbers taken as argument from command line. (sum.c)
#include<stdio.h>
#include<stdlib.h>
void main(int argc , char * argv[])
{
int i,sum=0;
if(argc != 3)
{
printf("Arguments not valid!");
exit(1);
}
printf("The sum is : ");
for(i=1;i<argc;i++)
sum = sum + atoi(argv[i]);
printf("%d",sum);
}
Program 9-6: Child is overlaid by “Program 9-5” using execl command after fork.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("fork error");
return 1;
}
else if(pid ==0)
{
if (execl("/home/sidra/lab/sum","sum","5","6", NULL) <0 )
{
perror("child fail to execute");
return 1;}}
return 0;
}
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
TASK 1:
Compile and execute the “Program 9-1” and “Program 9-2” as given below. Examine the
output.
o gcc –o args 9-1.c
o ./args Operating system C programming
TASK 2:
Compile and execute the “Program 9-3”and “Program 9-4”. What is the difference
between them?
TASK 3:
Keeping in mind “Program 9-5”, multiply 4 integers taken from command line.
Keeping in mind “Program 9-5” & “Program 9-6”, Compute power of 2 (2n where n is
taken as argument from exec system call).