vfork.c 480 B

1234567891011121314151617181920212223
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <unistd.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. } else {
  17. printf("[pid=%d|ppid=%d] Hello, Kid!\n", getpid(), getppid());
  18. }
  19. return 0;
  20. }