Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Created March 22, 2019 19:20
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 jochasinga/805e72161efe020484b488a1a40afa24 to your computer and use it in GitHub Desktop.
Save jochasinga/805e72161efe020484b488a1a40afa24 to your computer and use it in GitHub Desktop.
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");
exit(1);
} 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