fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main() {
  5. int pid1, pid2, pid3, pid4, pid5, pid6;
  6.  
  7. pid1 = fork();
  8.  
  9. if (pid1 == 0) {
  10. pid2 = fork();
  11. printf("A \n");
  12. } else {
  13. char *execArgs[] = { "echo", "Process replaced!", NULL };
  14. execvp("echo", execArgs);
  15. }
  16.  
  17. printf("B \n");
  18.  
  19. pid3 = fork();
  20.  
  21. if (pid4 != 0) {
  22. printf("C \n");
  23. char *execArgs[] = { "echo", "Process replaced!", NULL };
  24. execvp("echo", execArgs);
  25. } else {
  26. if (pid1 != 0) {
  27. pid5 = fork();
  28. char *execArgs[] = { "echo", "Process replaced!", NULL };
  29. execvp("echo", execArgs);
  30. printf("D \n");
  31. }
  32. }
  33.  
  34. if (pid2 > 0) {
  35. pid6 = fork();
  36. printf("E \n");
  37. } else {
  38. printf("F \n");
  39. char *execArgs[] = { "echo", "Process replaced!", NULL };
  40. execvp("echo", execArgs);
  41. }
  42.  
  43. printf("G \n");
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
A 
B 
E 
G 
A 
B 
E 
G 
A 
B 
E 
G 
A 
B 
E 
G 
Process replaced!
Process replaced!
Process replaced!