fork download
  1. #include <stdio.h>
  2. #include <sys/wait.h> /* contains prototype for wait */
  3. int main(void)
  4. {
  5. int pid;
  6. int status;
  7. printf("Hello World!\n");
  8. pid = fork( );
  9. if(pid == -1) /* check for error in fork */
  10. {
  11. perror("bad fork");
  12. exit(1);
  13. }
  14. if (pid == 0) {
  15. printf("I am the child process.\n");
  16. printf("My Process ID (PID) is: %d\n", getpid());
  17. printf("My Parent's PID is: %d\n", getppid()); }
  18. else
  19. {
  20. wait(&status); /* parent waits for child to finish */
  21. printf("I am the parent process.\n");
  22. printf("My PID is: %d\n", getpid());
  23. } }
Success #stdin #stdout 0s 548KB
stdin
Standard input is empty
stdout
Hello World!
I am the child process.
My Process ID (PID) is: 3
My Parent's PID is: 2
Hello World!
I am the parent process.
My PID is: 2