Last active
July 29, 2018 22:49
-
-
Save AkhandMishratruth/662ccd8006b4ee16f4b595d25bf0c43e to your computer and use it in GitHub Desktop.
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
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
int main(int argc, char**argv) { | |
printf("Parent process: before fork getpid result: %d\n", getpid()); | |
sleep(5); | |
pid_t pid = fork(); | |
// error occured | |
if (pid == -1) | |
return EXIT_FAILURE; | |
// executing child process | |
if (pid == 0){ | |
char* args[] = {"./execExample", "Arg1", "Arg2", NULL}; | |
execv(args[0], args); | |
} | |
// executing parent process | |
else { | |
printf("Parent process: getpid result: %d\n", getpid()); | |
int status; | |
pid_t childPID = wait(&status); | |
printf("Parent process: wait return: %d, status changes to: %d, and status value is: %d", (int)childPID, status, WEXITSTATUS(status)); | |
return EXIT_SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment