Next: Kernel msg structure Up: Internal and User Data Previous: Internal and User Data

#### Message buffer

The first structure we'll visit is the `msgbuf` structure. This particular data structure can be thought of as a template for message data. While it is up to the programmer to define structures of this type, it is imperative that you understand that there is actually a structure of type `msgbuf`. It is declared in `linux/msg.h` as follows:

    /* message buffer for msgsnd and msgrcv calls */
     struct msgbuf {
         long mtype;         /* type of message */
         char mtext[1];      /* message text */
     };

There are two members in the `msgbuf` structure:

`mtype`

  The message type, represented in a positive number. This must be a positive number!

`mtext`

  The message data itself.

The ability to assign a given message a type, essentially gives you the capability to multiplex messages on a single queue. For instance, client processes could be assigned a magic number, which could be used as the message type for messages sent from a server process. The server itself could use some other number, which clients could use to send messages to it. In another scenario, an application could mark error messages as having a message type of 1, request messages could be type 2, etc. The possibilities are endless.

On another note, do not be misled by the almost too-descriptive name assigned to the message data element (`mtext`). This field is not restricted to holding only arrays of characters, but any data, in any form. The field itself is actually completely arbitrary, since this structure gets redefined by the application programmer. Consider this redefinition:

    struct my_msgbuf {
             long    mtype;          /* Message type */
             long    request_id;     /* Request identifier */
             struct  client info;    /* Client information structure */
     };

Here we see the message type, as before, but the remainder of the structure has been replaced by two other elements, one of which is another structure! This is the beauty of message queues. The kernel makes no translations of data whatsoever. Any information can be sent.

There does exist an internal limit, however, of the maximum size of a given message. In Linux, this is defined in `linux/msg.h` as follows:

    #define MSGMAX  4056   /* <= 4056 */   /* max size of message (bytes) */

Messages can be no larger than 4,056 bytes in total size, including the `mtype` member, which is 4 bytes in length (`long`).

Next: Kernel msg structure Up: Internal and User Data Previous: Internal and User Data