Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/producer_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/producer.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/producer.c</h1><a href="producer_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00015"></a>00015 <span class="comment">/* INCLUDE FILES */</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;sys/ipc.h&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &lt;sys/msg.h&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 <span class="preprocessor">#include &quot;CommanHeader.h&quot;</span>
<a name="l00023"></a>00023 
<a name="l00024"></a>00024 <span class="preprocessor">#define MIN(X, Y) (X &lt; Y) ? X : Y;</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span>
<a name="l00026"></a>00026 <span class="comment">/*****************************************************************************</span>
<a name="l00027"></a>00027 <span class="comment">*  ProducerThreadEntryPoint</span>
<a name="l00028"></a>00028 <span class="comment">*  Function: Producer Thread that produces M number of items at a time </span>
<a name="l00029"></a>00029 <span class="comment">*  where M is number of Consumers.</span>
<a name="l00030"></a>00030 <span class="comment">*  This thread will produce N number of items where N is total number of items </span>
<a name="l00031"></a>00031 <span class="comment">*  needed by all the Consumers.</span>
<a name="l00032"></a>00032 <span class="comment">*  It also informs the Observer thread about item production</span>
<a name="l00033"></a>00033 <span class="comment">*******************************************************************************/</span>
<a name="l00034"></a>00034 
<a name="l00035"></a><a class="code" href="producer_8c.html#a9ba1d3d3c7c4fefedf72684e9abdc8ee">00035</a> <span class="keywordtype">void</span>* <a class="code" href="producer_8c.html#a9ba1d3d3c7c4fefedf72684e9abdc8ee">ProducerThreadEntryPoint</a>( <span class="keywordtype">void</span>* aParam ) 
<a name="l00036"></a>00036 {
<a name="l00037"></a>00037         <a class="code" href="struct_thread_param.html">ThreadParam</a>* args = (<a class="code" href="struct_thread_param.html">ThreadParam</a>*) aParam;
<a name="l00038"></a>00038         <span class="keywordtype">int</span> yetToProduce = args-&gt;noOfItems;
<a name="l00039"></a>00039         <span class="keywordtype">int</span> produceNow = 0;
<a name="l00040"></a>00040         <span class="keywordtype">int</span> noOfConsumers = args-&gt;noOfConsumers;
<a name="l00041"></a>00041         <a class="code" href="struct_produced_item.html">ProducedItem</a> item;
<a name="l00042"></a>00042         key_t msgQFd = -1;
<a name="l00043"></a>00043         <span class="keywordtype">int</span> ret = 0;
<a name="l00044"></a>00044         <span class="comment">/* Construct the message to be send thru msg q */</span>
<a name="l00045"></a>00045         <span class="keyword">struct </span>msgbuf* sendMsg = (<span class="keyword">struct </span>msgbuf*)malloc(KMAXSENDMSG);
<a name="l00046"></a>00046         sendMsg-&gt;mtype = 1;
<a name="l00047"></a>00047         
<a name="l00048"></a>00048         <span class="comment">/* Get the Handler to Observer Msg Q */</span>
<a name="l00049"></a>00049         msgQFd = msgget(KMSGQKEY, IPC_CREAT);
<a name="l00050"></a>00050 
<a name="l00051"></a>00051         item.itemNum = 1;
<a name="l00052"></a>00052 
<a name="l00053"></a>00053         <span class="keywordflow">while</span> (yetToProduce != 0) 
<a name="l00054"></a>00054         {
<a name="l00055"></a>00055                 produceNow = MIN(yetToProduce, noOfConsumers);
<a name="l00056"></a>00056                 yetToProduce -= produceNow;
<a name="l00057"></a>00057 
<a name="l00058"></a>00058                 <span class="comment">/* Acquire the Lock so that Producer can Produce &quot;produceNow&quot; items once. */</span>
<a name="l00059"></a>00059                 sem_wait(&amp;args-&gt;itemLock);
<a name="l00060"></a>00060                 <span class="keywordflow">for</span>(;produceNow != 0; produceNow--) 
<a name="l00061"></a>00061                 {
<a name="l00062"></a>00062                         sprintf(item.itemName, <span class="stringliteral">&quot;Item-Number: #%d&quot;</span>, item.itemNum);
<a name="l00063"></a>00063                         <span class="comment">/* Push this item on to Stack so that Consumetrs can take it.*/</span>
<a name="l00064"></a>00064                         <a class="code" href="stack_8cpp.html#a90e98e7c8440177d0df072df573cc038">PushOntoStack</a>(&amp;item);
<a name="l00065"></a>00065                         item.itemNum++;
<a name="l00066"></a>00066                         <span class="comment">/* Inform the Observer about Item Production */</span>
<a name="l00067"></a>00067                         sprintf(sendMsg-&gt;mtext, <span class="stringliteral">&quot; Producer Produced %s\0&quot;</span>, item.itemName);
<a name="l00068"></a>00068                         ret = msgsnd(msgQFd, sendMsg, strlen(sendMsg-&gt;mtext)+4, 0);
<a name="l00069"></a>00069                 }
<a name="l00070"></a>00070                 <span class="comment">/* Release the Lock so that Consumers can consume produced items. */</span>
<a name="l00071"></a>00071                 sem_post(&amp;args-&gt;itemLock);
<a name="l00072"></a>00072         }
<a name="l00073"></a>00073 
<a name="l00074"></a>00074         free( sendMsg );
<a name="l00075"></a>00075         <span class="keywordflow">return</span> (<span class="keywordtype">int</span>*)0;
<a name="l00076"></a>00076 }
<a name="l00077"></a>00077 
<a name="l00078"></a>00078 <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>