Newbie question on message queues

Newbie question on message queues

Post by Robert Lync » Tue, 09 Dec 1997 04:00:00



Hiya-

I'm working through Chap. 12 of Matthew & Stones' "Beginning Linux
Programming", and in a sample program (msg2.c) on message queues, in the
"sender" program the following lines makes the program fail:

...
        if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
            fprintf(stderr, "msgsnd failed\n");
            exit(EXIT_FAILURE);
        }
...

After a lot of back and forth, it seems to fail because of BUFSIZ.  If I
arbitrarily make the first line:

        if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {

it works.

Any hints?  This is on Linux 2.0.32...

I don't know if I'm giving the right amount of info or not enough, thus
I append the whole program below.

TIA.  Bob L.
--

http://www.veryComputer.com/~rm*/
---

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msg_st {
    long int my_msg_type;
    char some_text[BUFSIZ];

Quote:};

int main()
{
    int running = 1;
    struct my_msg_st some_data;
    int msgid;
    char buffer[BUFSIZ];

    msgid = msgget((key_t)1234, 0666 | IPC_CREAT);

    if (msgid == -1) {
        fprintf(stderr, "msgget failed\n");
        exit(EXIT_FAILURE);
    }

    while(running) {
        printf("Enter some text: ");
        fgets(buffer, BUFSIZ, stdin);
        some_data.my_msg_type = 1;
        strcpy(some_data.some_text, buffer);

                if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {
/*        if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) { */
            fprintf(stderr, "msgsnd failed\n");
            exit(EXIT_FAILURE);
        }

        if (strncmp(buffer, "end", 3) == 0) {
            running = 0;
        }
    }

    exit(EXIT_SUCCESS);

Quote:}

------END--------
 
 
 

Newbie question on message queues

Post by jean-marc COLLI » Wed, 10 Dec 1997 04:00:00


Take a look a the kernel variable msgsize witch give the maximum size of
a message in a message queue.
I don't where you can found this variable in linux, but :

   * with SCO, type sysadmsh,
   * with Solaris the file is /etc/system.

There is other variables that may have interest. You got the total size
of all the message in one queue, the size max of one message.



 
 
 

Newbie question on message queues

Post by Thomas Rin » Wed, 10 Dec 1997 04:00:00



> Hiya-

> I'm working through Chap. 12 of Matthew & Stones' "Beginning Linux
> Programming", and in a sample program (msg2.c) on message queues, in the
> "sender" program the following lines makes the program fail:

> ...
>         if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
>             fprintf(stderr, "msgsnd failed\n");
>             exit(EXIT_FAILURE);
>         }
> ...

> After a lot of back and forth, it seems to fail because of BUFSIZ.  If I
> arbitrarily make the first line:

>         if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {

> it works.

Did you #define the BUFSIZ macro somewhere in your code? Be aware that
stdio.h #defines and
uses a BUFSIZ macro, too! Hope this helps.

            Thomas

--
Thomas Rink
Lehrstuhl f. Biophysik
Ruhr-Universitaet Bochum
D-44780 Bochum, Germany

 
 
 

Newbie question on message queues

Post by Robert Lync » Wed, 10 Dec 1997 04:00:00



> > Hiya-

> > I'm working through Chap. 12 of Matthew & Stones' "Beginning Linux
> > Programming", and in a sample program (msg2.c) on message queues, in the
> > "sender" program the following lines makes the program fail:

> > ...
> >         if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
> >             fprintf(stderr, "msgsnd failed\n");
> >             exit(EXIT_FAILURE);
> >         }
> > ...

> > After a lot of back and forth, it seems to fail because of BUFSIZ.  If I
> > arbitrarily make the first line:

> >         if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {

> > it works.

For anyone interested...

I've gotten three replies so far, that all correctly point to the
problem being that BUFSIZ  probably exceeds MSGMAX (maximum size of a
message queue msg.).  After investigation I found with my system that
these are set to 8092 and 4080 bytes respectively.  Oddly, replacing
BUFSIZ with 4080 - 1 bytes doesn't work; I didn't try all values, but
when I went down to 4000 bytes it did finally work.

(This was a very useful exercise in that I found out how to get the
value of a given define, besides reading through the header files, which
tend to define one define in terms of another.  My first trick was to
define, say, BUFSIZ myself, then the compiler warned me it was also
defined in a certain header file and even gave the line number, then I
could look there easily!  The second way was to run the source code
through the preprocessor and look at the output...)

OK, the question that remains in my mind is why such experienced
programmers of such an excellent book would have made a "mistake" like
this?  Presumably they tested their code before releasing it?  Perhaps
in some library upgrade BUFSIZE was increased to 8092 bytes, thus
breaking the code with an "old" msgsnd, MAXMSG = 4080 bytes?  Is this
possible, or are defines like BUFSIZ, etc. more "frozen" than this view
would imply and it's more probably a programmer's error?

Sorry if this seems like I am creating a tempest out of a teapot.  Just
trying to plumb the depths of this mysterious grab-bag of lore called
"Unix programming".  ;-)

