fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <time.h>
  6.  
  7. int main() {
  8. srand(time(NULL));
  9. printf("A");
  10. fflush(stdout);
  11.  
  12. if (fork() == 0) {
  13. usleep(((rand() % 5) + 1) * 10000);
  14. printf("B");
  15. fflush(stdout);
  16.  
  17. if (fork() == 0) {
  18. usleep(((rand() % 5) + 1) * 10000);
  19. printf("C");
  20. fflush(stdout);
  21. } else {
  22. wait(NULL);
  23. printf("D");
  24. fflush(stdout);
  25. }
  26. } else {
  27. usleep(((rand() % 5) + 1) * 10000);
  28. printf("E");
  29. fflush(stdout);
  30. wait(NULL);
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
AEBCD