This is used for sending or delivering a message to the queue. Here is its syntax:
int msgsnd ( int msqid, struct msgbuf *msgstruc, int msgsize, int flag );
Here, we have to address the following:
- msqid: Represents the queue identifier of the message that we want to send. The queue identifier is usually retrieved by invoking the msgget function.
- msgstruc: This is a pointer to the user-defined structure. It is the mesg member that contains the message that we want to send to the queue.
- msgsize: Represents the size of the message in bytes.
- flag: Determines the action to be taken on the message. If the flag value is set to IPC_NOWAIT and if the message queue is full, the message will not be written to the queue, and the control is returned to the calling process. But if flag is not set and the message queue is full, then the calling process will suspend until a space becomes available in the queue. Usually, the value of flag is set to 0.
If this is executed successfully, the function returns 0, otherwise it returns -1.
We will now begin with the first part of this recipe: writing a message into the queue.