2 * Sample program for Hyper Operating System V4 Advance
5 * @brief %jp{サンプルプログラム}%en{Sample program}
7 * Copyright (C) 1998-2009 by Project HOS
8 * http://sourceforge.jp/projects/hos/
15 #include "kernel_id.h"
19 #define LEFT(num) ((num) <= 1 ? 5 : (num) - 1)
20 #define RIGHT(num) ((num) >= 5 ? 1 : (num) + 1)
24 void Sample_Initialize(VP_INT exinf)
30 act_tsk(TSKID_SAMPLE1);
31 act_tsk(TSKID_SAMPLE2);
32 act_tsk(TSKID_SAMPLE3);
33 act_tsk(TSKID_SAMPLE4);
34 act_tsk(TSKID_SAMPLE5);
39 void Sample_RandWait(void)
41 static unsigned long seed = 12345;
45 seed = seed * 22695477UL + 1;
49 dly_tsk((r % 1000) + 100);
54 void Sample_PrintSatet(int num, const char *text)
61 Uart_PutChar('0' + num);
65 for ( i = 0; text[i] != '\0'; i++ )
67 Uart_PutChar(text[i]);
78 void Sample_Task(VP_INT exinf)
84 /* %jp{いわゆる哲学者の食事の問題} */
88 Sample_PrintSatet(num, "thinking");
91 /* %jp{左右のフォークを取るまでループ} */
96 if ( pol_sem(RIGHT(num)) == E_OK )
98 break; /* %jp{両方取れた} */
100 sig_sem(LEFT(num)); /* %jp{取れなければ離す} */
103 Sample_PrintSatet(num, "hungry");
108 if ( pol_sem(LEFT(num)) == E_OK )
110 break; /* %jp{両方取れた} */
112 sig_sem(RIGHT(num)); /* %jp{取れなければ離す} */
115 Sample_PrintSatet(num, "hungry");
120 Sample_PrintSatet(num, "eating");