[comp.unix.i386] Message queues. Possibly dumb mistake.

geoff@antel.uucp (Geoff Vona) (12/07/89)

Hope someone can help me with this.  Its gotta be something stupid that
I'm doing, but...

I'm trying to use messages and queues under 386/ix 2.0.2 and I'm having
problems.  In msgsnd and msgrcv, messages are passed using a structure
called msgbuf that looks like this:

struct msgbuf {
long mtype;
char mtext[1];
};

(from /usr/include/sys/msg.h)

However, my documentation (?) sez it should be

struct msgbuf {
long mtype;
char mtext[];
};

What I want to do is pretty simple: Assign a string of text (something
profound like "This is a test") to this buffer and send it to another
process that I start up. In other words;

strcpy(gumby.mtext,"This is a test");
gumby.mtype = 2;
result = msgsnd(qid,&gumby,strlen(gumby.mtext),flag);

Unfortunately, I can't create any space for the buffer because its
only a char[1].

Maybe I'm using the wrong mechanism?  (Maybe I should be sent to remedial C
school!)  Please respond by e-mail if the solution is simple and would
simply clutter up the net.

-- 
Geoff Vona                 	| "One likes to believe 
Antel Optronics Inc. 		| In the freedom of music
3325B Mainway Burlington,	| But glittering prizes
Ontario, Canada  L7M 1A6	| And endless compromises

allbery@NCoast.ORG (Brandon S. Allbery) (12/09/89)

As quoted from <1989Dec7.150205.12104@antel.uucp> by geoff@antel.uucp (Geoff Vona):
+---------------
| struct msgbuf {
| long mtype;
| char mtext[1];
| };
+---------------

"char xxx[1];" is a convention for an "extensible" structure; you can't declare
an array of size 0, so you declare it of size 1 and allocate it as

	struct msgbuf *p = malloc(sizeof *p + msglen - 1);

This, BTW, is a rather miserable -- and potentially non-portable -- way of
doing things; a better design would have been to pass mtype as an argument
on msgsnd() and return it from msgrcv(), and pass the message itself as a
(char *) (or (void *) for ANSI C folks).  But they didn't ask my opinion
before designing it.

+---------------
| struct msgbuf {
| long mtype;
| char mtext[];
| };
+---------------

This is illegal C; it does, however, express that the array mtext extends
immediately after the structure.

This topic comes up every so often, so I've decided to post.

++Brandon
-- 
Brandon S. Allbery    allbery@NCoast.ORG, BALLBERY (MCI Mail), ALLBERY (Delphi)
uunet!hal.cwru.edu!ncoast!allbery ncoast!allbery@hal.cwru.edu bsa@telotech.uucp
*(comp.sources.misc mail to comp-sources-misc[-request]@backbone.site, please)*
*Third party vote-collection service: send mail to allbery@uunet.uu.net (ONLY)*
expnet.all: Experiments in *net management and organization.  Mail me for info.

alex@xicom.uucp (Alex Laney) (12/21/89)

In article <1989Dec7.150205.12104@antel.uucp> geoff@antel.uucp (Geoff Vona) writes:
>
>I'm trying to use messages and queues under 386/ix 2.0.2 and I'm having
>problems.  In msgsnd and msgrcv, messages are passed using a structure
>called msgbuf that looks like this:
>
>struct msgbuf {
>long mtype;
>char mtext[1];
>};
>
>(from /usr/include/sys/msg.h)
You can use these one-byte messages if you want ...

but I define a struct like this ...

struct {
long mtype;
char buf[2000];
}

and it works. You must have a buffer large enough on both ends for
your messages...



-- 
Alex Laney, Xicom Group, National Semiconductor, Ottawa, Canada (613) 728-9099
uunet!mitel!sce!xicom!alex (alex@xicom.uucp)     Fax: (613) 728-1134
"You save time, increase the amount of work done and it is easy."