Newsgroups: comp.parallel.pvm
Path: ukc!uknet!pipex!howland.reston.ans.net!torn!nott!cunews!mach10.scs.carleton.ca!davison
From: davison@mach10.scs.carleton.ca (robert davison)
Subject: master waits forever on pvm_recv()
Message-ID: <CLJrvw.MG@cunews.carleton.ca>
Sender: news@cunews.carleton.ca (News Administrator)
Organization: Carleton University, Ottawa, Canada
Date: Sun, 20 Feb 1994 23:57:32 GMT
Lines: 107

I writing a typical master/slave program with a little twist.
The slave process forks to run another program (call it slave2).
Slave2 does the actual work and sends the results to slave via pipes,
which are forwarded to the master process by slave via pvm calls.
My problem is that the master process blocks forever when it does
a pvm_recv(...) to get the results forwarded by slave.
All other communication works fine ie between slave/slave2 master/slave.

Below are the important parts of my master and slave code.

main(argc, argv)
int argc; char *argv[];
{
	int my_tid;
	int i, rc;
	int done;
	int *slave_tids;
	int num_success;
	char result[MAX_LINE_LEN];

	construct_msgs( argv[1] );

	my_tid = pvm_mytid();
	rc = pvm_initsend( PvmDataDefault );
	rc = pvm_pkstr( slave_msg1 );
	rc = pvm_pkstr( slave_msg2 );

	done = FALSE;
	while( !done )
	{ 
		slave_tids = (int*)malloc(sizeof(int) * NUM_SLAVES); 
		num_success = pvm_spawn(SLAVETASK, (char**)0, PvmTaskDefault,
										"", NUM_SLAVES, slave_tids);

		rc = pvm_mcast(slave_tids, num_success, MASTER_MSG_TAG);

		for(i=0; i<5; i++)     /* the 5 is not important */
		{	 
			pvm_recv(-1, SLAVE_MSG_TAG); --> blocks forever on first iteration of loop!
			pvm_upkstr( result );
			printf("\n%s", result);
		}
		done = TRUE;
	}

	pvm_exit();
	exit(0);
}


main()
{
	int pipe1[2], pipe2[2], childpid, n;
	int my_tid;
	int master;
	char exec_name[MAX_LINE_LEN];
	char exec_params[MAX_PARAMS_LEN];
	char result[MAX_LINE_LEN];


	my_tid = pvm_mytid();
	master = pvm_parent(); 
	pvm_recv(-1, MASTER_MSG_TAG);
	pvm_upkstr( exec_name );
	pvm_upkstr( exec_params );

	pipe(pipe1);
	pipe(pipe2);
	childpid = fork();
	if( childpid > 0 )
	{
		close(pipe1[0]);
		close(pipe2[1]);

		write(pipe1[1], exec_params, strlen(exec_params));  
		while( (n = read(pipe2[0], result, MAX_LINE_LEN)) > 0 )
		{
			pvm_initsend( PvmDataDefault );
			pvm_pkstr( result );
			pvm_send(master, SLAVE_MSG_TAG); ------------> forward result to master

		} 

		while( wait((int*)0) != childpid )
		{
			if( wait((int*)0) == -1 ) break;
		}

		pvm_exit();
		exit(0); 
	}
	else
	{
		close(0);
		dup(pipe1[0]);
		close(1);
		dup(pipe2[1]);

		close(pipe1[0]);
		close(pipe1[1]);
		close(pipe2[1]);
		close(pipe2[0]); 

		execlp(exec_name,exec_name,(char*)0);
		exit(0);
	}
}

