Created
March 22, 2019 19:20
-
-
Save jochasinga/805e72161efe020484b488a1a40afa24 to your computer and use it in GitHub Desktop.
Fork and wait system calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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