File: msg__queue_8c-example.html

package info (click to toggle)
xenomai 2.5.4-3squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 36,140 kB
  • ctags: 35,509
  • sloc: ansic: 109,084; sh: 11,745; makefile: 2,205; xml: 1,356; asm: 613; php: 316; perl: 155
file content (127 lines) | stat: -rw-r--r-- 7,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Xenomai API: msg_queue.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    <li>
      <form action="search.php" method="get">
        <table cellspacing="0" cellpadding="0" border="0">
          <tr>
            <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
            <td><input type="text" name="query" value="" size="20" accesskey="s"/></td>
          </tr>
        </table>
      </form>
    </li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>msg_queue.c</h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;sys/mman.h&gt;</span>
<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;string.h&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="task_8h.html" title="This file is part of the Xenomai project.">native/task.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="native_2queue_8h.html" title="This file is part of the Xenomai project.">native/queue.h</a>&gt;</span>

<span class="preprocessor">#define TASK_PRIO  99 </span><span class="comment">/* Highest RT priority */</span>
<span class="preprocessor">#define TASK_MODE  0  </span><span class="comment">/* No flags */</span>
<span class="preprocessor">#define TASK_STKSZ 0  </span><span class="comment">/* Stack size (use default one) */</span>

RT_QUEUE q_desc;

RT_TASK task_desc;

<span class="keywordtype">void</span> consumer (<span class="keywordtype">void</span> *cookie)

{
    ssize_t len;
    <span class="keywordtype">void</span> *msg;
    <span class="keywordtype">int</span> err;

    <span class="comment">/* Bind to a queue which has been created elsewhere, either in</span>
<span class="comment">       kernel or user-space. The call will block us until such queue</span>
<span class="comment">       is created with the expected name. The queue should have been</span>
<span class="comment">       created with the Q_SHARED mode set, which is implicit when</span>
<span class="comment">       creation takes place in user-space. */</span>

    err = <a name="a0"></a><a class="code" href="group__native__queue.html#g162bb372c5c3616666e8e1fbdcc79343" title="Bind to a shared message queue.">rt_queue_bind</a>(&amp;q_desc,<span class="stringliteral">"SomeQueueName"</span>,TM_INFINITE);

    <span class="keywordflow">if</span> (err)
        fail();

    <span class="comment">/* Collect each message sent to the queue by the queuer() routine,</span>
<span class="comment">       until the queue is eventually removed from the system by a call</span>
<span class="comment">       to rt_queue_delete(). */</span>

    <span class="keywordflow">while</span> ((len = <a name="a1"></a><a class="code" href="group__native__queue.html#g44759168a589ddbb2b60c1c1b183f448" title="Receive a message from a queue.">rt_queue_receive</a>(&amp;q_desc,&amp;msg,TM_INFINITE)) &gt; 0)
        {
        printf(<span class="stringliteral">"received message&gt; len=%d bytes, ptr=%p, s=%s\n"</span>,
               len,msg,(<span class="keyword">const</span> <span class="keywordtype">char</span> *)msg);
        <a name="a2"></a><a class="code" href="group__native__queue.html#g40d16a5ab14ea033889bb839967204e4" title="Free a message queue buffer.">rt_queue_free</a>(&amp;q_desc,msg);
        }

    <span class="comment">/* We need to unbind explicitly from the queue in order to</span>
<span class="comment">       properly release the underlying memory mapping. Exiting the</span>
<span class="comment">       process unbinds all mappings automatically. */</span>

    <a name="a3"></a><a class="code" href="group__native__queue.html#g1f190858ca18d906db339e086bfa8ced" title="Unbind from a shared message queue.">rt_queue_unbind</a>(&amp;q_desc);

    <span class="keywordflow">if</span> (len != -EIDRM)
        <span class="comment">/* We received some unexpected error notification. */</span>
        fail();

    <span class="comment">/* ... */</span>
}

<span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])

{
    <span class="keyword">static</span> <span class="keywordtype">char</span> *messages[] = { <span class="stringliteral">"hello"</span>, <span class="stringliteral">"world"</span>, NULL };
    <span class="keywordtype">int</span> n, len;
    <span class="keywordtype">void</span> *msg;

    mlockall(MCL_CURRENT|MCL_FUTURE);

    err = <a name="a4"></a><a class="code" href="group__task.html#g03387550693c21d0223f739570ccd992" title="Create a new real-time task.">rt_task_create</a>(&amp;task_desc,
                         <span class="stringliteral">"MyTaskName"</span>,
                         TASK_STKSZ,
                         TASK_PRIO,
                         TASK_MODE);
    <span class="keywordflow">if</span> (!err)
        <a name="a5"></a><a class="code" href="group__task.html#gc9638918b8310a430088f5c9a04d2bb7" title="Start a real-time task.">rt_task_start</a>(&amp;task_desc,&amp;task_body,NULL);

    <span class="comment">/* ... */</span>

    <span class="keywordflow">for</span> (n = 0; messages[n] != NULL; n++)
        {
        len = strlen(messages[n]) + 1;
        <span class="comment">/* Get a message block of the right size. */</span>
        msg = <a name="a6"></a><a class="code" href="group__native__queue.html#g8eaee8ca7d0c3af801c4df6f0adae421" title="Allocate a message queue buffer.">rt_queue_alloc</a>(&amp;q_desc,len);

        <span class="keywordflow">if</span> (!msg)
            <span class="comment">/* No memory available. */</span>
            fail();

        strcpy(msg,messages[n]);
        <a name="a7"></a><a class="code" href="group__native__queue.html#g8b1dcb15753bdfce02f25f166e18948d" title="Send a message to a queue.">rt_queue_send</a>(&amp;q_desc,msg,len,Q_NORMAL);
        }

    <a name="a8"></a><a class="code" href="group__task.html#gb6e0d411830710e8cc82d77b9df19510" title="Delete a real-time task.">rt_task_delete</a>(&amp;task_desc);
}
</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Mon Aug 2 12:48:36 2010 for Xenomai API by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>