Last active
April 27, 2016 15:54
-
-
Save jayantbh/56989b511107d52de3f07ae03f4d6e99 to your computer and use it in GitHub Desktop.
Exec family of functions usage demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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