Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fork and wait system calls
include <stdio.h>
include <stdlib.h>
include <unistd.h>
include <sys/wait.h>
int main(int argc, char *argv[])
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
// Fork failed, let's exit.
if (rc < 0) {
fprintf(stderr, "fork failed\n");
} else if (rc == 0) {
// child process created successfully
printf("hello, I am child (pid:%d)\n", (int) getpid());
} else {
// Parent process continue here and block/wait for the child to finish.
int rc_wait = wait(NULL);
printf("hello, I am parent of %d (rc_wait:%d) (pid:%d)\n",
rc, rc_wait, (int) getpid());
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.