barb.c

来自「澳洲人写的Cortex,包括uC_IP协议栈」· C语言 代码 · 共 318 行 · 第 1/2 页

C
318
字号
<HTML><HEAD><TITLE>/home/asysweb/public_html/cortex/examples/exmpl7/src/barb.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><b><font color='DarkGreen'>#include</font></b> <a href="cortex.h.FIND-INC"><font color="blue">"cortex.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><b><font color='DarkGreen'>#include</font></b> <a href="stdlib.h.FIND-INC"><font color="blue">"stdlib.h"</font></a><font color="#6920ac">/* the number of customers */</font><b><font color='DarkGreen'>#ifndef</font></b><font color="maroon"> BARB_CUSTOMERS</font><b><font color='DarkGreen'># define</font></b> <font color="maroon"><a name="BARB_CUSTOMERS">BARB_CUSTOMERS</a></font> 8<b><font color='DarkGreen'>#endif</font></b><font color="maroon"></font><font color="#6920ac">/* the number of chairs */</font><b><font color='DarkGreen'>#ifndef</font></b><font color="maroon"> BARB_CHAIRS</font><b><font color='DarkGreen'># define</font></b> <font color="maroon"><a name="BARB_CHAIRS">BARB_CHAIRS</a></font>    3<b><font color='DarkGreen'>#endif</font></b><font color="maroon"></font><font color="#6920ac">/* the number of customers must not be less than 1 */</font><b><font color='DarkGreen'>#if</font></b><font color="maroon"> (BARB_CUSTOMERS &lt; 1)</font><b><font color='DarkGreen'># error</font></b><font color="maroon"> Wrong number of customers</font><b><font color='DarkGreen'># undef</font></b><font color="maroon"> BARB_CUSTOMERS</font><b><font color='DarkGreen'># define</font></b> <font color="maroon"><a name="BARB_CUSTOMERS">BARB_CUSTOMERS</a></font> 1<b><font color='DarkGreen'>#endif</font></b><font color="maroon"></font><font color="#6920ac">/* the number of chairs must not be less than 1 */</font><b><font color='DarkGreen'>#if</font></b><font color="maroon"> (BARB_CHAIRS &lt; 1)</font><b><font color='DarkGreen'># error</font></b><font color="maroon"> Wrong number of customers</font><b><font color='DarkGreen'># undef</font></b><font color="maroon"> BARB_CHAIRS</font><b><font color='DarkGreen'># define</font></b> <font color="maroon"><a name="BARB_CHAIRS">BARB_CHAIRS</a></font> 1<b><font color='DarkGreen'>#endif</font></b><font color="maroon"></font><font color="#6920ac">/* time to print trace */</font><b><font color='DarkGreen'>#define</font></b> <font color="maroon"><a name="BARB_PRINT_TIME">BARB_PRINT_TIME</a></font>         TICK_SECS_TO_TICKS(5)<font color="#6920ac">/* sleep constant for customers */</font><b><font color='DarkGreen'>#define</font></b> <font color="maroon"><a name="BARB_CUST_SLEEP_CONST">BARB_CUST_SLEEP_CONST</a></font>   TICK_MS_TO_TICKS(1200)<font color="#6920ac">/* cutting constant */</font><b><font color='DarkGreen'>#define</font></b> <font color="maroon"><a name="BARB_CUTTING_CONST">BARB_CUTTING_CONST</a></font>      TICK_MS_TO_TICKS(100)<font color="#6920ac">/* a structure to collect data about customers */</font><i><a name="barb_Info_t">typedef</a></i> <i>struct</i>{    <i>crtx_Int32_t</i>    WaitTime;    <i>crtx_Int32_t</i>    SleepTime;    <i>crtx_Int32_t</i>    CutTimes;} <i><font color='DarkBlue'>barb_Info_t</font></i>;<i>barb_Info_t</i> barb_Info[<a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>];<font color="#6920ac">/* barber's sleep time */</font><i>crtx_Int32_t</i>        barb_SleepTime;<font color="#6920ac">/* the semaphore to take a chair by customers */</font><i>sema_Semaphore_t</i>    *pbarb_SemChairs;<font color="#6920ac">/* the semaphore to teke a barber by 1 customer */</font><i>sema_Semaphore_t</i>    *pbarb_SemBarber;<font color="#6920ac">/* the barber's sleeping semaphore */</font><i>sema_Semaphore_t</i>    *pbarb_SemSleep;<font color="#6920ac">/* the barber's cutting semaphore */</font><i>sema_Semaphore_t</i>    *pbarb_Cutting;<font color="#6920ac">/* wake the barber */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_Wake">barb_Wake</a></font></b>(<i>crtx_Void_t</i>){</font>    <a href="sema_Post.FIND-FUNC">sema_Post</a>(pbarb_SemSleep);}<font color="#6920ac">/* the barber goes to sleep */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_Sleep">barb_Sleep</a></font></b>(<i>crtx_Void_t</i>){</font>    <a href="sema_Wait.FIND-FUNC">sema_Wait</a>(pbarb_SemSleep);}<font color="#6920ac">/* wait for the end of cutting */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_WaitCutEnd">barb_WaitCutEnd</a></font></b>(<i>crtx_Void_t</i>){</font>    <a href="sema_Wait.FIND-FUNC">sema_Wait</a>(pbarb_Cutting);}<font color="#6920ac">/* the barber finished cutting */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_CutEnd">barb_CutEnd</a></font></b>(<i>crtx_Void_t</i>){</font>    <a href="sema_Post.FIND-FUNC">sema_Post</a>(pbarb_Cutting);}<font color="#6920ac">/* a process/task customer */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_Customer">barb_Customer</a></font></b>(<i>int</i> CustNum_a){</font>    <i>crtx_Int32_t</i>    SleepTime;    <i>tick_Time_t</i>     StartWait;    <i>tick_Time_t</i>     SysTime;    <b>while</b> (1)    {        <font color="#6920ac">/* memorise time before go to sleep */</font>        SleepTime = <a href="rand.FIND-FUNC">rand</a>()%(<a href="#BARB_CUST_SLEEP_CONST">BARB_CUST_SLEEP_CONST</a>);                <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&amp;StartWait);                <font color="#6920ac">/* a customer is sleeping */</font>        <a href="task_Sleep.FIND-FUNC">task_Sleep</a>(SleepTime);                <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&amp;SysTime);                <font color="#6920ac">/* accumulate a time a customer slept */</font>        barb_Info[CustNum_a].SleepTime += ((SysTime.tv_sec * 1000 +                                            SysTime.tv_nsec/1000000) -                                           (StartWait.tv_sec * 1000 +                                            StartWait.tv_nsec/1000000));        <font color="#6920ac">/* memorise a start time waiting for the service */</font>        StartWait = SysTime;        <font color="#6920ac">/* take a chair */</font>        <a href="sema_Wait.FIND-FUNC">sema_Wait</a>(pbarb_SemChairs);        <font color="#6920ac">/* take the barber */</font>        <a href="sema_Wait.FIND-FUNC">sema_Wait</a>(pbarb_SemBarber);                <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&amp;SysTime);                <font color="#6920ac">/* accumulate a time of waiting for the service */</font>        barb_Info[CustNum_a].WaitTime += ((SysTime.tv_sec * 1000 +                                           SysTime.tv_nsec/1000000) -                                          (StartWait.tv_sec * 1000 +                                           StartWait.tv_nsec/1000000));        <font color="#6920ac">/* wake the barber to cut */</font>        <a href="#barb_Wake">barb_Wake</a>();                <font color="#6920ac">/* cutting */</font>                <a href="#barb_WaitCutEnd">barb_WaitCutEnd</a>();        <font color="#6920ac">/* release the barber */</font>        <a href="sema_Post.FIND-FUNC">sema_Post</a>(pbarb_SemBarber);        <font color="#6920ac">/* realease a chair */</font>        <a href="sema_Post.FIND-FUNC">sema_Post</a>(pbarb_SemChairs);                barb_Info[CustNum_a].CutTimes++;    }}<font color="#6920ac">/* process/task the barber */</font><font size="+1"><i>crtx_Void_t</i> <b><font color="azure1"><a name="barb_Barber">barb_Barber</a></font></b>(){</font>    <i>crtx_Int32_t</i>    SleepTime;    <i>tick_Time_t</i>     StartWait;    <i>tick_Time_t</i>     SysTime;    <b>while</b> (1)    {

⌨️ 快捷键说明

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