-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5app.c
166 lines (141 loc) · 3.67 KB
/
lab5app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
File: lab5app.c
Revision date: 13 November 2009
Description: Application code for EE 425 lab 5 (Semaphores)
*/
#include "clib.h"
#include "yakk.h"
#include "yaku.h"
#define TASK_STACK_SIZE 512 /* stack size in words */
int TaskWStk[TASK_STACK_SIZE]; /* stacks for each task */
int TaskSStk[TASK_STACK_SIZE];
int TaskPStk[TASK_STACK_SIZE];
int TaskStatStk[TASK_STACK_SIZE];
int TaskPRMStk[TASK_STACK_SIZE];
YKSEM *PSemPtr; /* YKSEM must be defined in yakk.h */
YKSEM *SSemPtr;
YKSEM *WSemPtr;
YKSEM *NSemPtr;
void TaskWord(void)
{
while (1)
{
YKSemPend(WSemPtr);
printString("Hey");
YKSemPost(PSemPtr);
YKSemPend(WSemPtr);
printString("it");
YKSemPost(SSemPtr);
YKSemPend(WSemPtr);
printString("works");
YKSemPost(PSemPtr);
}
}
void TaskSpace(void)
{
while (1)
{
YKSemPend(SSemPtr);
printChar(' ');
YKSemPost(WSemPtr);
}
}
void TaskPunc(void)
{
while (1)
{
YKSemPend(PSemPtr);
printChar('"');
YKSemPost(WSemPtr);
YKSemPend(PSemPtr);
printChar(',');
YKSemPost(SSemPtr);
YKSemPend(PSemPtr);
printString("!\"\r\n");
YKSemPost(PSemPtr);
YKDelayTask(6);
}
}
void TaskPrime(void) /* task that actually computes primes */
{
int curval = 1001;
int j,flag,lncnt;
int endval;
while (1)
{
YKSemPend(NSemPtr);
/* compute next range of primes */
lncnt = 0;
endval = curval + 500;
for ( ; curval < endval; curval += 2)
{
flag = 0;
for (j = 3; (j*j) < curval; j += 2)
{
if (curval % j == 0)
{
flag = 1;
break;
}
}
if (!flag)
{
printChar(' ');
printInt(curval);
lncnt++;
if (lncnt > 9)
{
printNewLine();
lncnt = 0;
}
}
}
printNewLine();
}
}
void TaskStat(void) /* a task to track statistics */
{
unsigned max, switchCount, idleCount;
int tmp;
YKDelayTask(1);
printString("Welcome to the YAK kernel\r\n");
printString("Determining CPU capacity\r\n");
YKDelayTask(1);
YKIdleCount = 0;
YKDelayTask(5);
max = YKIdleCount / 25;
YKIdleCount = 0;
YKNewTask(TaskPrime, (void *) &TaskPRMStk[TASK_STACK_SIZE], 32);
YKNewTask(TaskWord, (void *) &TaskWStk[TASK_STACK_SIZE], 10);
YKNewTask(TaskSpace, (void *) &TaskSStk[TASK_STACK_SIZE], 11);
YKNewTask(TaskPunc, (void *) &TaskPStk[TASK_STACK_SIZE], 12);
while (1)
{
YKDelayTask(20);
YKEnterMutex();
switchCount = YKCtxSwCount;
idleCount = YKIdleCount;
YKExitMutex();
printString ("<<<<< Context switches: ");
printInt((int)switchCount);
printString(", CPU usage: ");
tmp = (int) (idleCount/max);
printInt(100-tmp);
printString("% >>>>>\r\n");
YKEnterMutex();
YKCtxSwCount = 0;
YKIdleCount = 0;
YKExitMutex();
}
}
void main(void)
{
YKInitialize();
/* create all semaphores, at least one user task, etc. */
PSemPtr = YKSemCreate(1);
SSemPtr = YKSemCreate(0);
WSemPtr = YKSemCreate(0);
NSemPtr = YKSemCreate(0);
YKNewTask(TaskStat, (void *) &TaskStatStk[TASK_STACK_SIZE], 30);
YKRun();
}