Replacing a Process Image ========================= - The exec function replaces current process with a new process. - The path of new process is specified as argument to exec. - It is defined in header : #include<unistd.h> - Last argument of function should be NULL. - It has 6 types of functions, whose prototype are :- (i) Complete path is specified int execl(const char * path, const char * arg0,const char * arg1,...,NULL); (ii) If file is at one of default paths int execlp(const char * file, const char * arg0,const char * arg1,...,NULL); (iii) If arguments are given as an vector(array) int execv(const char *path,char *const argv[]); (iv) If arguments are given as an vector(array) and file is at one of default paths int execvp(const char* file, char *const argv[]); (v) int execle(const char *path, const char * arg0, const char * arg1,...,NULL, char *const envp[]); (vi) int execve(const char *path, char *const argv[],char *const envp[] );
- If the exec is executed successfully the statements after it will not be executed. - It is because the current process is replaced. - The arguments given to exec are used by main() function of new process as command line argument. - If exec fails, it returns -1
- The open file descriptors remain open after the exec command. - These file descriptors can be passed to exec as command and can be used by the replaced process to read/write from opened file. - The argument is passed to exec as character. - For this sprintf() is used. - And for getting the file descriptors back , atoi() or integer typecasting can be used on the arguments of main().