fork download
  1. #include <stdio.h> // printf()
  2. #include <stdlib.h> // rand()
  3. #include <unistd.h> // fork(), usleep()
  4. #include <sys/wait.h> // wait()
  5.  
  6. int main() {
  7. printf("A\n");
  8. if (fork() == 0) {
  9. usleep((rand() % 5 + 1) * 100000); // wait for a random time
  10. printf("B\n");
  11. if (fork() == 0) {
  12. usleep((rand() % 5 + 1) * 100000);
  13. printf("C\n");
  14. } else {
  15. wait(NULL);
  16. printf("D\n");
  17. }
  18. } else {
  19. usleep((rand() % 5 + 1) * 100000);
  20. printf("E\n");
  21. wait(NULL);
  22. }
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
A
B
C
A
B
D
A
E