fork download
  1. /* main program illustrating the UNIX fork() system call.
  2. Compile using cc -o main main.c
  3. */
  4. #include <stdio.h>
  5. int a;
  6.  
  7. void count1() {
  8. a++;
  9. printf("%d", a);
  10. }
  11.  
  12. void count2() {
  13. a += 2;
  14. printf("%d", a);
  15. }
  16.  
  17. void count3() {
  18. a += 3;
  19. printf("%d", a);
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25. count1();
  26.  
  27. int pid = fork();
  28.  
  29. if (pid > 0) {
  30. wait(NULL);
  31. count2();
  32. count1();
  33. }
  34. else if (pid == 0) {
  35. count1();
  36. count3();
  37. }
  38. //printf("Using fork() system call\n");
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
125134