vfork.c 500 B

123456789101112131415161718192021222324
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. int main() {
  9. pid_t pid = vfork();
  10. if (pid < 0) {
  11. printf("failed on vfork (%s)\n", strerror(errno));
  12. return -1;
  13. }
  14. if (pid == 0) {
  15. printf("[pid=%d|ppid=%d] Hello, Dad!\n", getpid(), getppid());
  16. _exit(0);
  17. } else {
  18. printf("[pid=%d|ppid=%d] Hello, Kid!\n", getpid(), getppid());
  19. }
  20. return 0;
  21. }