07 - Message Queues Flashcards

1
Q

Explain the concept of message queues (MQ).

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the programming steps for MQ?

A
  • Define message structure
  • Create a message queue
  • Connect to queue
  • Read/ Write messages
  • Close queue
  • Destroy queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some concerns necessary to consider when defining the message structure?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of an System V (SV) MQ message structure.

A
struct mymsg{
    long msg_type;
		char mytext[512]; /* rest of the message*/
		int something_else;
}
  • msg_type used in reception
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the process of creation, opening, write, read and destruction for an SV MQ.

A

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 is NULL

Write

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
  • Writes a message to the queue
  • Parameters
    • msqid - queue id (returned from msgget)
    • 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 from msgget)
    • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the POSIX MQ message structure.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the process of creation, opening, write and read for a POSIX MQ.

A

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 from mq_open)
    • Message + size
    • msg_priority - used in mq_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 from mq_open)
    • Message + buffer size
    • msg_priority - used in mq_receive
    • Messages are always delivered to the receiving process highest priority first
    • msg_priority - NULL or stores the priority types of the received message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will happen in read/write for empty and full queues?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three ways to limit POSIX MQ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Present the characteristics of message queues.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly