mq_send /mq_receive problem
A message that is not in the archive (sent off-list or not yet received): [email protected]
I have a low priority thread running in the kernel which receives to a message queue and writes to the same message queue after it processes the message. The message queue is opened for reading and writing.
Without a delay (with nanosleep) between the reading (mq_receive) and writing (mq_send), the kernel hangs.
If I put in a small delay between the mq_send and mq_receive, the mechanism works fine.
Why is this?
***********************************************************************************
This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message.
***********************************************************************************
Quoted 4 lines · Bracha
I have a low priority thread running in the kernel which receives to a message queue and writes to the same message queue after it processes the message. The message queue is opened for reading and writing. Without a delay (with nanosleep) between the reading (
mq_receive) and writing (mq_send), the kernel hangs.
If I put in a small delay between themq_sendandmq_receive, the mechanism works fine.
Why is this?
You probably found a bug, could you send us a small example allowing us to reproduce this bug ?
Signature · 1 line
Gilles Chanteperdrix.
//********************************************************* // The following is a snippet from the kernel module:
// (If the delay is put in, it works fine.) //**********************************************************
static void *LinkThread (struct net_device *dev)
{
int status;
link_request linkr;struct timespec ttt,xxx;
ttt.tv_sec = 2;
ttt.tv_nsec = 0;
while (1)
{
if ((status=mq_receive (link_rq_mq, (char *)&linkr, sizeof (linkr), 0)) > 0)
{
//nanosleep (&ttt, &xxx);
status = mq_send (link_rq_mq_out, (char *)&linkr, sizeof (linkr), 0);
}
}
return 0;
}//****************************************************************** // Here's the user code that activates the above:
//***************************************************************** int CLinkState::GetLinkState (void)
{
struct link_request linkr;
linkr.request_type = get_link_state;
int status;
pthread_mutex_lock (&mutex);
status = mq_send (link_rq, (char *)&linkr, sizeof (linkr), 0);
DPRINT( ("GetLinkState: mq_send returned %08x\n", status) );
mq_receive (link_rq, (char *)&linkr, sizeof (linkr), 0);
DPRINT( ("GetLinkState: mq_receive returned %08x, link up = %08x\n", status, linkr.link_on) );
pthread_mutex_unlock (&mutex);
return linkr.link_on;
}-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Tuesday, August 01, 2006 5:33 PM
To: Landau, Bracha
Cc: [email protected]
Subject: Re: [Xenomai-help] mq_send /mq_receive problem
Quoted 4 lines · Bracha
I have a low priority thread running in the kernel which receives to a message queue and writes to the same message queue after it processes the message. The message queue is opened for reading and writing. Without a delay (with nanosleep) between the reading (
mq_receive) and writing (mq_send), the kernel hangs.
If I put in a small delay between themq_sendandmq_receive, the mechanism works fine.
Why is this?
You probably found a bug, could you send us a small example allowing us to reproduce this bug ?
Signature · 4 lines
Gilles Chanteperdrix. *********************************************************************************** This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message. ***********************************************************************************
Quoted 13 lines · Bracha
//********************************************************* // The following is a snippet from the kernel module:
// (If the delay is put in, it works fine.) //**********************************************************static void *LinkThread (struct
net_device*dev){ int status; link_request linkr;struct timespec ttt,xxx;
ttt.tv_sec = 2;
ttt.tv_nsec = 0;
Quoted 10 lines
while (1)
{
if ((status=mq_receive (link_rq_mq, (char *)&linkr, sizeof (linkr), 0)) > 0)
{
//nanosleep (&ttt, &xxx);
status = mq_send (link_rq_mq_out, (char *)&linkr, sizeof (linkr), 0);
}
}
return 0;
}What are the flags passed to mq_open ? If for example O_NONBLOCK is set, then the behaviour you observe is normal.
Signature · 1 line
Gilles Chanteperdrix.
The queue is opened with O_CREAT | O_RDWR.
Please note that I sent an updated version of the code that causes the crash - the queue in both the mq_send and mq_receive is "link_rq_mq".
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Tuesday, August 01, 2006 6:09 PM
To: Landau, Bracha
Cc: [email protected]
Subject: RE: [Xenomai-help] mq_send /mq_receive problem
Quoted 13 lines · Bracha
//********************************************************* // The following is a snippet from the kernel module:
// (If the delay is put in, it works fine.) //**********************************************************static void *LinkThread (struct
net_device*dev){ int status; link_request linkr;struct timespec ttt,xxx;
ttt.tv_sec = 2;
ttt.tv_nsec = 0;
Quoted 10 lines
while (1)
{
if ((status=mq_receive (link_rq_mq, (char *)&linkr, sizeof (linkr), 0)) > 0)
{
//nanosleep (&ttt, &xxx);
status = mq_send (link_rq_mq_out, (char *)&linkr, sizeof (linkr), 0);
}
}
return 0;
}What are the flags passed to mq_open ? If for example O_NONBLOCK is set, then the behaviour you observe is normal.
Signature · 4 lines
Gilles Chanteperdrix. *********************************************************************************** This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message. ***********************************************************************************
Quoted 2 lines · Bracha
The queue is opened with
O_CREAT|O_RDWR.
Please note that I sent an updated version of the code that causes the crash - the queue in both themq_sendandmq_receiveis "link_rq_mq".
I should have said "a self-contained example", what are the priorities of the two tasks ?
Signature · 1 line
Gilles Chanteperdrix.
Quoted 2 lines · Bracha
The queue is opened with
O_CREAT|O_RDWR.
Please note that I sent an updated version of the code that causes the crash - the queue in both themq_sendandmq_receiveis "link_rq_mq".
Merging your sample with the code of the satch example, I arrived to the attached test that work as expected as long as:
- the task priorities are correctly set
- there is no printf call between the mq_send and mq_receive in the "producer" function. It looks like such a printf suspends the GetLinkState thread out of mq_receive, so that when the LinkThread send a message, it is able to immediately receive it.
This state of affair is a bit fragile, in order to do something more robust, you should probably use one message queue for each direction.
Signature · 1 line
Gilles Chanteperdrix.
#ifdef __KERNEL__
#include <linux/module.h>
#elif !defined(__XENO_SIM__)
#include <stdio.h>
#define printk printf
#else
#endif
#ifndef __XENO_SIM__
#include <errno.h>
#include <pthread.h>
#include <mqueue.h>
#include <sys/mman.h>
#else
#include <posix/posix.h>
#endif
#define CONSUMER_TASK_PRI 1
#define CONSUMER_STACK_SIZE 8192
#define PRODUCER_TASK_PRI 2
#define PRODUCER_STACK_SIZE 8192
#define MQ_NAME "/mymq"
static pthread_t consumer_task, producer_task;
static mqd_t mqd = (mqd_t) -1;
static pthread_mutex_t mutex;
void *consumer(void *cookie)
{
int status;
unsigned linkr;
if ((status=mq_receive (mqd, (char *)&linkr, sizeof (linkr), 0)) > 0)
status = mq_send (mqd, (char *)&linkr, sizeof (linkr), 0);
else {
printk ("linkthread:mq_receive returned %08x, errno=%08x\n", status, errno);
return cookie;
}
status=mq_receive (mqd, (char *)&linkr, sizeof (linkr), 0);
printk("test failed!\n");
return 0;
}void *producer(void *cookie)
{
unsigned linkr = (unsigned) cookie;
int status;
status = mq_send (mqd, (char *)&linkr, sizeof (linkr), 0);
mq_receive (mqd, (char *)&linkr, sizeof (linkr), 0);
printk("test passed\n");
return (void *) linkr;
}void __xeno_user_exit(void)
{
if (consumer_task)
{
pthread_cancel(consumer_task);
pthread_join(consumer_task, NULL);
}
if (mqd != (mqd_t) -1)
mq_close(mqd);
if (producer_task)
{
pthread_cancel(producer_task);
pthread_join(producer_task, NULL);
}
mq_unlink(MQ_NAME);
}int __xeno_user_init (void)
{
struct sched_param parm;
pthread_attr_t attr;
int rc = 0;
struct mq_attr mattr;
pthread_mutex_init(&mutex, NULL);
mattr.mq_maxmsg = 30;
mattr.mq_msgsize = sizeof(unsigned);
mqd = mq_open(MQ_NAME, O_CREAT| O_EXCL| O_RDWR, 0, &mattr);
if (mqd == (mqd_t) -1)
{
if (errno == EEXIST)
{
printk("Mqtest: consumer module is already running, please "
"only launch one consumer instance.\n");
goto out;
}
printk("mq_open(mqd): %d\n", errno);
goto out;
}
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, CONSUMER_STACK_SIZE);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
parm.sched_priority = CONSUMER_TASK_PRI;
pthread_attr_setschedparam(&attr, &parm);
rc = pthread_create(&consumer_task, &attr, &consumer, NULL);
if (rc)
{
printk("pthread_create(consumer_task): %d\n", rc);
goto out;
}
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, PRODUCER_STACK_SIZE);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
parm.sched_priority = PRODUCER_TASK_PRI;
pthread_attr_setschedparam(&attr, &parm);
rc = pthread_create(&producer_task, &attr, &producer, (void *) 42);
if (rc)
{
printk("pthread_create(producer_task): %d\n", rc);
goto out;
}
return 0;out:
rc = -rc ?: -errno;
__xeno_user_exit();
return rc;
}#ifdef __KERNEL__ MODULE_AUTHOR("[email protected]"); MODULE_LICENSE("GPL"); module_init(__xeno_user_init); module_exit(__xeno_user_exit);
#elif !defined(__XENO_SIM__)
int main (int ac, char *av[])
{
sigset_t mask;
int rc, sig;
sigemptyset(&mask);
sigaddset(&mask,SIGINT);
sigaddset(&mask,SIGTERM);
sigaddset(&mask,SIGHUP);
sigaddset(&mask,SIGALRM);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
mlockall(MCL_CURRENT|MCL_FUTURE);
rc = __xeno_user_init();
if (rc)
{
printk("__xeno_user_init: %d\n", -rc);
return -rc;
}
sigwait(&mask, &sig);
__xeno_user_exit();
return 0;
}#endif /* !__XENO_SIM__ && !__KERNEL__ */
Thanks. I did have a printf call in my "producer" function between the send and receive.
Is this a bug?
Using two separate message queues does fix the problem, though I would expect that, as long as there is a mutex around the use in the "producer" function, one message queue should suffice.
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Tuesday, August 01, 2006 11:40 PM
To: Landau, Bracha
Cc: [email protected]
Subject: RE: [Xenomai-help] mq_send /mq_receive problem
Quoted 2 lines · Bracha
The queue is opened with
O_CREAT|O_RDWR.
Please note that I sent an updated version of the code that causes the crash - the queue in both themq_sendandmq_receiveis "link_rq_mq".
Merging your sample with the code of the satch example, I arrived to the attached test that work as expected as long as:
- the task priorities are correctly set
- there is no printf call between the mq_send and mq_receive in the "producer" function. It looks like such a printf suspends the GetLinkState thread out of mq_receive, so that when the LinkThread send a message, it is able to immediately receive it.
This state of affair is a bit fragile, in order to do something more robust, you should probably use one message queue for each direction.
Signature · 7 lines
Gilles Chanteperdrix. *** NDS UK IT scanned this email for malicious content *** *** IMPORTANT: Do not open attachments from unrecognized senders *** *********************************************************************************** This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message. ***********************************************************************************
Quoted 4 lines · Bracha
I have a low priority thread running in the kernel which receives to a message queue and writes to the same message queue after it processes the message. The message queue is opened for reading and writing. Without a delay (with nanosleep) between the reading (
mq_receive) and writing (mq_send), the kernel hangs.
If I put in a small delay between themq_sendandmq_receive, the mechanism works fine.
Why is this?
The normal (i.e. non-RT) kernel operations need some cycles to run too. Does your application enter an infinite loop receiving and sending messages?
Quoted 8 lines
***********************************************************************************
This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message.
***********************************************************************************_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help
Signature · 1 line
Philippe.
RE: [Xenomai-help] mq_send /mq_receive problem - code sample correction
In the code sample I sent, the following should be the kernel snippet:
*********************************************
static void *LinkThread (struct net_device *dev)
{
int status;
link_request linkr;struct timespec ttt,xxx;
ttt.tv_sec = 2;
ttt.tv_nsec = 0;
while (1)
{
if ((status=mq_receive (link_rq_mq, (char *)&linkr, sizeof (linkr), 0)) > 0)
{
//nanosleep (&ttt, &xxx);
status = mq_send (link_rq_mq, (char *)&linkr, sizeof (linkr), 0);
}
else
{
printk ("linkthread:mq_receive returned %08x, errno=%08x\n", status, errno);
break;
}
}
return 0;
}***********************************************************************************
This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message.
***********************************************************************************