Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Name: Sushant Baskota*/
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define NUMBER_OF_POINTS 50000000
- void *runner (void *param);
- int circle_count = 0;
- double random_double ()
- {
- return random () / ((double) RAND_MAX + 1);
- }
- int main (int argc, char *argv[])
- {
- if(argc<2||argc>2){
- printf("<number of threads> should be a positive integer");
- return 0;
- }
- int thikxa =atoi(argv[1]);
- if(thikxa<=0){
- printf("Number of threads needs to be more than 0");
- return 0;
- }
- int points_per_thread = NUMBER_OF_POINTS / thikxa;
- int i;
- double Pi;
- pthread_t workers[thikxa];
- srandom((unsigned)time(NULL));
- for(i=0; i<thikxa; i++)
- pthread_create(&workers[i], 0, runner, &points_per_thread);
- for(i=0; i<thikxa; i++)
- pthread_join(workers[i], NULL);
- Pi = 4.0*circle_count/NUMBER_OF_POINTS;
- printf("Pi: = %f\n", Pi);
- return 0;
- }
- void *runner(void *param)
- {
- int POINTS;
- POINTS = *((int *)param);
- int i;
- int hit_count = 0;
- double x,y;
- for(i=0; i<POINTS; i++)
- {
- x=random_double()*2.0-1.0;
- y=random_double()*2.0-1.0;
- if(sqrt(x*x+y*y)<1.0)
- ++hit_count;
- }
- circle_count += hit_count;
- pthread_exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement