0% found this document useful (0 votes)
123 views5 pages

ANP - 4. IPC-message Queue

- POSIX message queues allow processes to exchange messages/data in the form of variable length messages with assigned priorities. Each message has a priority, length, and data. - The messages are queued in the kernel with first-in first-out retrieval based on priority. Reading always returns the oldest highest priority message. - Processes use message queue descriptors after opening/creating a message queue to send and receive messages via functions like mq_send() and mq_receive().

Uploaded by

Suman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views5 pages

ANP - 4. IPC-message Queue

- POSIX message queues allow processes to exchange messages/data in the form of variable length messages with assigned priorities. Each message has a priority, length, and data. - The messages are queued in the kernel with first-in first-out retrieval based on priority. Reading always returns the oldest highest priority message. - Processes use message queue descriptors after opening/creating a message queue to send and receive messages via functions like mq_send() and mq_receive().

Uploaded by

Suman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

7/21/2017

Introduction to POSIX Message


Queue
• It can be thought of as a linked list of messages.
• Each message has record (boundary, unlike stream).
Inter-process Communication
• Each message has priority assigned by the sender.
(IPC)-POSIX Message Queue • No requirement exists: “receiver must be waiting for
a message to arrive on a queue before some process
Harshad B. Prajapati
writes a message to that queue”. (Such requirement
Associate Professor
exists in PIPE and FIFO)
Information Technology Department,
Dharmsinh Desai University, Nadiad • The message queues have kernel persistence.

Some features of POSIX Message


POSIX Message Queue
Queue
• A read always returns the oldest message of highest • Every message has following attributes
priority. – An unsigned integer priority.
• Posix message queues allow the generation of a – The length of the data portion of the message (can be 0)
signal or the initiation of a thread when a message – The data itself (if data length > 0).
arrives on an empty queue. • Comparison with PIPE and FIFO
– PIPE and FIFO are stream based. That means no
message boundaries exist. The data written in one
write operation may not be delivered to the
reader in exactly one read operation.

Programming POSIX Message Programming POSIX Message


Queue Queue
• Data types • Open a message queue
#include <mqueue.h>
– Open an existing message queue
– mqd_t
• It is message queue descriptor, we get it after successful opening of a mqd_t mq_open(const char *name,
message queue.
– struct mq_attr (Message Queue attributes)
int oflag);
struct mq_attr { – Open a new message queue
long mq_flags; /* Flags: 0 or O_NONBLOCK */
mqd_t mq_open(const char *name, int oflag,
long mq_maxmsg; /* Max. # of messages on queue */
long mq_msgsize; /* Max. message size (bytes) */
mode_t mode, struct mq_attr *attr);
long mq_curmsgs; /* # of messages currently in queue */
};

1
7/21/2017

Programming POSIX Message Programming POSIX Message


Queue Queue
• name: Each message queue is identified by a name of the • mode: If O_CREAT is specified in oflag, then two
form /somename. additional arguments must be supplied.
• oflags: O_RDONLY (to receive messages only), O_WRONLY (to send – The mode argument specifies the permissions to be placed
messages only), O_RDWR (to both send and receive messages.)
on the new queue
These three flags are used for opening an existing message queue.
Zero or more of the following flags can additionally be ORed in oflag: • attr: The attr argument specifies attributes for the
– O_NONBLOCK : non-blocking mode for mq_receive() and mq_send() queue.
these functions could fail with the error EAGAIN.
– O_CREAT: Create the message queue if it does not exist. The owner of
• On success, mq_open() returns a message queue
the message queue is set to the effective user ID of the calling process. descriptor for use by other message queue functions.
The group ownership is set to the effective group ID of the calling
process.
• On error, mq_open() returns (mqd_t) -1, with errno
– O_EXCL: If O_CREAT was specified in oflag, and a queue with the given set to indicate the error.
name already exists, then fail with the error EEXIST.

Programming POSIX Message Programming POSIX Message


