-
Notifications
You must be signed in to change notification settings - Fork 2
/
pi2.c
64 lines (58 loc) · 994 Bytes
/
pi2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<math.h>
#define buff 50000
float res=0;
int array[buff];
struct param
{
int start;
int end;
};
void init_array(int precision)
{
int i;
for (i=1;i<=precision;i++)
if (i%2==1)
array[i]=2*(i-1)+1;
else
array[i]=-2*(i-1)-1;
}
void *son_thread(void *argv)
{
int i;
struct param *p;
p=(struct param *)argv;
for (i=p->start;i<=p->end;i++)
res+=1.0/array[i];
}
int main()
{
int precision=40000,n,i,step;
pthread_t tid[20];
init_array(precision);
printf("计算进程数n=");
scanf("%d",&n);
step=precision/n;
for (i=1;i<=n;i++)
{
struct param *p;
p=(struct param *)malloc(sizeof(struct param));
if (i!=n)
{
p->start=1+(i-1)*step;
p->end=i*step;
}
else
{
p->start=1+(i-1)*step;
p->end=precision;
}
pthread_create(&tid[i],NULL,son_thread,p);
}
for (i=1;i<=n;i++)
pthread_join(tid[i],NULL);
printf("res = %f, PI = %f (precision:%d)\n",res,res*4,precision);
return 0;
}