Skip to content

Instantly share code, notes, and snippets.

@olastor
Created November 5, 2017 13:44
Show Gist options
  • Save olastor/c3213fd171c2a32b5b9286740e37c9ed to your computer and use it in GitHub Desktop.
Save olastor/c3213fd171c2a32b5b9286740e37c9ed to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid;
switch ((pid = fork())) {
case -1:
// error occurred
printf("Error using fork()");
break;
case 0:
// child process
printf("In child process:\n");
printf("\t- pid: %d\n", getpid());
printf("\t- parent's pid: %d\n", getppid());
break;
default:
// parent process
printf("In parent process:\n");
printf("\t- pid: %d\n", getpid());
printf("\t- child's pid: %d\n", pid);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment