/* "q1_master.txt" *Question #1, Master/Slave to add up N numbers (integers only) * (i think there's a bug if you enter a '1' for num items, hangs) * master sends tids to all, and receives sum result from the * Nth slave (only one recv in the master process). * * tjn 3:54 PM 3/2/00 * * MASTER */ #include #include #define MAX 100 int main() { int i,mytid,tids[MAX], n, ttl; ttl=n=0; printf("Enter number of items (<=20): "); scanf("%d", &n); mytid = pvm_mytid(); if( pvm_spawn("slave", (char **)0, 0, "", n, tids) == n) { pvm_initsend(PvmDataDefault); pvm_pkint(&n, 1, 1); pvm_pkint(tids, n, 1); pvm_mcast(tids, n, 1); /* just to be careful we'll specify the N'th one */ pvm_recv(tids[(n-1)], 1); pvm_upkint(&ttl, 1, 1); printf("The sum of the numbers is: %d\n", ttl); } else fprintf(stderr, "ERROR: unable to spawn %d slaves\n", n); pvm_exit(); return 0; }