[comp.sys.amiga.tech] Handshaking

phorgan@cup.portal.com (Patrick John Horgan) (11/27/89)

---===***> Here, eat the damn thing.

Hi guys, I'm having a problem I'm hoping you can help with...I'm making
an implementation of the dining philosophers to illustrate a magazine
article and I'm fighting a bug I can't kill.  I have a re-entrant chunk
of code called philo that is used for all the philosophers and a chunk
called table that is the handler for eating utensil requests.
Table starts up the tasks, keeping a note of each of their Task struct
addresses, then each philo Task looks up the table MsgPort by name and
sends a philomsg to CHECKIN (philomsg->cmd = CHECKIN) with table...

typedef struct{                  /* With this the philosophers communicate*/
	struct Message	message; /* their desires to the table.		*/
	short		cmd,which;
	char 		msg[256];
	struct Task	*pt;
} philomsg;

Table looks for an entry in his table with the same address and assigns
the table index to the which parameter in the philomsg so we can use
it to identify each task in messages et.al.

Well everything works fine until it's time to quit.  We have successful
communications with all the philosophers, finally agree to quit, then
each philosopher executes this code...

	Forbid();
	ltlmsg->cmd = BYE;	/* Tell table that we're quitting.	*/
	PutMsg(tablemp,ltlmsg);	/* Check in	with the table task.	*/
	DeletePort(mp);		/* Get rid of my port...table won't rply*/
	Wait(0L);

When table receives a philomsg with cmd == BYE, he executes this code.

	case BYE:
		DeleteTask(tasks[ltlmsg->which]);
		FreeMem(ltlmsg,(long)sizeof(philomsg));
		++num_trmd;
	break;

He Gets rid of the Task and Frees the memory AllocMem'd for the philomsg.

The problem is that while all of the tasks execute the exit code, all of
their MsgPorts are Deleted and they all go into a NULL Wait, only the
first philomsg ever arrives at tables MsgPort!  I've checked everything
I can think of...The values used for the PutMsg are correct, (checked 
before and after the PutMsg call).  Each task has it's own different
philomsg. Each task has a different Task structure at different addresses.
Each task has a different name.  I've tried having each Task DeleteTask
themselves too but with the same results...I even tried commenting out 
the DeleteTask and FreeMem in the table code and the DeletePort in the
philos, and replacing the Wait with for(;;), but nothing helps.
What gives? :(  Can anyone help?

Disclaimer: If my employers had my opinions they'd be in a different 
            business!

Patrick - (having an Amiga fetish) - Horgan