//安全状态的判断函数 int safe(int location) { // 若状态安全则返回true if ((goat(location) == cabbage(location)) && (goat(location) != farmer(location)) ) // 羊吃白菜 return 0; if ((goat(location) == wolf(location)) && (goat(location) != farmer(location))) // 狼吃羊 return 0; return 1; // 其他状态是安全的 }
int main(int argc, char **argv ) { int i, myid, numprocs; double area, a, b; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myid); if (numprocs < 2) { fprintf(stderr, "ERROR: Must have at least 2 processes to run\n"); MPI_Finalize(); exit(1); } if (myid == 0) { // Farmer // init counters tasks_per_process = (int *) malloc(sizeof(int)*(numprocs)); for (i=0; i<numprocs; i++) { tasks_per_process[i]=0; } } if (myid == 0) { // Farmer area = farmer(numprocs); } else { //Workers worker(myid); } if (myid == 0) { fprintf(stdout, "Area=%lf\n", area); fprintf(stdout, "\nTasks Per Process\n"); for (i=0; i<numprocs; i++) { fprintf(stdout, "%d\t", i); } fprintf(stdout, "\n"); for (i=0; i<numprocs; i++) { fprintf(stdout, "%d\t", tasks_per_process[i]); } fprintf(stdout, "\n"); free(tasks_per_process); } MPI_Finalize(); return 0; }