#include #include #include #include #include #include double drandom(void) { return (double)random() / 2147483648.0; } #define TSIZE (31*sizeof(long)) /* generator state array size */ int main(int argc, char **argv) { long rounds, maxchildrounds, round, totalrounds, totalinside; long *inside, *childrounds; int arg, children, child; pid_t cpid, *cpids; int cstatus, cnum; double x, y; unsigned seed; char (*states)[TSIZE]; /* state arrays for random number generators */ if (argc < 4 || 1!= sscanf(argv[1], "%ld", &rounds) || 1!= sscanf(argv[2], "%ld", &maxchildrounds)) { fprintf(stderr,"Usage: %s rounds-total rounds-per-process seed [seed...]\n", argv[0]); return 1; } children = argc - 3; if (children > 255) { fprintf(stderr, "Sorry, I can only handle up to 255 children\n"); exit(EXIT_FAILURE); } if (!(cpids = malloc(children * sizeof(pid_t)))) { perror("malloc"); } if (!(childrounds = calloc(children, sizeof(long)))) { perror("malloc"); exit(EXIT_FAILURE); } /* state arrays must be shared */ if ((states = mmap(NULL, children*TSIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } if ((inside = mmap(NULL, children*sizeof(long), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } /* initialize state arrays for all children just once */ for (arg=3, child=0; arg %f\n", cnum, inside[cnum], childrounds[cnum], (double)inside[cnum]/childrounds[cnum] * 4); totalinside += inside[cnum]; totalrounds += childrounds[cnum]; } } printf("Total: %ld:%ld -> %f\n", totalinside, totalrounds, (double)totalinside/totalrounds * 4); return 0; }