/* This program computes the value of PI */ #include #include #include int main(int argc, char *argv[]) { int done = 0, n, myid, numprocs, i, rc; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, sum1, x, a; int fds[2]; if (argc != 2) { printf("Usage: a.out N\n"); exit(0); } n = atoi(argv[1]); pipe(fds); if (fork() == 0) { h = 1.0 / (double) n; sum = 0.0; for (i = n/2; i <= n; i ++) { x = h * ((double)i - 0.5); sum += 4.0 / (1.0 + x*x); } write(fds[1], &sum, sizeof(sum)); exit(0); } h = 1.0 / (double) n; sum = 0.0; for (i = 1; i < n/2; i ++) { x = h * ((double)i - 0.5); sum += 4.0 / (1.0 + x*x); } read(fds[0], &sum1, sizeof(sum)); mypi = h * (sum+sum1); printf("pi is approximately %.16f, Error is %.16f\n", mypi, fabs(mypi - PI25DT)); return 0; }