150 likes | 319 Views
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
E N D
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 • 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
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
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
Functions • mq_close(mqd_t mqdes); • int mq_unlink(const char *name); • removes the name from the system
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);
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
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
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?
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?
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?