Tutorial-SystemV_Message_Queues
Tutorial-SystemV_Message_Queues
Tutorial on System V
Message Queues
Norman Lim
November 18, 2014
Outline
• Introduction
• Creating a Message Queue – msgget()
• Message Queue Operations (send and receive)
msgsnd()
msgrcv()
• Message Queue Control – msgctl()
• IPC Status Commands
• References
2
Introduction
• Allows two (or more) processes to exchange information via
access to a common system message queue.
One process establishes the message queue that others may
access.
• Processes must share a common key in order to gain access to
the message queue.
• The sender places a message into the queue using an (OS)
message-passing module (i.e. system call).
• The receiver retrieves the message from the queue also using
a system call.
http://msdn.microsoft.com
Creating a Message Queue (1)
• The msgget() system call creates a new message queue:
int msgget(key_t key, int msgflg)
• It can also return the message queue ID (msqid) of the queue
corresponding to the key argument.
• Parameters:
key: the key for the message queue. It can be specified directly
by the user or generated using ftok()
• key_t key = (key_t) 1234
• key_t key= = ftok("/home/nlim/somefile", 1);
msgflg: an octal integer with settings for the queue’s
permissions and control flags.
• IPC_CREAT | 0600 4
Creating a Message Queue (2)
• Returns:
Success: message queue ID
Failure: -1
• Example:
int msqid = msgget((key_t)1234, IPC_CREAT| 0600);
if (msqid == -1){
perror("msgget: msgget failed");
exit(1);
} else {
//success
}
• Need to include the following libraries:
<sys/types.h>
<sys/ipc.h> 5
<sys/msg.h>
Sending a Message (1)
• The msgsnd() system call places a message in the message queue:
int msgsnd (int msqid, const void *msgp,
size_t msgsz, int msgflg)
• Parameters:
msqid: the id of the message queue to send to.
msgp: the address of (pointer to) the message to be sent.
msgsz: the size of the message to be sent.
msgflg:
• 0: Block caller if the message queue is full.
• IPC_NOWAIT: Caller does not wait if message queue is full.
• Returns:
Success: 0 6
Failure: -1
Sending a Message (2)
• The message to be sent should be a struct that looks like this:
struct my_message {
/*REQUIRED: Message type, which is a positive number*/
long message_type;
8
Receiving a Message (1)
• The msgrcv() system call is used to retrieve a message from the
message queue:
int msgrcv (int msqid, void *msgp, size_t msgsz,
long msgtyp, int msgflg);
• Parameters:
msqid: the id of the message queue to receive from.
msgp: a pointer (address) to the variable (struct) used to store
the retrieved message.
msgsz: the size of the message buffer structure in bytes (excludes
message_type field)
msgtype: the type of the message to be retrieved.
• > 0 : return first message with type equal to msgtyp
• 0 : return first message in the queue regardless of type
• < 0 : return the first message with lowest type less than or equal to 9
msgtyp
Receiving a Message (2)
msgflg: indicates what action should be taken.
• 0: the calling process blocks until a message arrives in the queue
that satisfies the msgrcv()parameters
• IPC_NOWAIT: if no messages are available, the caller does not
wait for a message (returns -1 and sets errno to ENOMSG)
• MSG_NOERROR: if size of message exceeds msgsz, accept msgsz
bytes
• Returns:
Success: number of bytes received.
Failure: -1
10
Receiving a Message (3)
• Example:
struct my_message msg;
13
IPC Status Commands
• Useful commands that can be invoked from the terminal to
display allocated IPC resources: semaphores, shared memory,
and message queues.
• ipcs: displays all IPC resources active in the system
Display only message queues: ipcs -q
• ipcrm: remove a specific IPC resource
E.g. remove a semaphore with id 239: ipcrm –s 239
• Summary of flags:
-s: semaphores
-m: shared memory
-q: message queues
14
References
• Beginning Linux Programming 4th Edition by Neil Matthew
and Richard Stones
Available in e-book format at http://www.library.carleton.ca/.
• http://www.tldp.org/LDP/lpg/node27.html
• http://www.cs.cf.ac.uk/Dave/C/node25.html
• http://faculty.kutztown.edu/spiegel/CSc552/PowerPoint/
15