Problem with Message queue
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
Quoted 5 lines · Perrine Martignoni
Hello,
I'm writing an application using Message queue and
mq_sendreturns 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.
Signature · 1 line
Gilles Chanteperdrix
Quoted 7 lines · Perrine Martignoni
Hello,
I'm writing an application using Message queue and
mq_sendreturns 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.
Quoted 3 lines
(...)
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.
Signature · 1 line
Gilles Chanteperdrix.
Quoted 2 lines · Gilles Chanteperdrix
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,.
Signature · 1 line
Gilles Chanteperdrix.