vfork.c 660 B

123456789101112131415161718192021222324252627
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. int main() {
  10. pid_t pid = vfork();
  11. if (pid < 0) {
  12. printf("failed on vfork (%s)\n", strerror(errno));
  13. return -1;
  14. }
  15. if (pid == 0) {
  16. printf("[pid=%d|ppid=%d] Hello, Dad!\n", getpid(), getppid());
  17. _exit(0);
  18. }
  19. else {
  20. printf("[pid=%d|ppid=%d] Hello, Kid!\n", getpid(), getppid());
  21. }
  22. return 0;
  23. }