Skip to content

Instantly share code, notes, and snippets.

@AkhandMishratruth
Last active July 29, 2018 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkhandMishratruth/662ccd8006b4ee16f4b595d25bf0c43e to your computer and use it in GitHub Desktop.
Save AkhandMishratruth/662ccd8006b4ee16f4b595d25bf0c43e to your computer and use it in GitHub Desktop.
#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