Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. double pi() {
  7. srand(time(NULL) + getpid());
  8. int n = 10e7;
  9. int k = 0;
  10. double x, y;
  11. for(int i = 0; i < n; i++) {
  12. x = (double)rand()/RAND_MAX;
  13. y = (double)rand()/RAND_MAX;
  14. if( x*x + y*y <= 1)
  15. k++;
  16. }
  17. double wynik = (double)(k)/n;
  18. return wynik*4;
  19.  
  20. }
  21. int main() {
  22. for(int i = 0; i < 10; i++) {
  23. int pid = fork();
  24. if(pid == 0) {
  25. printf("%f\n", pi());
  26. break;
  27. } else {
  28. waitpid( pid, NULL, 0);
  29. }
  30. }
  31.  
  32.  
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement