/* * MailBoy.c - Handles all of the messages between processes. * * By: Theo Van Dinter - 96/09/09 * * I called this 'mailboy' since these set of functions handle mail delivery. * */ #include "mailbox.h" /* Mr. Standard Header */ /* * SendMsg - 96/09/09 * * By: Theo Van Dinter (felicity@kluge.net) * * Send a message to a mailbox. * * Parameters: iTo = Mailbox to send to * pMsg = Pointer to message structure * * Returns -1 on error, 0 if success. * */ int SendMsg(int iTo, struct msg *pMsg) { if (swait(semid[2*iTo])==-1) /* Wait to send message */ return(-1); if (pMsg!=(struct msg *)NULL) /* Assuming we didn't get a NULL message to send, copy the values into the mailbox message */ { inbox[iTo]->iFrom=pMsg->iFrom; inbox[iTo]->wType=pMsg->wType; inbox[iTo]->wVal1=pMsg->wVal1; inbox[iTo]->wVal2=pMsg->wVal2; inbox[iTo]->wTotal=pMsg->wTotal; } if (ssignal(semid[2*iTo+1])==-1) /* Signal any message receivers */ return(-1); return(0); } /* * RecvMsg - 96/09/09 * * By: Theo Van Dinter (felicity@kluge.net) * * Returns the message from a mailbox. * * Parameters: iFrom = Mailbox to retrieve the message from. * pMsg = Point to message structure. * * Returns -1 on error, 0 if success. * */ int RecvMsg(int iFrom, struct msg *pMsg) { if (swait(semid[2*iFrom+1])==-1) /* Wait for a message */ return(-1); if (pMsg!=(struct msg *)NULL) /* We can't receive into a NULL structure. Otherwise, copy the mailbox message into the message structure */ { pMsg->iFrom=inbox[iFrom]->iFrom; pMsg->wType=inbox[iFrom]->wType; pMsg->wVal1=inbox[iFrom]->wVal1; pMsg->wVal2=inbox[iFrom]->wVal2; pMsg->wTotal=inbox[iFrom]->wTotal; } if (ssignal(semid[2*iFrom])==-1) /* Signal any message senders */ return(-1); return(0); }