1 / 11

Posix Message Queues

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

Download Presentation

Posix Message Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction • A message queue is a linked list of messages • Threads can put messages into and remove messages from the queue • Each message is a record with a priority assigned by the sender • There is no requirement that someone be waiting for a message • Message queues have kernel persistence

  3. Attributes • An unsigned integer priority • The length of the data portion (possibly 0) • The data itself • These differ from pipes and FIFOs which are byte streams with no message boundaries and no type • The head of the queue contains two attributes: the maximum number of messages and the maximum message size

  4. Functions • #include <mqueue.h> • mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr); • returns a message queue descriptor • oflag is one of O_RDONLY, O_WRONLY, or O_RDWR and may be ORed with O_CREAT, O_EXCL, and O_NONBLOCK • attributes specify size and number of messages

  5. Functions • mq_close(mqd_t mqdes); • int mq_unlink(const char *name); • removes the name from the system

  6. Functions (continued) • int mq_getattr(mqd_t mqdes, struct mq_attr *attr); • int mq_setattr(mqd_t mqdes, const struct *attr, struct mq_attr *oattr); • int mq_send(mqd_t mqdes, const char *ptr, size_t len, unsigned int prio); • ssize_t mq_receive(mqd_t mqdes, char *ptr, size_t len, unsigned int *priop);

  7. Functions (mq_notify) • mq_notify(mqd_t mqdes, const struct sigevent *notification); • #include <signal.h> • If the notification argument is nonnull, the process wants to be notified when a message arrives for an empty queue • If the notification argument is null, the existing registration is removed

  8. mq_notify (continued) • Only one process at a time can be registered for notification for a given queue • A thread blocked in mq_receive takes precedence over a process waiting for notification • When the notification is sent, the registration is removed

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

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

  11. Assignment 3 • Modify mqcreate.c as follows: • before calling mq_open, print a message and sleep for 30 seconds. • After mq_open returns, print another message, sleep for 30 seconds, and then call mq_close. • Run the program specifying a large number of messages (200,000) and a maximum size of 10 bytes. • During the first 30 second pause run ps and look at the memory size of the program. Do this again after mq_open has returned. Can you explain what happens?

More Related