fork(1) download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5.  
  6. int main()
  7. {
  8. pid_t c1, c2;
  9. float a;
  10.  
  11. printf("Enter value of a: ");
  12. scanf("%f", &a);
  13.  
  14. c1 = fork();
  15.  
  16. if (c1 == 0)
  17. {
  18. printf("First Child (Circle)\n");
  19. printf("Area of Circle = %.2f\n", 3.14 * a * a);
  20. printf("Perimeter of Circle = %.2f\n", 2 * 3.14 * a);
  21. return 0;
  22. }
  23.  
  24. c2 = fork();
  25.  
  26. if (c2 == 0)
  27. {
  28. printf("Second Child (Square)\n");
  29. printf("Area of Square = %.2f\n", a * a);
  30. printf("Perimeter of Square = %.2f\n", 4 * a);
  31. return 0;
  32. }
  33.  
  34. wait(NULL);
  35. wait(NULL);
  36.  
  37. printf("Parent: Both children completed\n");
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5316KB
stdin
7.5
stdout
Enter value of a: Second Child (Square)
Area of Square = 56.25
Perimeter of Square = 30.00
Enter value of a: First Child (Circle)
Area of Circle = 176.62
Perimeter of Circle = 47.10
Enter value of a: Parent: Both children completed