OVERLAY CONCEPTS

 

Overlay

   Overlay is the concept which enables the user to run another new process from the currently   running process address space.

System call used :

Exec() System Call

The exec() system call replaces (overwrites) the current process with the new process image. The PID of the new process remains the same however code, data, heap and stack of the process are replaced by the new program. There are 6 system calls in the family of exec(). All of these functions mentioned below are layered on top of execve(), and they differ from one another and from execve() only in the way in which the program name, argument list, and environment of the new program are specified

Syntax:

int execl(const char* path, const char* arg, ...) int execlp(const char* file, const char* arg, ...)

int execle(const char* path, const char* arg, ..., char* const envpl)) int execv(const char* path, const char* argv[])

int execvp(const char* file, const char* argv[])

int execvpe(const char* file, const char* argv[], char *const envpl])

      The names of the first five of above functions are of the form execXY.

      X is either 1 or v depending upon whether arguments are given in the list format (argo, argl, ..., NULL) or arguments are passed in an array (vector).

      Y is either absent or is either a p or an e. In case Y is p, the PATH environment variable is used to search for the program. If Y is e, then the environment passed in envp array is used.

      In case of execvpe, X is v and Y is e. The execvpe function is a GNU extension. It is named so as to differentiate it from the execve system call.

 

Simple overlay concept Procedure:

      Stepl: Create two different c programs. Name it as example.c file and hello.c file

      Step2: Make example .c is the current running process,

      Step3: call the function execv() which takes the hello.c as an argument.

      Step4: Print process id of both the processes(hello.c and example.c processes).

      Step5: trace the system control by having simple print statement in both programs.

 

Expected Output:

$ process id of example.c=4733 We are in hello.c

Process id of hello.c=4733

COMBINING FORK() AND EXEC() SYSTEM CALL

Procedure:

 

      Stepl: Create two different c programs. Name it as example.c file and hello.c file

      Step2: Make example .c is the current running process and use fork() system call to create child process,

      Step3: call the function execv() in child process which takes the hello.c as an argument.

      Step4: Print Process id of both parent, child and overlay processes (hello.c and example.processes).

      Step5: trace the system control by having simple print statement in both programs.

 

    Expected Output:

 

   $ process id of example.c=4790

   The control is in parent process

   The control is in child process

   Process id of child = 4791

   Calling hello.c from child

   We are in hello.c

   Process id of hello.c=4791

 

 

PRACTICE QUESTIONS:

 

1.  Execute the Following Program and write the output

$vi ex1.c.

#include <stdio.h>

#include <unistd.h>

int main()

printf("Transfer to execlp function \n");

execlp("head”, “head”,””-2”,"fl”,NULL); // Assume fl is any text file printf("This line will not execute \n”);

return 0;

 

Output :


 

2.  Execute the Following Program and write the output

$vi ex1.c.

#include <stdio.h>

#include <unistd.h>

int main()

printf("Transfer to execlp function \n");

execlp("head”, “head”,””-2”,"fl”,NULL); // Assume fl is any text file printf("This line will not execute \n”);

return 0;

 

Output :

 

 

  Why second printf statement is not executing?

 

 

 

Rewrite question 1 with execl() function. Pass the 3rd and 4th argument of the function execl() through command line arguments.

 

$vi ex2.c


Input : /a.out -3 fi Output:

Post a Comment

Previous Post Next Post