Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending int with mq_send

Tags:

posix

queue

The solution in the post below doesn't work for me. I get error message "Message too long". What can be the problem? How to send integer with message queue with POSIX API in linux?

If I am correct a pid_t is defined as an int. I have done the following:

struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 1000;
attr.mq_msgsize = sizeof(pid_t);

mqd_t queue = mq_open(unique_name, O_RDWR|O_CREAT, 0600, &attr);

mqd_t result = mq_send(queue, &pid, sizeof(pid), 0);

I get the following error at compilation at the line of mq_send:

"passing argument 2 of 'mq_send' from incompatible pointer type"
"initialization makes pointer from integer without a cast"

like image 212
user1766169 Avatar asked Nov 07 '12 19:11

user1766169


People also ask

What is Mq_send?

The mq_send subroutine adds the message pointed to by the msg_ptr parameter to the message queue specified by the mqdes parameter. The msg_len parameter specifies the length of the message, in bytes, pointed to by msg_ptr.

Is Mq_receive blocking?

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.

What is Mqd_t in C?

DESCRIPTION. The <mqueue. h> header defines the mqd_t type, which is used for message queue descriptors. This will not be an array type. A message queue descriptor may be implemented using a file descriptor, in which case applications can open up to at least {OPEN_MAX} file and message queues.

What is Posix message queue?

POSIX message queues are a means by which processes exchange data in the form of messages to accomplish their tasks. They enable processes to synchronise their reads and writes to speed up processes. POSIX message queues are distinct from System V messages.


2 Answers

The problem was that I never did mq_unlink.

like image 197
user1766169 Avatar answered Sep 28 '22 00:09

user1766169


You probably want to set the maximum message size and queue size with an mq_attr first. See this post for more detail on POSIX queues.

like image 27
Joe Avatar answered Sep 28 '22 01:09

Joe