fork download
  1. #include <stdlib.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6. #include <time.h>
  7.  
  8. pthread_t thr_counter[8];
  9.  
  10. unsigned long n_odd = 0;
  11. unsigned long n_even = 0;
  12.  
  13. void *counter(void *arg) {
  14. int n;
  15. int *c = (int*)arg;
  16. double d;
  17.  
  18. for (int k=0; k<*c; k++) {
  19. n = rand()%100;
  20. for (int j=0; j<n; j++)
  21. d = sqrt((double)j);
  22. if (n%2 == 0)
  23. n_even++;
  24. else
  25. n_odd++;
  26. }
  27.  
  28. pthread_exit(NULL);
  29. }
  30.  
  31. int main() {
  32. int cnt=1000000, k;
  33. srand(time(NULL));
  34. for (k=0; k<8; k++) {
  35. pthread_create(&thr_counter[k], NULL, counter, (void*)&cnt);
  36. }
  37.  
  38. for (k=0; k<8; k++) {
  39. pthread_join(thr_counter[k], NULL);
  40. }
  41.  
  42. printf("Broj parnih: \t %lu\n", n_even);
  43. printf("Broj neparnih: \t %lu\n", n_odd);
  44. printf("Ukupno: \t %lu\n", n_odd+n_even);
  45. return 0;
  46. }
Success #stdin #stdout 0.61s 5328KB
stdin
Standard input is empty
stdout
Broj parnih:   	 3998208
Broj neparnih: 	 4001792
Ukupno:        	 8000000