Site Loader
Rock Street, San Francisco

 

 

Messaging Queues

     CS531
Report

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


order now

 

                        Sunchit
Sehgal

                           G01068224

December
2017

 

 

 

 

 

 

 

 

Abstract

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.

 

 

 

 

 

 

 

 

 

 

 

Contents
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
Figures

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

5

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

5

1.3
      Sending a message to the Message
Queue…………………………………………

6

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

7

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

7

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

8

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

8

 

 

 

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.

Message
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.

1.1.1                     
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

Centralized
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
messages.

 

Figure 1.1: Messaging Methods

 

1.2                         
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

2   
#include

3   
#include

4   
#include

5

6    //Creation or connect to an existing queue

7   
int msgget(key_t key, int msgflg);

8

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

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

11

           key
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
queue.

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.

1.3                         
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
Queue

1         
#include

2         
#include

3         
#include

4         
int
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:

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

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

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

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

·        
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

1           
struct msgbuf {

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

3           
char name20;

4           
};

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

1.4                         
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

1           
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

Action

0

Return first (oldest)
message from the queue.

> 0

Return first message
with equal to type equal to msgtyp

defines another function for the
cleanup of a message queue: msgctl().

Figure 1.7: Function prototype for destroying a
message Queue

1           
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

Summary

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.

References

Literature

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

Online
sources

2      URL:
https://users.cs.cf.ac.uk/Dave.Marshall/C/node25.html (visited on 28/11/2017)

3      URL:
https://en.wikipedia.org/wiki/Message_queue (visited on 28/11/2017).

4      URL:  http://beej.us/guide/bgipc/output/html/multipage/mq.html (visited on 28/11/2017).

Post Author: admin

x

Hi!
I'm Eunice!

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

Check it out