⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bbpc_sem.c

📁 澳洲人写的Cortex,包括uC_IP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>/home/asysweb/public_html/cortex/examples/exmpl1/src/bbpc_sem.c</TITLE></HEAD><BODY><pre><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*     Copyright (c) 1997-1999 Australian Real Time Embedded Systems     */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/* PROPRIETARY RIGHTS of Australian Real Time Embedded Systems           */</font><font color="#6920ac">/* are involved in the subject matter of this material. All reproduction,*/</font><font color="#6920ac">/* manufacturing, use, and sales rights pertaining to this subject matter*/</font><font color="#6920ac">/* are governed by the license agreement. The recipient of this software */</font><font color="#6920ac">/* implicitly accepts the terms of the license.                          */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/* * The Bounded Buffer Consumers and Producers problem  *         (semaphore implementation) * -------------------------------------------------- * * This program solves the bounded buffer producers and consumers  * problem with semaphores. This implementation is direct translation * into CORTEX environment of the classical semaphore solution presented  * in operation systems textbooks.  * * In this program, the semaphore Mutex is used to synchronize access to the  * shared variable Count in critical section of code where Count is updated. * This semaphore is used for mutual exclusion synchronisation. The other two  * semaphores, Elements and Spaces, are used to synchronize the producer and * consumer process so the producer doesn't try to put  something into a full  * buffer and the consumer doesn't try to take something from an empty  * buffer. These semaphores are used for conditional synchronisation, also * called event synchronisation. */</font><b><font color='DarkGreen'>#include</font></b> <a href="bbpc_sem.h.FIND-INC"><font color="blue">"bbpc_sem.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="plt_defs.h.FIND-INC"><font color="blue">"plt_defs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="prn_defs.h.FIND-INC"><font color="blue">"prn_defs.h"</font></a><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="crtx_Main">crtx_Main</a></font></b>(    <i>crtx_Int_t</i>  ArgC_a,     <i>crtx_Void_t</i> *pArgV_a,     <i>crtx_Void_t</i> *pEnvV_a){</font><font color="#6920ac">/********************* *  LOCAL VARIABLES  * *********************/</font>    <i>crtx_Int_t</i>      ArgC;    <i>crtx_Void_t</i>     *pArgV;    <i>crtx_Void_t</i>     *pEnvV;    <i>sema_Attr_t</i>     sema_Attr;          <font color="#6920ac">/* semaphore attributes */</font>    <i>task_Attr_t</i>     task_Attr;          <font color="#6920ac">/* task attributes */</font>                                        <font color="#6920ac">/* task name buffer */</font>    <i>crtx_Char_t</i>     TaskName[<a href="TASK_MAX_NAME_LEN.FIND-DEF">TASK_MAX_NAME_LEN</a>];    <i>crtx_Int_t</i>      i;    <i>crtx_Status_t</i>   Status;<font color="#6920ac">/********************* *  PROCEDURE LOGIC  * *********************/</font>    <a href="pltf_Init.FIND-FUNC">pltf_Init</a>(ArgC_a, pArgV_a, pEnvV_a);    ArgC  = ArgC_a;     <font color="#6920ac">/* to avoid compiler's warning */</font>    pArgV = pArgV_a;    <font color="#6920ac">/* ... */</font>    pEnvV = pEnvV_a;    <font color="#6920ac">/* ... */</font>    <font color="#6920ac">/* startup message */</font>    <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"\n%s (Version: %s)\n\n"</font>, <a href="syst_Copyright.FIND-FUNC">syst_Copyright</a>(), <a href="syst_VersionStr.FIND-FUNC">syst_VersionStr</a>());    <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%s[%d]:Example: The Bounded Buffer Producers and Consumers "</font>           <font color="DarkGreen">"(semaphore implementation) (%d ticks/sec)  ...\n"</font>,           <a href="task_GetName.FIND-FUNC">task_GetName</a>(<a href="task_Self.FIND-FUNC">task_Self</a>()), <a href="task_Self.FIND-FUNC">task_Self</a>(),            <a href="TICK_SYSTEM_TICKS_PER_SEC.FIND-DEF">TICK_SYSTEM_TICKS_PER_SEC</a>);    <font color="#6920ac">/* initialise the counded buffer's variables */</font>    Count   = 0;        <font color="#6920ac">/* buffer is empty */</font>    PutIn   = 0;        <font color="#6920ac">/* where to put */</font>    TakeOut = 0;        <font color="#6920ac">/* where to take */</font>    <font color="#6920ac">/* initialise the buffer access semaphores */</font>    <font color="#6920ac">/* Note: sema_Create() wouldn't fail because preallocated     *       semaphore control blockes are used */</font>    <font color="#6920ac">/* example of correct attribute initialisation */</font>    sema_Attr = sema_AttrDefault_g;    sema_Attr.SuspendPolicy = <a href="THRD_SUSPEND_FIFO.FIND-DEF">THRD_SUSPEND_FIFO</a>;    <font color="#6920ac">/* First In First Out */</font>    <font color="#6920ac">/* mutex here -- binary semaphore */</font>    pMutex = <a href="sema_Create.FIND-FUNC">sema_Create</a>(&amp;Mutex, 1, 1, &amp;sema_Attr);    <font color="#6920ac">/* used to count items in the buffer */</font>    pElements = <a href="sema_Create.FIND-FUNC">sema_Create</a>(&amp;Elements, 0, <a href="BOUNDED_BUFFER_SIZE.FIND-DEF">BOUNDED_BUFFER_SIZE</a>, &amp;sema_Attr);    <font color="#6920ac">/* used to count empty slots in the buffer */</font>    pSpaces = <a href="sema_Create.FIND-FUNC">sema_Create</a>(&amp;Spaces, <a href="BOUNDED_BUFFER_SIZE.FIND-DEF">BOUNDED_BUFFER_SIZE</a>, <a href="BOUNDED_BUFFER_SIZE.FIND-DEF">BOUNDED_BUFFER_SIZE</a>,                           &amp;sema_Attr);    <font color="#6920ac">/* create Producers */</font>    <b>for</b> (i=0; i&lt;<a href="NUM_PRODUCERS.FIND-DEF">NUM_PRODUCERS</a>; i++)    {        <font color="#6920ac">/* make a name for the current producer */</font>        <a href="sprintf.FIND-FUNC">sprintf</a>(TaskName, <font color="DarkGreen">"Prod %d"</font>, i);        task_Attr = task_AttrDefault_g;        task_Attr.Priority   = <a href="PRIORITY.FIND-DEF">PRIORITY</a>;<font color="#6920ac">#if 0        task_Attr.SchdPolicy = task_SchdTimeSharing;        task_Attr.TimeSlice  = TIMESLICE;#else        task_Attr.SchdPolicy = thrd_SchdRoundRobin;#endif</font>        task_Attr.State      = <a href="TASK_READY.FIND-DEF">TASK_READY</a>;        task_Attr.pName      = TaskName;        Producers[i] = <a href="task_Create1.FIND-FUNC">task_Create1</a>((<i>task_Entry1_t</i>)Producer, &amp;task_Attr,                                     &amp;Status, 0, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>);        <b>if</b> (Status != <a href="TASK_SUCCESS.FIND-DEF">TASK_SUCCESS</a>)        {            <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%s[%d]:ERROR: %s: %d: task_Create: P%d, E:%ld\n"</font>,                   <a href="task_GetName.FIND-FUNC">task_GetName</a>(<a href="task_Self.FIND-FUNC">task_Self</a>()), <a href="task_Self.FIND-FUNC">task_Self</a>(), <a href="__FILE__.FIND-DEF">__FILE__</a>, <a href="__LINE__.FIND-DEF">__LINE__</a>,                   i, Status);            <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> Error_Exc;        }    }    <font color="#6920ac">/* create Consumers */</font>    <b>for</b> (i=0; i&lt;<a href="NUM_CONSUMERS.FIND-DEF">NUM_CONSUMERS</a>; i++)    {        <font color="#6920ac">/* make a name for the current consumer */</font>        <a href="sprintf.FIND-FUNC">sprintf</a>(TaskName, <font color="DarkGreen">"Cons %d"</font>, i);        task_Attr = task_AttrDefault_g;        task_Attr.Priority   = <a href="PRIORITY.FIND-DEF">PRIORITY</a>;<font color="#6920ac">#if 0        task_Attr.SchdPolicy = task_SchdTimeSharing;        task_Attr.TimeSlice  = TIMESLICE;#else        task_Attr.SchdPolicy = thrd_SchdRoundRobin;#endif</font>        task_Attr.State      = <a href="TASK_READY.FIND-DEF">TASK_READY</a>;        task_Attr.pName      = TaskName;        Consumers[i] = <a href="task_Create1.FIND-FUNC">task_Create1</a>((<i>task_Entry1_t</i>)Consumer, &amp;task_Attr,                                     &amp;Status, 0, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>);        <b>if</b> (Status != <a href="TASK_SUCCESS.FIND-DEF">TASK_SUCCESS</a>)        {            <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%s[%d]:ERROR: %s: %d: task_Create: C%d, E:%ld\n"</font>,                   <a href="task_GetName.FIND-FUNC">task_GetName</a>(<a href="task_Self.FIND-FUNC">task_Self</a>()), <a href="task_Self.FIND-FUNC">task_Self</a>(), <a href="__FILE__.FIND-DEF">__FILE__</a>, <a href="__LINE__.FIND-DEF">__LINE__</a>,                   i, Status);            <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> Error_Exc;        }    }    <a href="task_Sleep.FIND-FUNC">task_Sleep</a>(<a href="TICK_SECS_TO_TICKS.FIND-DEF">TICK_SECS_TO_TICKS</a>(<a href="RUNTIME.FIND-DEF">RUNTIME</a>));    <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%s[%d]: must stop now\n"</font>,           <a href="task_GetName.FIND-FUNC">task_GetName</a>(<a href="task_Self.FIND-FUNC">task_Self</a>()), <a href="task_Self.FIND-FUNC">task_Self</a>());    <font color="#6920ac">/* delete Consumers */</font>    <b>for</b> (i=0; i&lt;<a href="NUM_CONSUMERS.FIND-DEF">NUM_CONSUMERS</a>; i++)    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -