Site Loader
Rock Street, San Francisco



Messaging Queues


We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now














An Inter-process communication (IPC)
refers to the mechanisms that are provided by an Operating System to allow
multiple processes to manage shared data. These mechanisms are broadly
classified into three categories-Message Passing, Shared Memory and
Synchronization. Messaging queues are an example of the asynchronous message
passing mechanisms provided by IPC. In this report, the basics and usage of

the operations and function calls
of messaging queues are described using C code syntax. Also covered are the
differences between Message Queues and Shared Memory.












Messaging Queues. 4
1.1         Introduction. 4
1.1.1          Messaging
Methods. 4
1.2         Creating
a Messaging Queue. 5
1.3         Sending
a Message to the Queue. 6
1.3.1          The
Message Structure. 7
1.4         Receiving
Messages from the Queue. 7
1.5         Destroying
a Message Queue. 8
Summary. 9
References. 10
Literature. 10
Online sources. 10









List of

1.1       Messaging Methods…………………………………………………………………………


1.2       Creating a Message Queue…………………………………………………………….


      Sending a message to the Message


The Message Structure…………………………………………………………………….


1.5        The msgrcv() prototype…………………………………………………………………..


1.6       Options for msgtyp parameter…………………………………………………………


1.7         Function prototype for destroying a
message Queue……………………….





Chapter 1

Messaging Queues

1.1         Introduction

A formal
definition would be as follows, “In computer science, message
queues and mailboxes are software-engineering components used for inter-process communication (IPC), or for inter-thread communication within the same process.
They use a queue for messaging – the passing of control or of content.”3.

queues help in providing an asynchronous protocol which means that there is no
requirement for sending and receiving processes to communicate with each other
in order to share messages. Messages placed onto the queue are stored until the
recipient retrieves them. Message queues have implicit or explicit limits on
the size of data that may be transmitted in a single message and the number of
messages that may remain outstanding on the queue 2.

One process
establishes a message queue that others may access. Often a server will
establish a message queue that multiple clients can access. A process
generating a message may specify its type when it places the message in the
message queue so that processes reading the queue can selective read messages
of specific type(s) on a FIFO (First in first out) basis.

When a
message is sent, its text is copied to the message queue. The msgsnd() and
msgrcv() functions can be performed as either blocking or non-blocking
operations. Non-blocking operations allow for asynchronous message transfer —
the process is not suspended as a result of sending or receiving a message. In
blocking or synchronous message passing the sending process cannot continue
until the message has been transferred or has even been acknowledged by a
receiver. IPC signal and other mechanisms can be employed to implement such transfer
2. A blocked message operation remains suspended until one of the following
three conditions occurs:

The call succeeds.

The process receives a signal.

The queue is removed.

Messaging Methods

1.1.1. a. Peer to Peer Messaging

In the case
of peer to peer messaging, multiple components communicate with each other
directly. This type of messaging method requires the system to maintain more
than one message queues.

1.1.1. b. Centralized Messaging

messaging is a type of architecture where a central component maintains the
messaging queue and other components access this queue in order to retrieve
their messages. This type of messaging method is known as the client-server
architecture where the centralized component or the server generates the
messages and the clients have access to this messaging queue can retrieve their


Figure 1.1: Messaging Methods


Creating a Messaging Queue

         In order to connect to a message queue
or create a new one, the system call msgget() is used which takes two arguments
and returns the message queue id on success and -1 on failure. The value passed
as the msgflg argument must be an octal integer with settings for the queue’s
permissions and control flags.

Figure 1.2: Creating a message Queue

1    //required header files





6    //Creation or connect to an existing queue

int msgget(key_t key, int msgflg);


9      //ftok() for generating a unique queue id

key_t ftok(const char *path, int id);


parameter is similar to a file name and is used to identify the message
queue The key is an integer of the type key_t
and is generated by the library function ftok(). flag parameter
is a bit mask by OR-ing constants which provide permissions to the message

IPC_PRIVATE – the value of the
key is ignored and a private semaphore set is created.

IPC_CREAT – if a semaphore with
the given key does not exist, it is created, and otherwise the call returns
with 1 which is the return value when there is an error 1.

Sending a Message to the Queue

A message, once defined is sent
into the message queue using the msgsnd() system call.

Figure 1.3: Sending a message to the Message




msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); // System call
for msgsnd()

msqid is the message queue identifier
returned by msgget(). The pointer msgp is a pointer to the data you want
to put on the queue. msgsz is the
size in bytes of the data to add to the queue (not counting the size of the mtype member). Finally, msgflg allows
you to set some optional flag parameters 4. The msgflg specifies the action to be taken if one or more of the
following are true:

The number of bytes already on the queue is
equal to msg_qbytes.

The total number of messages on all queues
system-wide is equal to the system-imposed limit.

These actions
are as follows:

If (msgflg & IPC_NOWAIT) is non-zero, the
message will not be sent and the calling process will return immediately.

If (msgflg & IPC_NOWAIT) is 0, the calling
process will suspend execution until one of the following occurs:

The condition responsible for the suspension no
longer exists, in which case the message is sent.

The message queue identifier msqid is removed
from the system; when this occurs, errno is set equal to EIDRM and -1 is

The calling process receives a signal that is to
be caught; in this case the message is not sent and the calling process resumes

Upon successful completion, the
following actions are taken with respect to the data structure associated with

msg_qnum is incremented by 1.

msg_lspid is set equal to the process ID of the
calling process.

msg_stime is set equal to the current time.


1.3.1      The Message Structure

The message structure refers to
the data/message that needs to be added to the message queue. The first data
member in a message should always be a long integer specifying the message type
so that the process retrieving the message from the queue picks up a message
only of that particular type(s) from the queue 1. The structure of a message
is defined below:

Figure 1.4: The Message structure

struct msgbuf {

long mtype; /? identifies the message in the message queue ?/

char name20;


Here, mtype specifies the message type and is
always a long Integer data type.

Receiving Messages from the Queue                

A process reading the
message queue can retrieve   the message
using the msgrcv() system call
which  is somewhat similar to  the msgsnd()
system call but comes up with an additional parameter of message type which is
used to get a message of that particular type from the queue4.

The prototype for msgrcv() system call is:


Figure 1.5: The msgrcv() prototype

int msgrcv(int msgqid, void *msgp, size_t msgz, long msgtyp, int msgflg);

msqid is the message queue identifier returned by msgget(). The pointer msgp is a pointer to the data you want
to put on the queue. msgsz is the
size in bytes of the data to add to the queue (not counting the size of the mtype member). Finally, msgflg are some optional flag
parameters. The msgtyp field is used
to identify the message structure. The behavior of msgrcv() can be modified by
choosing a msgtyp that is positive,
negative, or zero:


msgtyp value



Return first (oldest)
message from the queue.

> 0

Return first message
with equal to type equal to msgtyp

< 0 return the first message with lowest type less than or equal to msgtyp     Figure 1.6: Options for msgtyp parameter2.     1.5                          Destroying a Message Queue                   When a message queue is created, it doesn't destroy itself until deleted explicitly. All the processes that have ever used it can quit, but the queue will still exist. It is important that the queue is destroyed so that no system resources are wasted.         The header file defines another function for the
cleanup of a message queue: msgctl().

Figure 1.7: Function prototype for destroying a
message Queue

int msgctl(int msqid, int cmd, struct msqid_ds *buf);


msqid is the queue identifier
obtained from msgget(). The
important argument is cmd which
tells msgctl() how to behave. It can
be many values for cmd. But in order to destroy the queue, the cmd value IPC_RMID, which is used to remove the message queue. The buf argument can be set to NULL for the
purposes of IPC_RMID.


Chapter 2


Message queues, one of the tools used for process and
thread synchronization were described along with their function calls.

This report also helped me to study
about the other IPC tools that are available such as shared memory and signals.
Many implementations of message queues function internally: within an operating
system or within an application. Such queues exist for the purposes of that
system only.  Other implementations allow
the passing of messages between different computer systems, potentially
connecting multiple applications and multiple operating systems. An Example of
commercial implementations of message Queuing is Oracle Advanced Queuing (AQ).

To summarize, message queues are a
great IPC synchronization tool that allow processes to communicate with each
other and allow a smooth execution of interdependent threads 3.



1    Advanced Programming in the UNIX
Environment, 3rd Edition by W. Richard


2      URL: (visited on 28/11/2017)

3      URL: (visited on 28/11/2017).

4      URL: (visited on 28/11/2017).

Post Author: admin


I'm Eunice!

Would you like to get a custom essay? How about receiving a customized one?

Check it out