LABORATORY-5

Replacing the image process




The command will be used is "execv"


execv (in C program means ): The execv is the function executes the file named by the filename as a new process image.


The argv argument is an array of null terminated strings that is used to give the value for the argv argument to the main function of the program to be executed.


exec family of a functions replaces the current running process with a new process.


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



Code for various methods of execv function:

            #include "stdio.h"
            #include "stdlib.h"
            #include "unistd.h"
            
            int main(){
                char* count ps_argv[] = {"ps", "-ax", 0};
                printf("Before handing over");
                execv("ps", ps_argv);
                printf("After handing over");
                printf(Done);
            }
    



            #include "stdio.h"
            #include "stdlib.h"
            #include "unistd.h"
            
            int main(){
	            char*const ls_argv[] = {"ls","-l",0};
	            printf("Before handing over");
	            execv("ls", ls_argv);
	            printf("After handing over");
	            printf(Done);
            }
    



        #include "stdio.h"
        #include "stdlib.h"
        #include "unistd.h"
        int main(){
            char*const cat_argv[] = {"cat","newfile.txt",0};
            printf("Before handing over");
            execv("cat", cat_argv);
            printf("After handing over");
            printf(Done);
        }
    



        #include "stdio.h"
        #include "stdlib.h"
        #include "unistd.h"
        //If we have a file named a program named as: "selectionsort.c" then 

        int main(){
	        char *args[] = {"./selectionsort.out",0};
	        printf("Before handing over");
	        execv("args[0]",args);
	        printf("After handing over");
	        printf(Done);
        }