Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created October 18, 2021 17:12
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 fmitha/a03522522742603610ae53aa3599e0b7 to your computer and use it in GitHub Desktop.
Save fmitha/a03522522742603610ae53aa3599e0b7 to your computer and use it in GitHub Desktop.
/*
//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