vfork.c 484 B

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