Created
October 18, 2021 17:12
-
-
Save fmitha/a03522522742603610ae53aa3599e0b7 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
/* | |
//From https://stackoverflow.com/a/2667166/350713 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
int main() | |
{ | |
int number, statval; | |
printf("%d: I'm the parent !\n", getpid()); | |
if(fork() == 0) | |
{ | |
number = 10; | |
printf("PID %d: exiting with number %d\n", getpid(), number); | |
exit(number) ; | |
} | |
else | |
{ | |
printf("PID %d: waiting for child\n", getpid()); | |
wait(&statval); | |
if(WIFEXITED(statval)) | |
printf("Child's exit code %d\n", WEXITSTATUS(statval)); | |
else | |
printf("Child did not terminate with exit\n"); | |
} | |
return 0; | |
} | |
*/ | |
//////////////////////////////////////////////////////////////////// | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
int main() | |
{ | |
int childpid, statval; | |
printf("parent PID is %d\n", getpid()); | |
if(fork() == 0) // child | |
{ | |
childpid = getpid(); | |
printf("child PID is %d\n", getpid()); | |
execl("/usr/bin/lsxx","lsxx", (char*) NULL); | |
} | |
else // parent | |
{ | |
printf("PID %d: waiting for child\n", getpid()); | |
wait(&statval); | |
//waitpid(childpid, &statval, 0); | |
if(WIFEXITED(statval)) | |
printf("Child's exit code %d\n", WEXITSTATUS(statval)); | |
else | |
printf("Child did not terminate with exit\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment