Newsgroups: comp.parallel.pvm
From: manchek@thud.CS.UTK.EDU (Bob Manchek)
Subject: Re: fork() and PVM
Organization: Computer Science Dept, University of Tennessee, Knoxville
Date: 22 Sep 1995 14:32:37 GMT
Message-ID: <43uhe6INN9j6@CS.UTK.EDU>

In article <43sch9$gh3@sakarya.cs.umd.edu>, jimduff@cs.umd.edu (Jim Duff) writes:

|>   I'd like to fork() a child process from a parent attached to PVM,
|> and then have the parent and child talk to each other using PVM
|> routines.  Is this even possible, and if so, how?
|> 
|> I wrote a short test program to do this, but the child
|> process dies right away.

You can do it, you just need to use a trick to disconnect the child
process from PVM (it thinks it's already connected with the same
task id).  I'd suggest trying the following function.
-b
------------------------------------------------------------------------
#include <pvm3.h>


int
pvm_fork(cpid)
	int *cpid;		/* returns child pid if nonnull */
{
	int ptid;			/* tid of parent */
	int tid;			/* tid to return */
	int pid;			/* pid of child */
	int sbf, rbf;

	sbf = pvm_setsbuf(0);
	rbf = pvm_setrbuf(0);

	if ((ptid = pvm_mytid()) < 0) {
		tid = ptid;
		goto done;
	}

	if (pid = fork()) {		/* parent */
		if (pid == -1) {
/*
			perror("fork");
*/
			tid = PvmOutOfRes;
			goto done;
		}

		tid = pvm_recv(-1, 1);
		if (tid < 0)
			goto done;
		tid = -1;
		pvm_upkint(&tid, 1, 1);

		if (cpid)
			*cpid = pid;

	} else {				/* child */
		pvmendtask();
		tid = pvm_mytid();
		pvm_initsend(PvmDataRaw);
		pvm_pkint(&tid, 1, 1);
		pvm_send(ptid, 1);
		tid = 0;
	}

done:
	if (sbf = pvm_setsbuf(sbf))
		pvm_freebuf(sbf);
	if (rbf = pvm_setrbuf(rbf))
		pvm_freebuf(rbf);
	return tid;
}
------------------------------------------------------------------------

/ Robert Manchek                University of Tennessee     /
/                               Computer Science Department /
/ (615)974-8295                 Ayres Hall #104             /
/ manchek@CS.UTK.EDU.           Knoxville TN  37996-1301    /
/     http://www.netlib.org/utk/people/BobManchek.html      /


