1 Answers
Excel usage
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t ret;
printf("welcome...pid=%d\n", getpid());
ret = fork();
if (ret < 0) {
perror("fork");
exit(1);
}
if (ret == 0) {
printf("child--hello,pid=%d,ppid=%d\n", getpid(), getppid());
int k;
k = execl("/usr/bin/cal", "cal", "10", "2021", NULL);
if (k < 0) {
perror("execv");
exit(1);
}
exit(0);
} else // ret>0
{
printf("parent--hello,pid=%d,ppid=%d\n", getpid(), getppid());
waitpid(-1, &status, NULL);
}
return 0;
}
Other Possible ways of using execl family of functions
execlp:-
execlp("cal", "cal", "10", "2018", NULL);
execv:-
char* argv[]={"cal","10","2018",NULL};
k=execv("/usr/bin/cal",argv);
execvp:-
char* argv[]={"cal","10","2018",NULL};
k=execvp("cal",argv);
Try this example:-
- Build and generate an executable from a C Program taking command line arguments
- Launch this executable using execl/execv.
Notes
- Overwrites child address space with resources of specified program
- Process remains same, but program/resources will change
- Any code after execl is redundant, if execl succeeds
- execl vs execlp
- Usage of execv, execvp, execvpe
- Usage of excecle
Observation & Understanding
- When do execl fails, what’ll happen in such case?
- What if, program launched by execl fails (failure/abnormal termination) – how to analyze?
- Matching of
- execl 2nd param
- argv[0] in launched program
- cmd attribute in ps output