11
Posix Message Posix Message Queues Queues Courtesy of W. Richard Courtesy of W. Richard Stevens Stevens Unix Network Programming Unix Network Programming Volume 2: Interprocess Volume 2: Interprocess Communication Communication

Posix Message Queues

Embed Size (px)

DESCRIPTION

Posix Message Queues. Courtesy of W. Richard Stevens Unix Network Programming Volume 2: Interprocess Communication. Introduction. A message queue is a linked list of messages Threads can put messages into and remove messages from the queue - PowerPoint PPT Presentation

Citation preview

Page 1: Posix Message Queues

Posix Message QueuesPosix Message Queues

Courtesy of W. Richard StevensCourtesy of W. Richard Stevens

Unix Network ProgrammingUnix Network Programming

Volume 2: Interprocess CommunicationVolume 2: Interprocess Communication

Page 2: Posix Message Queues

IntroductionIntroduction

A message queue is a linked list of messagesA message queue is a linked list of messages Threads can put messages into and remove Threads can put messages into and remove

messages from the queuemessages from the queue Each message is a record with a priority Each message is a record with a priority

assigned by the senderassigned by the sender There is no requirement that someone be There is no requirement that someone be

waiting for a messagewaiting for a message Message queues have kernel persistenceMessage queues have kernel persistence

Page 3: Posix Message Queues

AttributesAttributes

An unsigned integer priorityAn unsigned integer priority The length of the data portion (possibly 0)The length of the data portion (possibly 0) The data itselfThe data itself These differ from pipes and FIFOs which are These differ from pipes and FIFOs which are

byte streams with no message boundaries and byte streams with no message boundaries and no typeno type

The head of the queue contains two attributes: The head of the queue contains two attributes: the maximum number of messages and the the maximum number of messages and the maximum message sizemaximum message size

Page 4: Posix Message Queues

FunctionsFunctions

#include <mqueue.h>#include <mqueue.h> mqd_t mq_open(const char *name, int oflag, mqd_t mq_open(const char *name, int oflag,

mode_t mode, struct mq_attr *attr);mode_t mode, struct mq_attr *attr); returns a message queue descriptorreturns a message queue descriptor oflag is one of O_RDONLY, O_WRONLY, or oflag is one of O_RDONLY, O_WRONLY, or

O_RDWR and may be ORed with O_CREAT, O_RDWR and may be ORed with O_CREAT, O_EXCL, and O_NONBLOCKO_EXCL, and O_NONBLOCK

attributes specify size and number of messagesattributes specify size and number of messages

Page 5: Posix Message Queues

FunctionsFunctions

mq_close(mqd_t mqdes);mq_close(mqd_t mqdes); int mq_unlink(const char *name);int mq_unlink(const char *name);

removes the name from the systemremoves the name from the system

Page 6: Posix Message Queues

Functions (continued)Functions (continued)

int mq_getattr(mqd_t mqdes, struct mq_attr int mq_getattr(mqd_t mqdes, struct mq_attr *attr);*attr);

int mq_setattr(mqd_t mqdes, const struct *attr, int mq_setattr(mqd_t mqdes, const struct *attr, struct mq_attr *oattr);struct mq_attr *oattr);

int mq_send(mqd_t mqdes, const char *ptr, int mq_send(mqd_t mqdes, const char *ptr, size_t len, unsigned int prio);size_t len, unsigned int prio);

ssize_t mq_receive(mqd_t mqdes, char *ptr, ssize_t mq_receive(mqd_t mqdes, char *ptr, size_t len, unsigned int *priop);size_t len, unsigned int *priop);

Page 7: Posix Message Queues

Functions (mq_notify)Functions (mq_notify)

mq_notify(mqd_t mqdes, const struct sigevent mq_notify(mqd_t mqdes, const struct sigevent *notification);*notification); #include <signal.h>#include <signal.h> If the notification argument is nonnull, the process If the notification argument is nonnull, the process

wants to be notified when a message arrives for an wants to be notified when a message arrives for an empty queueempty queue

If the notification argument is null, the existing If the notification argument is null, the existing registration is removedregistration is removed

Page 8: Posix Message Queues

mq_notify (continued)mq_notify (continued)

Only one process at a time can be registered Only one process at a time can be registered for notification for a given queuefor notification for a given queue

A thread blocked in mq_receive takes A thread blocked in mq_receive takes precedence over a process waiting for precedence over a process waiting for notificationnotification

When the notification is sent, the registration When the notification is sent, the registration is removedis removed

Page 9: Posix Message Queues

Assignment 1Assignment 1

Modify pxmsg/mqnotifysig1.c so that it does Modify pxmsg/mqnotifysig1.c so that it does not call mq_notify when the signal is not call mq_notify when the signal is delivered. Then send two messages to the delivered. Then send two messages to the queue and verify that the signal is not queue and verify that the signal is not generated for the second message. Why not?generated for the second message. Why not?

Page 10: Posix Message Queues

Assignment 2Assignment 2

Modify the same program so that it does not Modify the same program so that it does not read the message from the queue when the read the message from the queue when the signal is delivered. Instead, just call mq_notify signal is delivered. Instead, just call mq_notify and print that the signal was received. Then and print that the signal was received. Then send two messages to the queue and verify that send two messages to the queue and verify that the signal is not generated for the second the signal is not generated for the second message. Why not?message. Why not?

Page 11: Posix Message Queues

Assignment 3Assignment 3

Modify mqcreate.c as follows:Modify mqcreate.c as follows: before calling mq_open, print a message and sleep for 30 before calling mq_open, print a message and sleep for 30

seconds. seconds. After mq_open returns, print another message, sleep for 30 After mq_open returns, print another message, sleep for 30

seconds, and then call mq_close.seconds, and then call mq_close. Run the program specifying a large number of messages Run the program specifying a large number of messages

(200,000) and a maximum size of 10 bytes.(200,000) and a maximum size of 10 bytes. During the first 30 second pause run ps and look at the During the first 30 second pause run ps and look at the

memory size of the program. Do this again after mq_open memory size of the program. Do this again after mq_open has returned. Can you explain what happens?has returned. Can you explain what happens?