Presentation is loading. Please wait.

Presentation is loading. Please wait.

Message Queues.

Similar presentations


Presentation on theme: "Message Queues."— Presentation transcript:

1 Message Queues

2 Unix IPC Package Unix System V IPC package consists of three things
Messages – allows processes to send formatted data streams to arbitrary processes Shared memory allows processes to share parts of their virtual address space. Semaphores allow processes to synchronise execution.

3 struct msqid_ds { struct ipc_perm msg_perm; struct msg *msg_first; /* first message on queue */ struct msg *msg_last; /* last message in queue */ time_t msg_stime; /* last msgsnd time */ time_t msg_rtime; /* last msgrcv time */ time_t msg_ctime; /* last change time */ ushort msg_cbytes; ushort msg_qnum; ushort msg_qbytes; /* max number of bytes on queue */ ushort msg_lspid; /* pid of last msgsnd */ ushort msg_lrpid; /* last receive pid */ };

4 struct ipc_perm { key_t key; ushort uid; /* owner euid and egid */ ushort gid; ushort cuid; /* creator euid and egid */ ushort cgid; ushort mode; /* access modes see mode flags below */ ushort seq; /* slot usage sequence number */ };

5 msg structure struct msg {
struct msg *msg_next; /* pointer to next message on q */ long msg_type; /* message type */ ushort msg_ts; /* message text size */ short msg_spot; /* message text map address */ };

6 System imposed limits on the manipulation of message queues
System imposed limits on the manipulation of message queues. The limits are defined in the <sys/msg.h>. MSGMNI :The maximum number of message queues that may exist any given time in a system MSGMAX : The maximum number of bytes of data allowed for a message MSGMNB : The maximum number of bytes of all messages allowed in a queue MSGTQL : The maximum number of messages in all queues allowed in a system

7 UNIX system V API for Message Queues
msgget() : open and create if needed, a message queue for access msgsnd( ) :send a message to a queue msgrcv() : receive a message from message queue msgctl() : manipulate the control data of a message queue

8 To connect to a queue Arguments int msgget(key_t key, int msgflg);
msgget() returns the message queue ID on success, or -1 on failure Arguments key is a system-wide unique identifier describing the queue you want to connect to (or create). Every other process that wants to connect to this queue will have to use the same key. msgflg tells msgget() what to do with queue in question. To create a queue, this field must be set equal to IPC_CREAT bit-wise OR'd with the permissions for this queue.

9 type key_t is actually just a long, you can use any number you want.
use the ftok() function which generates a key from two arguments: key_t ftok(const char *path, int id); path is the file that this process can read, id is usually just set to some arbitrary char, like 'A'. The ftok() function uses information about the named file (like inode number, etc.) and the id to generate a probably- unique key for msgget().

10 To make the call: #include <sys/msg.h>
key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT);

11 Sending 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]; }; mtype is used later when retrieving messages from the queue, set to any positive number. mtext is the data this will be added to the queue.

12 struct pirate_msgbuf {
You can use any structure you want to put messages on the queue, as long as the first element is a long. struct pirate_msgbuf { long mtype; /* must be positive */ char name[30]; char ship_type; int notoriety; int cruelty; int booty_value; };

13 To send msg int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); Arguments msqid is the message queue identifier returned by msgget(). 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. msgflg allows you to set some optional flag parameters, which we'll ignore for now by setting it to 0.

14 Sending to the Queue key_t key; int msqid;
struct pirate_msgbuf pmb = {2, "L'Olonais", 'S', 80, 10, }; key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT); msgsnd(msqid, &pmb, sizeof(pmb), 0);

15 Receiving from the Queue
int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); Here msgtyp corresponds to the mtype we set in the msgsnd() msgtyp Effect on msgrcv() 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.

16 Receiving from a Queue key_t key; int msqid;
struct pirate_msgbuf pmb; /* where L'Olonais is to be kept */ key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666); msgrcv(msqid, &pmb, sizeof(pmb), 2, 0);

17 Destroying a message queue
There are two ways of destroying a queue: Use the Unix command ipcs to get a list of defined message queues, then use the command ipcrm to delete the queue. Write a program to do it for you.

18 int msgctl(int msqid, int cmd, struct msqid_ds *buf); Arguments
msqid is the queue identifier obtained from msgget(). cmd tells msgctl() how to behave. IPC_RMID is used to remove the message queue. Buf argument can be set to NULL for the purposes of IPC_RMID. msgctl(msqid, IPC_RMID, NULL);

19 Demo Program


Download ppt "Message Queues."

Similar presentations


Ads by Google