Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).


//A small example to show how to use execvp() function in C. We will have

two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by

calling execvp() function in execDemo.c .


//EXEC.c 

#include<stdio.h>

 #include<unistd.h>

 int main() 

 { 

      int i;

      printf("This  is EXEC.c, called by execvp() "); 

      printf("\n");

      return 0;

  } 


Now,create an executable file of EXEC.c using command

 gcc EXEC.c -o EXEC


//execDemo.c

#include<stdio.h>

#include<stdlib.h> 

#include<unistd.h> 


int main()

 {

    //A null terminated array of character 

    //pointers 


     char *args[]={"./EXEC",NULL}; 

     execvp(args[0],args);


    /*All statements are ignored after execvp() call as this whole process(execDemo.c) is replaced by       another process (EXEC.c) */ 

     printf("Ending-----");

     return 0; 


Now, create an executable file of execDemo.c using command gcc execDemo.c -o execDemo


After running the executable file of execDemo.cby using command ./excDemo, we get the following output: 

This is EXEC.c, called by execvp() 


When the file execDemo.c is compiled, as soon as the statement execvp(args[0],args) is

executed, this very program is replaced by the program EXEC.c. is not printed because

because as soon as the execvp() function is called, this program is replaced by the

program EXEC.c.

execv : This is very similar to execvp() function in terms of syntax as well. The syntax of

execv() is as shown below:Syntax:

int execv(const char *path, char *const argv[]);

path: should point to the path of the file being executed.

argv[]: is a null terminated array of character pointers.

Comments

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.