msg_queue.c

00001 #include <sys/mman.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <native/task.h>
00005 #include <native/queue.h>
00006 
00007 #define TASK_PRIO  99 /* Highest RT priority */
00008 #define TASK_MODE  0  /* No flags */
00009 #define TASK_STKSZ 0  /* Stack size (use default one) */
00010 
00011 RT_QUEUE q_desc;
00012 
00013 RT_TASK task_desc;
00014 
00015 void consumer (void *cookie)
00016 
00017 {
00018     ssize_t len;
00019     void *msg;
00020     int err;
00021 
00022     /* Bind to a queue which has been created elsewhere, either in
00023        kernel or user-space. The call will block us until such queue
00024        is created with the expected name. The queue should have been
00025        created with the Q_SHARED mode set, which is implicit when
00026        creation takes place in user-space. */
00027 
00028     err = rt_queue_bind(&q_desc,"SomeQueueName",TM_INFINITE);
00029 
00030     if (err)
00031         fail();
00032 
00033     /* Collect each message sent to the queue by the queuer() routine,
00034        until the queue is eventually removed from the system by a call
00035        to rt_queue_delete(). */
00036 
00037     while ((len = rt_queue_receive(&q_desc,&msg,TM_INFINITE)) > 0)
00038         {
00039         printf("received message> len=%d bytes, ptr=%p, s=%s\n",
00040                len,msg,(const char *)msg);
00041         rt_queue_free(&q_desc,msg);
00042         }
00043 
00044     /* We need to unbind explicitly from the queue in order to
00045        properly release the underlying memory mapping. Exiting the
00046        process unbinds all mappings automatically. */
00047 
00048     rt_queue_unbind(&q_desc);
00049 
00050     if (len != -EIDRM)
00051         /* We received some unexpected error notification. */
00052         fail();
00053 
00054     /* ... */
00055 }
00056 
00057 int main (int argc, char *argv[])
00058 
00059 {
00060     static char *messages[] = { "hello", "world", NULL };
00061     int n, len;
00062     void *msg;
00063 
00064     mlockall(MCL_CURRENT|MCL_FUTURE);
00065 
00066     err = rt_task_create(&task_desc,
00067                          "MyTaskName",
00068                          TASK_STKSZ,
00069                          TASK_PRIO,
00070                          TASK_MODE);
00071     if (!err)
00072         rt_task_start(&task_desc,&task_body,NULL);
00073 
00074     /* ... */
00075 
00076     for (n = 0; messages[n] != NULL; n++)
00077         {
00078         len = strlen(messages[n]) + 1;
00079         /* Get a message block of the right size. */
00080         msg = rt_queue_alloc(&q_desc,len);
00081 
00082         if (!msg)
00083             /* No memory available. */
00084             fail();
00085 
00086         strcpy(msg,messages[n]);
00087         rt_queue_send(&q_desc,msg,len,Q_NORMAL);
00088         }
00089 
00090     rt_task_delete(&task_desc);
00091 }

Generated on Mon Mar 24 18:02:40 2008 for Xenomai API by  doxygen 1.5.3