Download presentation
Presentation is loading. Please wait.
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 A message queue is a linked list of messages Threads can put messages into and remove messages from the queue Threads can put messages into and remove messages from the queue Each message is a record with a priority assigned by the sender Each message is a record with a priority assigned by the sender There is no requirement that someone be waiting for a message There is no requirement that someone be waiting for a message Message queues have kernel persistence Message queues have kernel persistence
3
Attributes An unsigned integer priority An unsigned integer priority The length of the data portion (possibly 0) The length of the data portion (possibly 0) The data itself The data itself These differ from pipes and FIFOs which are byte streams with no message boundaries and no type 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 The head of the queue contains two attributes: the maximum number of messages and the maximum message size
4
Functions Functions #include #include mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr); mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr); returns a message queue descriptor 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 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 attributes specify size and number of messages
5
Functions Functions 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 system removes the name from the system
6
Functions (continued) int mq_getattr(mqd_t mqdes, struct mq_attr *attr); 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_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); 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); 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); mq_notify(mqd_t mqdes, const struct sigevent *notification); #include #include 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 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 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 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 A thread blocked in mq_receive takes precedence over a process waiting for notification When the notification is sent, the registration is removed 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? 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? 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: Modify mqcreate.c as follows: before calling mq_open, print a message and sleep for 30 seconds. 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. 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. 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? 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.