Sending a message to the queue
Each message is made up of two parts, which are defined in the template structure struct msgbuf, as defined in sys/msg.h:
struct msgbuf {
long mtype;
char mtext[1];
};
The field mtype is used later when retrieving messages from the queue, and can be set to any positive number. mtext is the data that will be added to the queue.
Look,the above struct has data type mtext of size 1 byte.Well,it is not a big problem.We 'll create a struct like tht below
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
MSGSZ set to 128.
Ok, so how do we pass this information to a message queue? Just use msgsnd():
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
msqid is the message queue identifier returned by msgget(). The pointer msgp is a pointer to the data you want to put on the queue. msgsz is the size in bytes of the data to add to the queue. Finally, msgflg allows you to set some optional flag parameters, which we'll ignore for now by setting it to 0.
the code snippnet showing msgsnd()
key_t key;
int msqid;
message_buf pmb = {1,"Hello I'm here"};
key = ftok("/home/beej/somefile", 'b');
msqid = msgget(key, 0666 | IPC_CREAT);
msgsnd(msqid, &pmb, sizeof(pmb), 0); /* stick him on the queue */
Receiving from the queue
there is a counterpart to msgsnd(): it is msgrcv().A call to msgrcv() that would do it looks something like this:
key_t key;
int msqid;
struct pirate_msgbuf pmb; /* where L'Olonais is to be kept */
key = ftok("/home/beej/somefile", 'b');
msqid = msgget(key, 0666 | IPC_CREAT);
msgrcv(msqid, &pmb, sizeof(pmb), 1, 0); /* get him off the queue! */
There is something new to note in the msgrcv() call: the 1! What does it mean? Here's the synopsis of the call:
int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
The 1 we specified in the call is the requested msgtyp. Recall that we set the mtype arbitrarily to 1 in the msgsnd() section of this document, so that will be the one that is retrieved from the queue.
Additional notes:
The behavior of msgrcv() can be modified drastically by choosing a msgtyp that is positive, negative, or zero
Zero --> Retrieve the next message on the queue, regardless of its mtype.
Positive --> Get the next message with an mtype equal to the specified msgtyp.
Negative --> Retrieve the first message on the queue whose mtype field is less than or equal to the absolute value of the msgtyp argument.
Destroying a message queue
You have to destroy a message queue.It is important that you do this so you don't waste system resources.There are two ways:
1. Use the Unix command ipcs to get a list of defined message queues, then use the command ipcrm to delete the queue.
2. Write a program to do it for you.
Often, the latter choice is the most appropriate, since you might want your program to clean up the queue at some time or another.msgctl() comes as our handy for this.
The synopsis of msgctl() is:
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
Of course, msqid is the queue identifier obtained from msgget(). The important argument is cmd which tells msgctl() how to behave. It can be a variety of things, but we're only going to talk about IPC_RMID, which is used to remove the message queue. The buf argument can be set to NULL for the purposes of IPC_RMID.
the code snipnets will be:msgctl(msqid, IPC_RMID, NULL);
And the message queue is no more.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.