fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4.  
  5. int main()
  6. {
  7. pid_t pid;
  8.  
  9. /* fork a child process */
  10. pid = fork();
  11.  
  12. if (pid < 0) { /* error occurred */
  13. fprintf(stderr, "Fork Failed\n");
  14. exit(-1);
  15. }
  16. else if (pid == 0) { /* child process */
  17. printf("I am the child %d\n",pid);
  18. execlp("/bin/ls","ls",NULL);
  19. }
  20. else { /* parent process */
  21. /* parent will wait for the child to complete */
  22. printf("I am the parent %d\n",pid);
  23. wait(NULL);
  24.  
  25. printf("Child Complete\n");
  26. exit(0);
  27. }
  28. }
  29.  
Success #stdin #stdout 0s 3668KB
stdin
Standard input is empty
stdout
prog
I am the parent 5394
Child Complete