barb.c
来自「澳洲人写的Cortex,包括uC_IP协议栈」· C语言 代码 · 共 318 行 · 第 1/2 页
C
318 行
<font color="#6920ac">/* memorise a time to go to sleep */</font> <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&StartWait); <a href="#barb_Sleep">barb_Sleep</a>(); <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&SysTime); <font color="#6920ac">/* accumulate time the barber slept */</font> barb_SleepTime += ((SysTime.tv_sec * 1000 + SysTime.tv_nsec/1000000) - (StartWait.tv_sec * 1000 + StartWait.tv_nsec/1000000)); <font color="#6920ac">/* cutting */</font> SleepTime = <a href="rand.FIND-FUNC">rand</a>()%(<a href="#BARB_CUTTING_CONST">BARB_CUTTING_CONST</a>); <a href="task_Sleep.FIND-FUNC">task_Sleep</a>(SleepTime); <a href="#barb_CutEnd">barb_CutEnd</a>(); } }<font color="#6920ac">/* main function */</font><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> i; <i>task_Attr_t</i> TAttr; <i>tick_Time_t</i> SysTime; <i>crtx_Char_t</i> str[20];<font color="#6920ac">/********************* * PROCEDURE LOGIC * *********************/</font> <font color="#6920ac">/* call the platform init function */</font> <a href="pltf_Init.FIND-FUNC">pltf_Init</a>(ArgC_a, pArgV_a, pEnvV_a); <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">"Sleeping Barber (and %d customers on %d chairs):\n"</font>, <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>, <a href="#BARB_CHAIRS">BARB_CHAIRS</a>); <a href="memset.FIND-FUNC">memset</a>(barb_Info, 0, <i>sizeof</i>(barb_Info)); barb_SleepTime = 0; <font color="#6920ac">/* create semaphores */</font> <font color="#6920ac">/* a semaphore for chairs, with maximum number BARB_CHAIRS */</font> pbarb_SemChairs = <a href="sema_Create.FIND-FUNC">sema_Create</a>(<a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, <a href="#BARB_CHAIRS">BARB_CHAIRS</a>, <a href="#BARB_CHAIRS">BARB_CHAIRS</a>, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>); <b>if</b> (pbarb_SemChairs == <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create semaphore pbarb_SemChairs failed\n"</font>); <a href="exit.FIND-FUNC">exit</a>(0); } <font color="#6920ac">/* a binary semaphore for a barber */</font> pbarb_SemBarber = <a href="sema_Create.FIND-FUNC">sema_Create</a>(<a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, 1, 1, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>); <b>if</b> (pbarb_SemBarber == <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create semaphore pbarb_SemBarber failed\n"</font>); <a href="exit.FIND-FUNC">exit</a>(0); } <font color="#6920ac">/* a binary semaphore a barber is sleeping with initial value 0 */</font> pbarb_SemSleep = <a href="sema_Create.FIND-FUNC">sema_Create</a>(<a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, 0, 1, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>); <b>if</b> (pbarb_SemSleep == <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create semaphore pbarb_SemSleep failed\n"</font>); <a href="exit.FIND-FUNC">exit</a>(0); } <font color="#6920ac">/* a binary semaphore a barber is cutting with initial value 0 */</font> pbarb_Cutting = <a href="sema_Create.FIND-FUNC">sema_Create</a>(<a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, 0, 1, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>); <b>if</b> (pbarb_Cutting == <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create semaphore pbarb_Cutting failed\n"</font>); <a href="exit.FIND-FUNC">exit</a>(0); } <font color="#6920ac">/* create a task ( process) a barber */</font> TAttr = task_AttrDefault_g; <b>if</b> (<a href="task_Create1.FIND-FUNC">task_Create1</a>((<i>task_Entry1_t</i>)barb_Barber, &TAttr, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, 0, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) == <a href="TASK_ILLEGAL_ID.FIND-DEF">TASK_ILLEGAL_ID</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create task for a barber %d failed\n"</font>, i + 1); <a href="exit.FIND-FUNC">exit</a>(0); } <font color="#6920ac">/* create tasks for customers */</font> <b>for</b> (i = 0; i < <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>; i++) { TAttr = task_AttrDefault_g; <b>if</b> (<a href="task_Create1.FIND-FUNC">task_Create1</a>((<i>task_Entry1_t</i>)barb_Customer, &TAttr, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, i, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) == <a href="TASK_ILLEGAL_ID.FIND-DEF">TASK_ILLEGAL_ID</a>) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"Create task for the customer %d failed\n"</font>, i + 1); <a href="exit.FIND-FUNC">exit</a>(0); } } <b>while</b> ( 1 ) { <font color="#6920ac">/* print statistics */</font> <a href="tick_GetSystemTime.FIND-FUNC">tick_GetSystemTime</a>(&SysTime); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"At %02ld:%02ld:%02ld "</font>, SysTime.tv_sec/3600, (SysTime.tv_sec%3600)/60, SysTime.tv_sec%60); <b>for</b> (i = 0; i < <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>; i++) { <a href="sprintf.FIND-FUNC">sprintf</a>(str,<font color="DarkGreen">"cust#%d"</font>, i + 1); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%10s"</font>, str); } <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">" barber sleeps"</font>); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"\n"</font>); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">" %-12s "</font>,<font color="DarkGreen">"Waiting(ms)"</font>); <b>for</b> (i = 0; i < <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>; i++) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%10ld"</font>, barb_Info[ i ].WaitTime); } <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">" (ms)"</font>); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"\n"</font>); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">" %-12s "</font>,<font color="DarkGreen">"Sleeping(ms)"</font>); <b>for</b> (i = 0; i < <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>; i++) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%10ld"</font>, barb_Info[ i ].SleepTime); } <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%12ld"</font>, barb_SleepTime); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"\n"</font>); <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">" %-12s "</font>,<font color="DarkGreen">"Cut(times)"</font>); <b>for</b> (i = 0; i < <a href="#BARB_CUSTOMERS">BARB_CUSTOMERS</a>; i++) { <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"%10ld"</font>, barb_Info[ i ].CutTimes); } <a href="printf.FIND-FUNC">printf</a>(<font color="DarkGreen">"\n"</font>); <a href="task_Sleep.FIND-FUNC">task_Sleep</a>(<a href="#BARB_PRINT_TIME">BARB_PRINT_TIME</a>); }}</pre></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?