Skip to content

Instantly share code, notes, and snippets.

@olastor
Created November 5, 2017 13:25
Show Gist options
  • Save olastor/4f48edf411303e394b90a838c1a1dfef to your computer and use it in GitHub Desktop.
Save olastor/4f48edf411303e394b90a838c1a1dfef to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid;
if ((pid = fork()) < 0) {
// error occurred
printf("Error using fork()");
return EXIT_FAILURE;
} else if(pid == 0) {
// child process
printf("In child process:\n");
printf("\t- pid: %d\n", getpid());
printf("\t- parent's pid: %d\n", getppid());
} else {
// 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