And my grateful thanks to those who took the time to reply.

Bob L.
--

http://www.veryComputer.com/~rm*/

 
 
 

Newbie question on message queues

Post by John A. Krol » Wed, 10 Dec 1997 04:00:00


> OK, the question that remains in my mind is why such experienced
> programmers of such an excellent book would have made a "mistake" like
> this?  Presumably they tested their code before releasing it?  Perhaps
> in some library upgrade BUFSIZE was increased to 8092 bytes, thus
> breaking the code with an "old" msgsnd, MAXMSG = 4080 bytes?  Is this
> possible, or are defines like BUFSIZ, etc. more "frozen" than this view
> would imply and it's more probably a programmer's error?

> Sorry if this seems like I am creating a tempest out of a teapot.  Just
> trying to plumb the depths of this mysterious grab-bag of lore called
> "Unix programming".  ;-)

> And my grateful thanks to those who took the time to reply.

> Bob L.
> --

> http://www.veryComputer.com/~rm*/

If you give them the benefit of the doubt, maybe the #define was left out as an
editing mistake.
The other option is that the system they tested the code on had the kernel
parameters increased
to support larger messages.

John Kroll

 
 
 

Newbie question on message queues

Post by Claudio Cors » Sun, 14 Dec 1997 04:00:00



> Hiya-

> I'm working through Chap. 12 of Matthew & Stones' "Beginning Linux
> Programming", and in a sample program (msg2.c) on message queues, in the
> "sender" program the following lines makes the program fail:

> ...
>         if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
>             fprintf(stderr, "msgsnd failed\n");
>             exit(EXIT_FAILURE);
>         }
> ...

> After a lot of back and forth, it seems to fail because of BUFSIZ.  If I
> arbitrarily make the first line:

>         if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {

> it works.

> Any hints?  This is on Linux 2.0.32...

> I don't know if I'm giving the right amount of info or not enough, thus
> I append the whole program below.

> TIA.  Bob L.
> --

> http://www.veryComputer.com/~rm*/
> ---

> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>

> #include <sys/types.h>
> #include <sys/ipc.h>
> #include <sys/msg.h>

> struct my_msg_st {
>     long int my_msg_type;
>     char some_text[BUFSIZ];
> };

> int main()
> {
>     int running = 1;
>     struct my_msg_st some_data;
>     int msgid;
>     char buffer[BUFSIZ];

>     msgid = msgget((key_t)1234, 0666 | IPC_CREAT);

>     if (msgid == -1) {
>         fprintf(stderr, "msgget failed\n");
>         exit(EXIT_FAILURE);
>     }

>     while(running) {
>         printf("Enter some text: ");
>         fgets(buffer, BUFSIZ, stdin);
>         some_data.my_msg_type = 1;
>         strcpy(some_data.some_text, buffer);

>                 if (msgsnd(msgid, (void *)&some_data, 1024, 0) == -1) {
> /*        if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) { */
>             fprintf(stderr, "msgsnd failed\n");
>             exit(EXIT_FAILURE);
>         }

>         if (strncmp(buffer, "end", 3) == 0) {
>             running = 0;
>         }
>     }

>     exit(EXIT_SUCCESS);
> }
> ------END--------

  Hello Robert!

You are sending the some_data to the message queue. What I have read the
value BUFSIZ or 1024 is the size of the passed structure some_data. If your
structure is smaller then this can cause a problem. Thus replace BUFSIZ or
1024 with sizeof( some_data ). This should resolve the problem. Also,
determine that some_text can contain the data in the buffer. This can be
another reason why your program has a problem.

Claudio

 
 
 

1. Newbie question: message queues

I am currently writing an application on HP-UX 10.20 using
message queues as the IPC medium.  What I'm wondering is,
are messages removed from the queue after a msgrcv()?  If
not, is there a way to remove them?  If not, is there a
maximum size the queue may grow, what is it, and how should
an app accomodate for it?

Any assistance is greatly appreciated.

Thanks in advance,

KTK

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Security??? Server: Netscape-Enterprise/2.0a

3. Error Message of Message Queue

4. I need to start a terminal

5. HELP! encoding messages for message queues

6. Help! Linux Install Can't See Second Drive.

7. Deleting a message from a message queue??

8. Linux driver for Equinox SST board

9. HELP!: encoding messages for message queues

10. HELP! encoding messages for message queues

11. how to see messages in message queue ...

12. SysV message queues question

13. Message queues vs sockets question...