Master Source Code For Communication Example
/* master program for the simple communication program */
/* which starts slaves just to get their names and the */
/* local time back */
#include <stdio.h>
#include <pvm3.h>
main()
{
struct pvmhostinfo *hostp;
int result, check, i, nhost, narch, stid;
char buf[64];
pvm_setopt(PvmRoute, PvmRouteDirect); /* channel for communication */
gethostname(buf, 20); /* get name of master */
printf("The master process runs on %s \n", buf);
/* get and display configuration of the parallel machine */
pvm_config( &nhost, &narch, &hostp ); /* get configuration */
printf("I found the following hosts in your virtual machine\n");
for (i = 0; i < nhost; i++)
{
printf("\t%s\n", hostp[i].hi_name);
}
for (i=0; i<nhost; i++) /* spawn processes on */
{ /* all physical machines */
check=pvm_spawn("answer", 0,PvmTaskHost,hostp[i].hi_name, 1, &stid);
if (!check) printf("Couldn't start process on %s\n", hostp[i].hi_name);
}
result=0;
while (result<nhost)
{
pvm_recv(-1, 2); /* wait for reply message */
pvm_upkstr(buf); /* unpack message */
printf("%s\n", buf); /* print contents */
result++; /* next message */
}
pvm_exit; /* we are done */
}
Download: A html-free code you can save
Back: The first program - communication is everything