Queue Queue
• Close a message queue • remove a message queue name
int mq_close(mqd_t mqdes); int mq_unlink(const char* name);
It returns 0 on success, -1 on failure. It returns 0 on success, -1 on failure.
– The calling process can no longer use the – Message queues have reference count. I.e. how
descriptor. many processes have particular message queue
• Important: the message queue is not removed from the open at current time.
system. – Only the name of the message queue is removed,
– If process terminates, all open message queues the actual message queue gets destructed when
get closed implicitly. reference count becomes zero, i.e., when last
close occurs.

Programming POSIX Message Programming POSIX Message


Queue Queue
• Send a message – The msg_prio:
int mq_send(mqd_t mqdes, const char *msg_ptr, • a non-negative integer, priority of this message.
size_t msg_len, unsigned int msg_prio); Messages are placed on the queue in decreasing order
(Note: first three arguments are similar to three arguments of priority, with newer messages of the same priority
of write) being placed after older messages with the same
– mq_send() adds the message pointed to by msg_ptr to the priority.
message queue referred to by the descriptor mqdes. – How mq_send can become a blocked call
– msg_len argument specifies the length of the message • If the message queue is already full (i.e.,
pointed to by msg_ptr; mq_curmsgs==mq_maxmsg), then, by default,
• this length must be less than or equal to the queue's mq_msgsize mq_send() blocks until sufficient space becomes
attribute. available to allow the message to be queued, or until
• Zero-length messages are allowed. the call is interrupted by a signal handler.

2
7/21/2017

Programming POSIX Message Programming POSIX Message


Queue Queue
• Receive a message • Priority of message
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, – The priority value of the message in mq_send must be less
unsigned *msg_prio);
than MQ_PRIO_MAX.
(Note: first three arguments are similar to three arguments of read)
– mq_receive() removes the oldest message with the highest priority – If the application does not need messages of different
from the message queue referred to by the descriptor mqdes, and priorities, specify priority value 0 in all mq_send and null
places it in the buffer pointed to by msg_ptr. pointer in mq_receive.
– msg_len: the size of the buffer pointed to by msg_ptr; this must be
greater than the mq_msgsize attribute of the queue.
– msg_prio: to retrieve priority of received message. (Pass NULL, if not
interested)
– Blocking of mq_receive()
If the queue is empty, then, by default, mq_receive() blocks until a
message becomes available, or the call is interrupted by a signal
handler. (use flag O_NONBLOCK to make it non-blocking)

Programming POSIX Message Programs on POSIX Message


Queue Queue
• Access attributes of message queue. • We create separate programs
• Get attributes – for creating a message queue.
– For sending a message to the message queue.
int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
– For receiving a message from message queue.
– The mq_getattr fills the message queue attributes in a structure
pointed by attr pointer.
• Set attributes
int mq_setattr(mqd_t mqdes, struct mq_attr *newattr,
struct mq_attr *oldattr);
– It sets the attributes for the queue. But, only mq_flags member is
used, other are ignored. After opening a queue for blocking calls, it
can be made non-blocking using mq_setattr.
– Message size and maximum number of messages are to be specified
at the time of message queue creation.

