fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h> // For wait()
  4.  
  5. int main() {
  6. pid_t pid;
  7.  
  8. pid = fork(); // Create a new process
  9.  
  10. if (pid < 0) {
  11. // fork() failed
  12. perror("fork failed");
  13. return 1;
  14. }
  15. else if (pid == 0) {
  16. // Child process
  17. printf("I am a child, my parent process is %d\n", getppid());
  18. }
  19. else {
  20. // Parent process
  21. printf("I am parent process, my id is %d\n", getpid());
  22. wait(NULL); // Wait for child to finish
  23. }
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
I am a child, my parent process is 1781248
I am parent process, my id is 1781248