Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


❑ q_qbytesspecifies the maximum number of bytes that may be used by the messages in the
queue.
❑ q_numspecifies the number of messages in the queue.
❑ q_lspidis the PID of the last sender process;q_lrpidis the PID of the last receiver process.

Three standard lists of the kernel are used to manage sleeping senders (q_senders), sleeping receivers
(q_receivers), and the messages themselves (q_messages). Each uses its own separate data structures as
list elements.

Each message inq_messagesis encapsulated in an instance ofmsg_msg.

ipc/msg.c
struct msg_msg {
struct list_head m_list;
long m_type;
int m_ts; /* message text size */
struct msg_msgseg* next;
/* the actual message follows immediately */
};

m_listis used as a list element to link the individual messages; the other elements are used to manage
the message itself.

❑ m_typespecifies the message type and is used as described to support several types per queue.
❑ m_tsspecifies the message text size in bytes.
❑ nextis needed to hold long messages requiring more than one memory page.

There is no explicit field in which the message itself is stored. Because (at least) one page is always
reserved for each message and themsg_msginstance is held at the beginning of this page, the remaining
space can be used to store the message text, as shown in Figure 5-5.

Page frame

struct msg_msg

next next
struct msg_msg_seq struct msg_msg_seq

Message text

Figure 5-5: Managing an IPC message in memory.

The maximum number of bytes available for the message text in amsg_msgpage is calculated by sub-
tracting the size of the structure from the size of a memory page.

ipc/msgutils.c
#define DATALEN_MSG (PAGE_SIZE-sizeof(struct msg_msg))
Free download pdf