* [Xenomai-help] Problem with Message queue
@ 2007-05-14 9:21 Perrine Martignoni
2007-05-14 9:51 ` Gilles Chanteperdrix
2007-05-14 11:04 ` Gilles Chanteperdrix
0 siblings, 2 replies; 8+ messages in thread
From: Perrine Martignoni @ 2007-05-14 9:21 UTC (permalink / raw)
To: xenomai
[-- Attachment #1: Type: text/plain, Size: 5837 bytes --]
Hello,
I'm writing an application using Message queue and mq_send returns the errno
value EBADF.
I don't understand what I'm doing wrong, I need some help.
Here's my code :
pthread_t task1, task2;
pthread_mutex_t mutex;
void routine1 (void * cookie);
void routine2 (void * cookie);
int tab[200];
int DESTRUCT=0;
mqd_t mq1, mq2;
#define MAX_MSGS 5
#define MSG_SIZE 50
char buf1[MSG_SIZE];
char buf2[MSG_SIZE];
void affich(int toto)
{
static int cpt = 0;
pthread_mutex_lock(&mutex);
tab[cpt] = toto;
cpt ++;
if(cpt == 150){
cpt = 0;
}
pthread_mutex_unlock(&mutex);
}
void routine1 (void * cookie){
int ret;
struct sched_param param = {.sched_priority = 10 };
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
for(;;){
affich(1);
ret = mq_receive(mq2, buf2,MSG_SIZE, NULL);
printf("receive mq2 returned %d, errno %d\n", ret,
errno);
ret = mq_send(mq1, buf1, MSG_SIZE, 0);
printf("send mq1 returned %d, errno %d\n", ret,
errno );
if(DESTRUCT==1){
pthread_exit(NULL);
};
}
}
void routine2 (void * cookie){
int ret;
struct sched_param param = {.sched_priority = 10 };
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
for(;;){
affich(2);
ret = mq_send(mq2, buf2, MSG_SIZE, 0);
printf("send mq2 returned %d, errno %s\n", ret,
errno );
ret = mq_receive(mq1, buf1, MSG_SIZE, NULL );
printf("receive mq1 returned %d, errno %s\n", ret,
errno);
if(DESTRUCT==1){
pthread_exit(NULL);
};
}
}
void cleanup_upon_sig(int sig __attribute__((unused)))
{
DESTRUCT=1;
}
int main(int argc, char **argv)
{
int i = 0;
int err;
sigset_t mask, oldmask;
pthread_attr_t attr1, attr2;
struct sched_param param1 = {.sched_priority = 10 };
struct sched_param param2 = {.sched_priority = 10 };
struct mq_attr qattr1, qattr2;
mlockall(MCL_CURRENT | MCL_FUTURE);
/*création du mutex*/
pthread_mutex_init(&mutex,NULL);
/*création des Message_queue*/
qattr1.mq_maxmsg = MAX_MSGS;
qattr1.mq_msgsize = MSG_SIZE;
qattr2.mq_maxmsg = MAX_MSGS;
qattr2.mq_msgsize = MSG_SIZE;
if (mq1=mq_open("/mq1", O_CREAT|O_RDWR, 0, &qattr1) ==
(mqd_t)(-1)) {
perror( "mq1_open failed" );
exit(1);
}
if (mq2=mq_open("/mq2", O_CREAT|O_RDWR, 0, &qattr2) ==
(mqd_t)(-1)) {
perror( "mq2_open failed" );
exit(1);
}
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
signal(SIGINT, cleanup_upon_sig);
sigaddset(&mask, SIGTERM);
signal(SIGTERM, cleanup_upon_sig);
sigaddset(&mask, SIGHUP);
signal(SIGHUP, cleanup_upon_sig);
pthread_sigmask(SIG_BLOCK, &mask, &oldmask);
/*Initialisation des attributs des tâches*/
pthread_attr_init(&attr1);
pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
pthread_attr_setschedparam(&attr1, ¶m1);
pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN);
pthread_attr_init(&attr2);
pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
pthread_attr_setschedparam(&attr2, ¶m2);
pthread_attr_setstacksize(&attr2, PTHREAD_STACK_MIN);
if (err = pthread_create (&task1,&attr1,(void *)routine1,NULL))
fprintf(stderr, "failed to create threads: %s\n",
strerror(err));
if (err = pthread_create (&task2,&attr2,(void *)routine2,NULL))
fprintf(stderr, "failed to create threads: %s\n",
strerror(err));
pthread_sigmask(SIG_UNBLOCK, &mask, &oldmask);
pthread_join(task1, NULL);
pthread_join(task2, NULL);
/*destruction mutex*/
if (err = pthread_mutex_destroy(&mutex))
fprintf(stderr, "failed to cancel mutex");
/*destruction message_queue*/
if( mq_close(mq1) == -1 ) {
perror( "mq1_close failed" );
exit(1);
}
if( mq_close(mq2) == -1 ) {
perror( "mq2_close failed" );
exit(1);
}
for(i = 0; i < 20 ; i++)
{
printf("Task %d\n",tab[i]);
}
return 0;
}
Thanks
[-- Attachment #2: Type: text/html, Size: 31846 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 9:21 Perrine Martignoni
@ 2007-05-14 9:51 ` Gilles Chanteperdrix
2007-05-14 11:04 ` Gilles Chanteperdrix
1 sibling, 0 replies; 8+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-14 9:51 UTC (permalink / raw)
To: Perrine Martignoni; +Cc: xenomai
Perrine Martignoni wrote:
> Hello,
>
> I'm writing an application using Message queue and mq_send returns the
> errno value EBADF.
> I don't understand what I'm doing wrong, I need some help.
I see nothing obvious, EBADF usually means that the queue descriptor is
incorrect, or that the open mode is incorrect. I will try your example
later.
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 9:21 Perrine Martignoni
2007-05-14 9:51 ` Gilles Chanteperdrix
@ 2007-05-14 11:04 ` Gilles Chanteperdrix
2007-05-14 11:57 ` Gilles Chanteperdrix
1 sibling, 1 reply; 8+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-14 11:04 UTC (permalink / raw)
To: Perrine Martignoni; +Cc: xenomai
Perrine Martignoni wrote:
> Hello,
>
> I'm writing an application using Message queue and mq_send returns the errno
> value EBADF.
> I don't understand what I'm doing wrong, I need some help.
>
> Here's my code :
Next time, please do not omit the #includes, I need them to compile your
program.
> (...)
> if (mq1=mq_open("/mq1", O_CREAT|O_RDWR, 0, &qattr1) ==
> (mqd_t)(-1)) {
Here gcc gives the following message:
test_mq.c:186: warning: suggest parentheses around assignment used as truth value
Which should be the sign that something is not correct,
a=b == c is interpreted by the compiler as a=(b == c), whereas you would
like
(a = b) == c.
So, add the parentheses, and everything should work fine.
However, I do not understand what your program is supposed to do, here
it dies without printing anything.
--
Gilles Chanteperdrix.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 11:04 ` Gilles Chanteperdrix
@ 2007-05-14 11:57 ` Gilles Chanteperdrix
0 siblings, 0 replies; 8+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-14 11:57 UTC (permalink / raw)
To: Perrine Martignoni, xenomai
Gilles Chanteperdrix wrote:
> However, I do not understand what your program is supposed to do, here
> it dies without printing anything.
There is definitely something wrong on ARM. The program works as
expected on x86, it dies on ARM at exactly the second printf of
routine2. Replacing printf with fprintf(stderr works. Supposing that it
could be a stack size issue, I increased it, and it does not change
anything.
More on this tonight, in the meantime, please replace printf with
fprintf(stderr,.
--
Gilles Chanteperdrix.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
@ 2007-05-14 12:11 Fillod Stephane
2007-05-14 13:36 ` Gilles Chanteperdrix
2007-05-14 19:16 ` Gilles Chanteperdrix
0 siblings, 2 replies; 8+ messages in thread
From: Fillod Stephane @ 2007-05-14 12:11 UTC (permalink / raw)
To: Perrine Martignoni, xenomai
Perrine Martignoni wrote:
[..]
>int DESTRUCT=0;
I'm always nervous to see global variables shared among threads without
a volatile keyword. An explicit memory barrier would be better, as it
has been re-explained in a LWN article lately (wait Thursday for free access)
http://lwn.net/Articles/233479/
But the problem in routine2 is somewhere else:
void routine2 (void * cookie){
int ret;
struct sched_param param = {.sched_priority = 10 };
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
for(;;){
affich(2);
ret = mq_send(mq2, buf2, MSG_SIZE, 0);
printf("send mq2 returned %d, errno %s\n", ret, errno );
ouch! ^^ ^^^^^
ret = mq_receive(mq1, buf1, MSG_SIZE, NULL );
printf("receive mq1 returned %d, errno %s\n", ret, errno);
ouch! ^^ ^^^^^
This should be either
printf("receive mq1 returned %d, errno %d\n", ret, errno);
or
printf("receive mq1 returned %d, errno %s\n", ret, strerror(errno));
--
Stephane
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 12:11 [Xenomai-help] Problem with Message queue Fillod Stephane
@ 2007-05-14 13:36 ` Gilles Chanteperdrix
2007-05-14 19:16 ` Gilles Chanteperdrix
1 sibling, 0 replies; 8+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-14 13:36 UTC (permalink / raw)
To: Fillod Stephane; +Cc: xenomai
Fillod Stephane wrote:
> Perrine Martignoni wrote:
> [..]
>
>>int DESTRUCT=0;
>
>
> I'm always nervous to see global variables shared among threads without
> a volatile keyword. An explicit memory barrier would be better, as it
> has been re-explained in a LWN article lately (wait Thursday for free access)
> http://lwn.net/Articles/233479/
>
> But the problem in routine2 is somewhere else:
>
> void routine2 (void * cookie){
> int ret;
> struct sched_param param = {.sched_priority = 10 };
> pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
> for(;;){
> affich(2);
> ret = mq_send(mq2, buf2, MSG_SIZE, 0);
> printf("send mq2 returned %d, errno %s\n", ret, errno );
> ouch! ^^ ^^^^^
> ret = mq_receive(mq1, buf1, MSG_SIZE, NULL );
> printf("receive mq1 returned %d, errno %s\n", ret, errno);
> ouch! ^^ ^^^^^
>
>
> This should be either
> printf("receive mq1 returned %d, errno %d\n", ret, errno);
> or
> printf("receive mq1 returned %d, errno %s\n", ret, strerror(errno));
Yes, I did not look at gcc warnings long enough. Yet another error (but
this one is arguably a bug in Xenomai posix skin) is to pass a null
pointer as last argument of mq_receive.
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 12:11 [Xenomai-help] Problem with Message queue Fillod Stephane
2007-05-14 13:36 ` Gilles Chanteperdrix
@ 2007-05-14 19:16 ` Gilles Chanteperdrix
2007-05-15 8:00 ` Perrine Martignoni
1 sibling, 1 reply; 8+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-14 19:16 UTC (permalink / raw)
To: Fillod Stephane; +Cc: xenomai
Fillod Stephane wrote:
> Perrine Martignoni wrote:
> [..]
> >int DESTRUCT=0;
>
> I'm always nervous to see global variables shared among threads without
> a volatile keyword. An explicit memory barrier would be better, as it
> has been re-explained in a LWN article lately (wait Thursday for free access)
> http://lwn.net/Articles/233479/
Besides, this termination mechanism results on a deadlock on SMP. Please
kill this DESTRUCT variable and replace cleanup_upon_sig with:
void cleanup_upon_sig(int sig __attribute__((unused)))
{
pthread_cancel(task1);
pthread_cancel(task2);
}
--
Gilles Chanteperdrix.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Xenomai-help] Problem with Message queue
2007-05-14 19:16 ` Gilles Chanteperdrix
@ 2007-05-15 8:00 ` Perrine Martignoni
0 siblings, 0 replies; 8+ messages in thread
From: Perrine Martignoni @ 2007-05-15 8:00 UTC (permalink / raw)
To: xenomai
[-- Attachment #1: Type: text/plain, Size: 842 bytes --]
Thanks for all of your ideas.
I solve my issue.
On 5/14/07, Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org> wrote:
>
> Fillod Stephane wrote:
> > Perrine Martignoni wrote:
> > [..]
> > >int DESTRUCT=0;
> >
> > I'm always nervous to see global variables shared among threads without
> > a volatile keyword. An explicit memory barrier would be better, as it
> > has been re-explained in a LWN article lately (wait Thursday for free
> access)
> > http://lwn.net/Articles/233479/
>
> Besides, this termination mechanism results on a deadlock on SMP. Please
> kill this DESTRUCT variable and replace cleanup_upon_sig with:
>
> void cleanup_upon_sig(int sig __attribute__((unused)))
>
> {
> pthread_cancel(task1);
> pthread_cancel(task2);
> }
>
>
> --
>
>
> Gilles Chanteperdrix.
>
[-- Attachment #2: Type: text/html, Size: 1519 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2007-05-15 8:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-14 12:11 [Xenomai-help] Problem with Message queue Fillod Stephane
2007-05-14 13:36 ` Gilles Chanteperdrix
2007-05-14 19:16 ` Gilles Chanteperdrix
2007-05-15 8:00 ` Perrine Martignoni
-- strict thread matches above, loose matches on Subject: below --
2007-05-14 9:21 Perrine Martignoni
2007-05-14 9:51 ` Gilles Chanteperdrix
2007-05-14 11:04 ` Gilles Chanteperdrix
2007-05-14 11:57 ` Gilles Chanteperdrix
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.