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
Post a Comment