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"
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.
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.
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.
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.
The problem was that I never did mq_unlink
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With