fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int main(void)
  5. {
  6. int childpid;
  7. childpid = fork();
  8. if (childpid == -1) {
  9. printf("Can't fork.\n");
  10. exit(1);
  11. }
  12. else if (childpid == 0) {
  13. printf("\nChild: child pid = %d, parent pid = %d\n", getpid(), getppid());
  14. exit(0);
  15. }
  16. else {
  17. printf("\nParent: child pid = %d, parent pid = %d\n", childpid, getpid());
  18. printf("Hai\n");
  19. exit(0);
  20. }
  21. }
  22.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Parent: child pid = 2419594, parent pid = 2419576
Hai

Child: child pid = 2419594, parent pid = 1