Skip to content

Instantly share code, notes, and snippets.

@jayantbh
Last active April 27, 2016 15:54
Show Gist options
  • Save jayantbh/56989b511107d52de3f07ae03f4d6e99 to your computer and use it in GitHub Desktop.
Save jayantbh/56989b511107d52de3f07ae03f4d6e99 to your computer and use it in GitHub Desktop.
Exec family of functions usage demo.
//exec() family of functions
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv){
pid_t id;
id = vfork();
if(id == 0){
execl("./aux","execl",NULL);
}
id = vfork();
if(id == 0){
execlp("./aux","execlp",NULL);
}
id = vfork();
if(id == 0){
char *envar[] = {"DUMMY=dummyenv",NULL};
execle("./aux","execle",NULL,envar);
}
id = vfork();
if(id == 0){
char *vc[] = {"execv"};
execv("./aux",vc);
}
id = vfork();
if(id == 0){
char *vc[] = {"execvp","somearg",NULL};
execvp("./aux",vc);
}
id = vfork();
if(id == 0){
char *vc[] = {"execvpe","somearg",NULL};
char *ev[] = {"DUMMY=dummyenv",NULL};
execvpe("./aux",vc,ev);
}
wait(10);
printf("\n");
return 0;
}
//Printing program for the above
#include <stdio.h>
int main(int argc, char** argv){
printf("AUX: %d %s %s %s %s | ENV (DUMMY): %s\n",argc,argv[0],(argv[1])?argv[1]:"none",(argv[2])?argv[2]:"none",(argv[3])?argv[3]:"none",((char *)getenv("DUMMY"))?(char *)getenv("DUMMY"):"none");
//return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment