Skip to content

Instantly share code, notes, and snippets.

@scriptum
Last active December 22, 2015 15:58
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 scriptum/6495592 to your computer and use it in GitHub Desktop.
Save scriptum/6495592 to your computer and use it in GitHub Desktop.
Simplest possible fork example for Linux
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define __USE_GNU
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
pid_t child;
int status;
if(argc < 2)
{
printf("%s: command_to_execute [args]\n", argv[0]);
exit(1);
}
switch(child = fork())
{
case -1:
status = -1;
case 0:
execvp(argv[1], argv + 1);
exit(127);
default:
if(TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) != child)
status = -1;
}
printf("=============\nChild's exit code: %d\n", (status));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment