07 - Message Queues Flashcards
Explain the concept of message queues (MQ).
- Link between producer and consumer is indirect
- Producer writes messages on the queue without selecting the consumer
- The consumer retrieves a message from the queue, without selecting a producer
- Writings are unblocking as long as resources are available
- Reading is blocking when the queue is empty
- A Message Queue is a linked list of message structures stored inside the kernel’s memory space and accessible by multiple processes
- Synchronization is provided automatically by the kernel
- Preallocated message buffers
- Messages with priority. A message with a higher priority is always received first.
- Send and receive functions are synchronous by default. Possibility to set a wait timeout to avoid nondeterminism.
- Support asynchronous delivery notifications.
What are the programming steps for MQ?
- Define message structure
- Create a message queue
- Connect to queue
- Read/ Write messages
- Close queue
- Destroy queue
What are some concerns necessary to consider when defining the message structure?
- Multiple processes should agree on the structure/size
- Application level
- Message queues handle different sizes
- The programmer should define a structure
- With pre-defined size
- Able to accommodate multiple sub-types
Give an example of an System V (SV) MQ message structure.
struct mymsg{ long msg_type; char mytext[512]; /* rest of the message*/ int something_else; }
-
msg_type
used in reception
Explain the process of creation, opening, write, read and destruction for an SV MQ.
Creation
-
msgget
- get a SV MQ identifier -int msgget(key_t key, int msg flg);
- Create private key -
IPC_PRIVATE
. Hinerited by child processes. - Create public key - not in use (ipcs).
msgflag
-IPC_CREAT
. Verify if it exists:msgflag
-O_CREAT
|O_EXCL
Opening
- Open a public MQ - key - already creates MQ -
msgflag
isNULL
Write
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
- Writes a message to the queue
- Parameters
-
msqid
- queue id (returned frommsgget
) - Message + size
-
msgflag
-IPC_NOWAIT
-
Read
ssize_t msgrcv(int msqid, const void *msgp, size_t msgsz, long msgtyp, int msgflg);
- Reads a message from the queue
- Parameters
-
msqid
- queue id (returned frommsgget
) - Pointer to buffer + max size
- Type of message
-
msgflag
-IPC_NOWAIT
,MSG_COPY
(does not remove message)
-
Destruction
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
msqid
-
cmd
-IPC_RMID
-
msqid_ds
-NULL
Describe the POSIX MQ message structure.
- Array of bytes
- Priority/message selection
- API
- Each message has an associated priority
- Messages are always delivered to the receiving process with the highest priority first
- Message priorities range
- From 0 (low) to
sysconf(_SC_MQ_PRIO_MAX) - 1
(high) - On linux, the max is
32768 - 1
- POSIX.1 requires a range from 0 to 31
- From 0 (low) to
Explain the process of creation, opening, write and read for a POSIX MQ.
Creation
-
mq_open
- open a message queue
mqd_t mq_open(const char * name, int oflag, mode_t mode, struct mq_attr * attr);
-
name
- identifier -
oflags
-O_CREAT
|O_RDONLY
|O_WRONLY
|O_RDWR
-
mode
- file access modes -
attr
-NULL
,struct mq_attr queue _attr;
Opening
- Default settings:
name
- identifier,oflags
-O_RDONLY
|O_WRONLY
|O_RDWR
- Creates -
O_CREAT
- message queue is assigned to a file queue in/dev/msgque
, where the file name is used by other processess -
mq_unlink
- removes a message queue and deletes the file -
mq_close
- closes a message queue descriptor and process can no longer use the queue
Write
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned int msg_prio);
- Writes a message to the queue
- Parameters
-
mqdes
- queue id (returnedf frommq_open
) - Message + size
-
msg_priority
- used inmq_receive
-
Read
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio);
- Reads a message from the queue
-
mqdes
- queue id (returned frommq_open
) - Message + buffer size
-
msg_priority
- used inmq_receive
- Messages are always delivered to the receiving process highest priority first
-
msg_priority
-NULL
or stores the priority types of the received message
-
What will happen in read/write for empty and full queues?
Empty queue
- Receive call blocks
-
mq_timedreceive
- block some time
Full queue
- Send blocks
-
mq_timedsend
- blocks some time
Will lead to errno ETIMEDOUT
if timeout is reached.
What are the three ways to limit POSIX MQ?
On the use program - queue_attr.mq_maxmsg
and queue_attr.mq_msgsize
Change directly on /etc/security/limits.conf
Values delimited by the OS - /proc/sys/fs/mqueue
Present the characteristics of message queues.
- Implementation - Kernel/syscall
- Scope - local
- No duples
- Time-uncoupling
- Space-uncoupling
- Explicit
- Synchronization - Yes (reads) no (writes)
- Process relation - unrelated
- Identification - string
- API - specific API