fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. void forkexample()
  6. {
  7. pid_t p;
  8. p = fork();
  9. if(p<0)
  10. {
  11. perror("fork fail");
  12. exit(1);
  13. }
  14. // child process because return value zero
  15. else if ( p == 0)
  16. printf("Hello from Child!\n");
  17.  
  18. // parent process because return value non-zero.
  19. else
  20. printf("Hello from Parent!\n");
  21. }
  22. int main()
  23. {
  24. forkexample();
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Hello from Parent!
Hello from Child!