Karch, Joshua wrote:
> Hi All,
> 
> I tried implementing inter-task message queues and following the
> msg_queue.c example
> (http://www.xenomai.org/documentation/trunk/html/api/msg__queue_8c-example.html)
> and when I try to run the implementation after fixing some of the code
> (declaring fail() to printf an error, declaring int err, and changing
> &task_body to &consumer), I get the following error
> msg_queue[3897]: segfault at 00000000 eip b7cf8933 esp bf8dce04 error
> 6.  This comes out of the rt_queue_alloc function.
> 
> Has the implementation of message passing between userspace tasks
> changed?  Is there a more recent example?  My end goal is to send non
> blocking messages from one producer task at 50Hz to two reading tasks
> tasks with similar (though not exactly same) loop rates-- i.e. None of
> the tasks are synchronized, though they all run at about 50Hz.  Should I
> use message queues to do this?
>

Btw, that code skeleton was meant to be paired with a peer module in
kernel/userland that would create the queue. Nothing in this code does this; you
need to call rt_queue_create() at init to create the queue the consumer task
will bind to.

> Thank you,
> 
> Joshua Karch
> 
> Attached is the code I used..
> 
> #include <sys/mman.h>
> #include <stdio.h>
> #include <string.h>
> #include <native/task.h>
> #include <native/queue.h>
> 
> #define TASK_PRIO  99 /* Highest RT priority */
> #define TASK_MODE  0  /* No flags */
> #define TASK_STKSZ 0  /* Stack size (use default one) */
> 
> RT_QUEUE q_desc;
> 
> RT_TASK task_desc;
> 
> void fail()
> {
>         printf("error\n");
> }
> 
> 
> void consumer (void *cookie)
> 
> {
>     ssize_t len;
>     void *msg;
>     int err;
> 
>     /* Bind to a queue which has been created elsewhere, either in
>        kernel or user-space. The call will block us until such queue
>        is created with the expected name. The queue should have been
>        created with the Q_SHARED mode set, which is implicit when
>        creation takes place in user-space. */
> 
>     err = rt_queue_bind(&q_desc,"SomeQueueName",TM_INFINITE);
> 
>     if (err)
>         fail();
> 
>     /* Collect each message sent to the queue by the queuer() routine,
>        until the queue is eventually removed from the system by a call
>        to rt_queue_delete(). */
> 
>     while ((len = rt_queue_receive(&q_desc,&msg,TM_INFINITE)) > 0)
>         {
>         printf("received message> len=%d bytes, ptr=%p, s=%s\n",
>                len,msg,(const char *)msg);
>         rt_queue_free(&q_desc,msg);
>         }
> 
>     /* We need to unbind explicitly from the queue in order to
>        properly release the underlying memory mapping. Exiting the
>        process unbinds all mappings automatically. */
> 
>     rt_queue_unbind(&q_desc);
> 
>     if (len != -EIDRM)
>         /* We received some unexpected error notification. */
>         fail();
> 
>     /* ... */
> }
> 
> 
> int main (int argc, char *argv[])
> 
> {
>     static char *messages[] = { "hello", "world", NULL };
>     int n, len;
>     void *msg;
>     int err;
> 
>     mlockall(MCL_CURRENT|MCL_FUTURE);
> 
>     err = rt_task_create(&task_desc,
>                          "MyTaskName",
>                          TASK_STKSZ,
>                          TASK_PRIO,
>                          TASK_MODE);
>     if (!err)
>         rt_task_start(&task_desc,&consumer,NULL);
> 
>     /* ... */
> 
>     for (n = 0; messages[n] != NULL; n++)
>         {
>         len = strlen(messages[n]) + 1;
>         /* Get a message block of the right size. */
>         msg = rt_queue_alloc(&q_desc,len);
> 
>         if (!msg)
>             /* No memory available. */
>             fail();
> 
>         strcpy((char *)msg,messages[n]);
>         rt_queue_send(&q_desc,msg,len,Q_NORMAL);
>         }
> 
>     rt_task_delete(&task_desc);
> }
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Xenomai-help mailing list
> Xenomai-help@gna.org
> https://mail.gna.org/listinfo/xenomai-help


-- 
Philippe.

_______________________________________________
Xenomai-help mailing list
Xenomai-help@gna.org
https://mail.gna.org/listinfo/xenomai-help

Reply via email to