CreateMessageQueue.c CreateMessageQueue.c
#include <mqueue.h> // First we need to set up the attribute structure
#include <stdlib.h> attr.mq_maxmsg = 5;
#include <stdio.h> attr.mq_msgsize = MSG_SIZE;
attr.mq_flags = 0;
#include <errno.h>
attr.mq_curmsgs=0;
#define MSG_SIZE 4096
// Open a queue with the attribute structure
int main (int argc,char* *args) { mqdes = mq_open (args[1], O_RDWR | O_CREAT, 0664, &attr);
struct mq_attr attr; if(mqdes==-1){
mqd_t mqdes; printf("error: %s (%d)\n",strerror(errno),errno);
if(argc!=2){ return 1;
fprintf(stderr,"Usage: %s MessageQueueName\n",args[0]); }
fprintf(stderr,"For Example #%s /myMQ\n",args[0]); printf("Message Queue with the name %s has been successfully
created.\n",args[1]);
exit(0);
return 0;
}
mq_close (mqdes); //why
}

3
7/21/2017

SendMessage.c SendMessage.c
#include <mqueue.h> if(argc!=3){
#include <stdlib.h> fprintf(stderr,"Usage: %s <Message-Queue Name> <Message
#include <stdio.h> Priority>\n",args[0]);
#include <errno.h> fprintf(stderr,"For Example #%s /myMQ 1\n",args[0]);
#define MSG_SIZE 4096 exit(0);
int main (int argc,char* *args) { }
struct mq_attr attr;
mqd_t mqdes; /* Open a queue with the attribute structure */
int priority; priority=atoi(args[2]);
char message[4096]; mqdes = mq_open (args[1], O_WRONLY);
if(mqdes==-1){
printf("error: %s (%d)\n",strerror(errno),errno);
return 1;
}

SendMessage.c ReceiveMessage.c
/* Take message from user through keyboard */ #include <mqueue.h>
printf("Type your message below...Press Enter to input\n"); #include <stdlib.h>
fgets(message,4096,stdin); #include <stdio.h>
/* Send the message */ #include <errno.h>
mq_send(mqdes,message,sizeof(message),priority);
printf("Your message with the priority %d has been successfully sent to int main (int argc,char* *args) {
the %s\n",priority,args[1]); struct mq_attr attr;
mq_close(mqdes); mqd_t mqdes;
return 0; void *buff;
//mq_close (mqdes); int priority;
} int n=0;

ReceiveMessage.c ReceiveMessage.c
if(argc!=2){ mq_getattr(mqdes,&attr);
fprintf(stderr,"Usage: %s <Message-Queue Name>\n",args[0]); buff=malloc(attr.mq_msgsize);
fprintf(stderr,"For Example #%s /myMQ\n",args[0]); n=mq_receive(mqdes,buff,attr.mq_msgsize,&priority);
exit(0); printf("The received message is as follows. Its priority is
} %d:\n%s",priority,buff);
printf("Press any key to terminate\n");
mqdes = mq_open (args[1], O_RDONLY); getchar();
if(mqdes==-1){ mq_close(mqdes);
printf("error: %s (%d)\n",strerror(errno),errno); return 0;
return 1; }
}

4
7/21/2017

Compilation of Programs Execution of Programs


On command shell, execute following commands On command shell, execute following commands
$gcc CreateMessage.c –o CreateMessage –lrt $./CreateMessage /MyQueue
$gcc SendMessage.c –o SendMessage –lrt
$gcc ReceiveMessage.c –o ReceiveMessage –lrt $./SendMessage /MyQueue 1
..
• librt is needed to link the programs that use $./SendMessage /MyQueue 1
functions of message queue. ..
• Specify –lrt option at the time of building Perform six times, then
executables. $./ReceiveMessage /MyQueue

Execution of Programs Message Queue Limits


Send one more message of higher priority. • mq_maxmsg: the maximum number of messages on
$./SendMessage /MyQueue 5 the queue
• mq_msgsize: the maximum size of a given message.
Then receive a message
$./ReceiveMessage /MyQueue • MQ_OPEN_MAX: the maximum number of message
queues that a process can have open at once.
• MQ_PRIO_MAX: the maximum value plus one for the
priority of any message (Posix requires that this be at
least 32)

Message Queue Limits


• The values of MQ_OPEN_MAX and MQ_PRIO_MAX
can be obtained using sysconf function.

• E.g.,
printf(“MQ_OPEN_MAX = %ld, MQ_PRIO_MAX= %ld\n”,
sysconf(_SC_MQ_OPEN_MAX),
sysconf(_SC_ MQ_PRIO_MAX));

You might also like