/*
pvm-test.master.c - queries slave programs for their process id and hostname
Jeffrey Grafton <jgrafton@tjhsst.edu> - 11/12/2002 15:57

This program is released under the GNU General Public License.
A copy of this license is available online at
http://www.gnu.org/copyleft/gpl.html.
*/

#include <stdio.h>
#include <pvm3.h>

#define SLAVENAME "slave" /* name of slave program */
#define MAXTASKS 10 /* maximum number of processes to spawn */
#define SEND_MSGTYPE 1337 /* arbitrary number to specify message type when communicating */
#define RECV_MSGTYPE 4321 /* again, arbitrary */
#define HOSTNAME_MAXLEN 50

int
main(void)
{
    int num_tasks, i, id, pid;
    int my_tid;  /* my task id */
    int tids[MAXTASKS]; /* slave task ids */
    char hostname[HOSTNAME_MAXLEN];
    struct pvmhostinfo *hostp[MAXTASKS]; /* pointers to host information */

/* Enroll in pvm */
    my_tid = pvm_mytid();

/* Start up slave tasks */  
    num_tasks = pvm_spawn(SLAVENAME, (char**)0, 0, "", MAXTASKS, tids);

/* Send data to each slave */
    for (i=0; i < MAXTASKS; i++)    /* Cycle through all processes */
    {
        pvm_initsend(PvmDataDefault); 	/* Get ready to send message buffer */
        pvm_pkint(&i, 1, 1);    /* Pack value to send */
        pvm_send(tids[i], SEND_MSGTYPE);	/*i Send to identified process */
    }

/* Receive data from each slave */
    for(i=0 ; i < MAXTASKS ; i++)   /* Cycle through all processes */
    {
        pvm_recv(-1, RECV_MSGTYPE);	/* Wait for message of right type */
        pvm_upkint(&id, 1, 1);  /*  Find out who sent message */
        pvm_upkint(&pid, 1, 1); /* get pid of returing program */
        pvm_upkstr(hostname);   /* Unpack Reciprocal */

/* Display results */
    printf("ID: %d\tProcess: %d \tHostname: %s\n", id, pid, hostname);
    }

    return pvm_exit();
}


