All of lore.kernel.org
 help / color / mirror / Atom feed
* 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, &param);
	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
* [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, &param);



            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, &param);

            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, &param1);

            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, &param2);

            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

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.