0
votes
  1. what is msgtype in msgrcv system call? what is meaning of msg types? Please explain in begineers langauge?? int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

  2. is it correct if I use struct for message queue as mentioned below

    struct mybuff { long mtype int flag char buff[50] }buf

    and while sending msgsnd(msgid,&buf,sizeof(buf),0)

2

2 Answers

1
votes

From the documentation:

The argument msgtyp specifies the type of message requested as follows:

*

If msgtyp is 0, then the first message in the queue is read.

*

If msgtyp is greater than 0, then the first message in the queue of type msgtyp is read, unless MSG_EXCEPT was specified in msgflg, in which case the first message in the queue of type not equal to msgtyp will be read.

*

If msgtyp is less than 0, then the first message in the queue with the lowest type less than or equal to the absolute value of msgtyp will be read.

Also from the documentation, the second parameter of msgsnd() must be a pointer to this structure:

struct msgbuf {
    long mtype;       /* message type, must be > 0 */
    char mtext[1];    /* message data */
};

The mtext field is an array (or other structure) whose size is specified by msgsz, a nonnegative integer value.

So your third parameter is not the size of this entire structure, but rather the size of everything that follows the mtype. So it's sizeof(buf)-sizeof(long).

0
votes

In beginners language the msgtype field lets you, the developer, have some flexibility in designing how messages in a queue are processed. All the types are an integer that represents something you want to differentiate. Some example uses would be:

(a) a kind of message - e.g. a buy transaction versus a refund transaction. This allows two (or potentially many more) kinds of messages in the same queue.

(b) an indicator as to what process should handle a message. The msgrcv function can be used to read only particular types of messages. So, for instance, a client can write buy, sell, refund, status, etc., kinds of messages to a single queue. However multiple processes can read that queue looking only for messages they are interested in e.g. the BuyPgm would read only Buy transactions.

(c) with sufficient forethought the msgtype can be used in a way that both clients and servers can share the same queue. Normally queues are First In First Out mechanisms so clients would normally read one queue and write a separate queue meant for some other process to handle. They need two queues to avoid them reading the messages they wrote. Msgtypes allow a way around this. For instance, clients can write various messages with msgtypes between 1..100. Some process(es) can handle those messages and then write the resulting response messages as msgtype 200..300. The original client can then receive its response message on the same queue it originally wrote by reading only those in the 200-300 range. (Or whatever msgtype protocol you create.)

(d) msgtypes can be used to turn a regular queue into a crude but effective priority queue. For instance you might have a queue of all Order transactions but you design your msgtype protocol so that your important customers get processed first based on the msgtype.

chisaycock posted the rules for reading the queues. It seems daunting at first glance but it actually makes sense when it sinks in.