Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/producerconsumer_8c_source.html
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 6 43e37759235e
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/PIPS/opencproducerconsumerex/src/producerconsumer.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/PIPS/opencproducerconsumerex/src/producerconsumer.c</h1><a href="producerconsumer_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00016"></a>00016 <span class="comment">/* INCLUDE FILES */</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;sys/ioctl.h&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00026"></a>00026 <span class="comment">//#include &lt;staticlibinit_gcce.h&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &quot;CommanHeader.h&quot;</span>
<a name="l00028"></a>00028 
<a name="l00029"></a>00029 <span class="keywordtype">int</span> <a class="code" href="redirectprintf_8c.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>() 
<a name="l00030"></a>00030 {
<a name="l00031"></a>00031         <a class="code" href="struct_thread_param.html">ThreadParam</a> threadParams[MAX_CLIENTS + 1];
<a name="l00032"></a>00032         pthread_t threadID[MAX_CLIENTS + 1];
<a name="l00033"></a>00033         <span class="keywordtype">int</span> i = 0;
<a name="l00034"></a>00034         <span class="keywordtype">int</span> noOfConsumers = 0;
<a name="l00035"></a>00035         <span class="keywordtype">int</span> noOfItems = 0;
<a name="l00036"></a>00036         <span class="keywordtype">int</span> totalItems = 0;
<a name="l00037"></a>00037         <span class="keywordtype">int</span> ret = 0;
<a name="l00038"></a>00038         <span class="keywordtype">int</span> exitReason = 0;
<a name="l00039"></a>00039         sem_t semLock;
<a name="l00040"></a>00040         pthread_attr_t threadAttr;
<a name="l00041"></a>00041 
<a name="l00042"></a>00042         <span class="keywordflow">if</span>( sem_init( &amp;semLock, 0, 1 ) != 0 ) 
<a name="l00043"></a>00043         {
<a name="l00044"></a>00044                 printf(<span class="stringliteral">&quot;Error Creating semaphore&quot;</span>);
<a name="l00045"></a>00045                 exit(-1);
<a name="l00046"></a>00046         }
<a name="l00047"></a>00047 
<a name="l00048"></a>00048         <span class="comment">/* Read all the inputs needed */</span>
<a name="l00049"></a>00049         printf(<span class="stringliteral">&quot;Enter Number of Consumers : &quot;</span>);
<a name="l00050"></a>00050         scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;noOfConsumers);
<a name="l00051"></a>00051 
<a name="l00052"></a>00052         <span class="keywordflow">if</span>(MAX_CLIENTS &lt; noOfConsumers) 
<a name="l00053"></a>00053         {
<a name="l00054"></a>00054                 noOfConsumers = MAX_CLIENTS;
<a name="l00055"></a>00055         }
<a name="l00056"></a>00056 
<a name="l00057"></a>00057         <span class="keywordflow">for</span>(i=1; i&lt;=noOfConsumers; i++) 
<a name="l00058"></a>00058         {
<a name="l00059"></a>00059                 printf(<span class="stringliteral">&quot;Items Needed by Consumer-%d : &quot;</span>, i);
<a name="l00060"></a>00060                 scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;noOfItems);
<a name="l00061"></a>00061                 totalItems += noOfItems;
<a name="l00062"></a>00062 
<a name="l00063"></a>00063                 threadParams[i].noOfItems = noOfItems;
<a name="l00064"></a>00064                 threadParams[i].itemLock = semLock;
<a name="l00065"></a>00065                 threadParams[i].noOfConsumers = i;
<a name="l00066"></a>00066         }
<a name="l00067"></a>00067 
<a name="l00068"></a>00068         <span class="comment">/* For the Producer      */</span>
<a name="l00069"></a>00069         threadParams[0].noOfItems = totalItems;
<a name="l00070"></a>00070         threadParams[0].noOfConsumers = noOfConsumers;
<a name="l00071"></a>00071         threadParams[0].itemLock = semLock;
<a name="l00072"></a>00072 
<a name="l00073"></a>00073         pthread_attr_init( &amp;threadAttr );
<a name="l00074"></a>00074         pthread_attr_setdetachstate( &amp;threadAttr, PTHREAD_CREATE_JOINABLE );
<a name="l00075"></a>00075         
<a name="l00076"></a>00076         <span class="comment">/* Create a Producer thread now */</span>
<a name="l00077"></a>00077         ret = pthread_create( &amp;threadID[0], &amp;threadAttr, <a class="code" href="producer_8c.html#a9ba1d3d3c7c4fefedf72684e9abdc8ee">ProducerThreadEntryPoint</a>, 
<a name="l00078"></a>00078                 (<span class="keywordtype">void</span>*)&amp;threadParams[0] );
<a name="l00079"></a>00079 
<a name="l00080"></a>00080         <span class="keywordflow">if</span>(ret != 0) 
<a name="l00081"></a>00081         {
<a name="l00082"></a>00082                 printf(<span class="stringliteral">&quot;Error Creating Producer Thread&quot;</span>);
<a name="l00083"></a>00083                 exit(-1);
<a name="l00084"></a>00084         }
<a name="l00085"></a>00085 
<a name="l00086"></a>00086         <span class="comment">/* Create a Consumer Threads now */</span>
<a name="l00087"></a>00087         <span class="keywordflow">for</span>(i=1; i &lt;= noOfConsumers; i++) 
<a name="l00088"></a>00088         {
<a name="l00089"></a>00089                 ret = pthread_create( &amp;threadID[i], &amp;threadAttr, <a class="code" href="consumer_8c.html#aeb7daec86aaf17cff923e4e69e1ca964">ConsumerThreadEntryPoint</a>, 
<a name="l00090"></a>00090                         (<span class="keywordtype">void</span>*)&amp;threadParams[i] );
<a name="l00091"></a>00091 
<a name="l00092"></a>00092                 <span class="keywordflow">if</span>(ret != 0) 
<a name="l00093"></a>00093                 {
<a name="l00094"></a>00094                         printf(<span class="stringliteral">&quot;Error Creating Consumer Thread %d&quot;</span>, i);
<a name="l00095"></a>00095                         exit(-1);
<a name="l00096"></a>00096                 }
<a name="l00097"></a>00097         }
<a name="l00098"></a>00098 
<a name="l00099"></a>00099         <span class="comment">/* Call C++ function that creats Observer Thread */</span>
<a name="l00100"></a>00100         <a class="code" href="observer_8cpp.html#abe25ac9eb42087610e2949b201e2f0b5">CreateObserverThread</a> (totalItems * 2);
<a name="l00101"></a>00101 
<a name="l00102"></a>00102         <span class="comment">/* Wait for the completion of all the threads */</span>
<a name="l00103"></a>00103         <span class="keywordflow">for</span>(i=0; i&lt;=noOfConsumers; i++) 
<a name="l00104"></a>00104         {
<a name="l00105"></a>00105                 ret = pthread_join(threadID[i], (<span class="keywordtype">void</span>**)&amp;exitReason );
<a name="l00106"></a>00106         }
<a name="l00107"></a>00107 
<a name="l00108"></a>00108         <span class="comment">/* Destroy the semaphore */</span>
<a name="l00109"></a>00109         sem_destroy( &amp;semLock );
<a name="l00110"></a>00110         printf(<span class="stringliteral">&quot;Completed the Production/Consumption..\n Press Any Key to Exit&quot;</span>);
<a name="l00111"></a>00111         getchar();
<a name="l00112"></a>00112         getchar();
<a name="l00113"></a>00113         <span class="keywordflow">return</span> 0;
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00116"></a>00116 <span class="comment">/*  End of File */</span>
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>