Help needed with System V message queues

George Bogatko bogatko at lzga.ATT.COM
Thu Aug 9 02:51:25 AEST 1990


Postnews (or something) trashed the ending of the message queue article.
Here's the ending.

******

When sending messages, the third parameter must be the size of
the actual message, not the size of the 'struct msgbuf', which
will be sizeof(long) bytes bigger.  If you don't pay attention
to this, you will get message trashing.   I can't recall now how,
when, or why this happens, but I guarantee that it will be mysterious,
and usually fatal.

		 struct msgbuf {
			 long    mtype;	  /* message type */
			 char    mtext[1];       /* message text */
		 };

So the safe way to use 'msgsnd' is:

typedef struct {
	long mtype;
	struct {
		char buf[10];
		int xxx;
		double yyy;
		etc...
	} mtext;
} MSG;

MSG message;

	1. msgsnd( msqid, &message, sizeof(message.mtext), 0);
OR
	2. msgsnd( msqid, &message, sizeof(message)-sizeof(long), 0);

I prefer #1.

I also prefer to send message with the fourth parameter as 0.  This will
make the message block if there is no room at the time.  It is more normal
for the message to block in a heavily loaded system then not, so you
will probably NOT want to die if you can't send the message because
there's no room.  If you are worried about deadlock, put in an
alarm call so you can time out.

When receiving messages, check for EINTR if you get a -1.
You will usually want to just wrap around and try again if you get
an interrupt (usually from an alarm call, or some other friendly signal).


Hope this (long-winded) yakking helps somebody.


GB



More information about the Comp.unix.wizards mailing list