-
Notifications
You must be signed in to change notification settings - Fork 0
/
os345p3.c
374 lines (327 loc) · 8.88 KB
/
os345p3.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// os345p3.c - Jurassic Park
// ***********************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <time.h>
#include <assert.h>
#include "os345.h"
#include "os345park.h"
// ***********************************************************************
// project 3 variables
// Jurassic Park
extern JPARK myPark;
extern Semaphore* parkMutex; // protect park access
extern Semaphore* fillSeat[NUM_CARS]; // (signal) seat ready to fill
extern Semaphore* seatFilled[NUM_CARS]; // (wait) passenger seated
extern Semaphore* rideOver[NUM_CARS]; // (signal) ride over
extern Semaphore* tickets;
extern Semaphore* cars;
extern Semaphore* drivers;
extern Semaphore* seats;
extern Semaphore* visitors;
extern Semaphore* inPark;
extern Semaphore* waitPark;
extern Semaphore* inMuseum;
extern Semaphore* inGiftshop;
// ***********************************************************************
// project 3 functions and tasks
void CL3_project3(int, char**);
void CL3_dc(int, char**);
// ***********************************************************************
// ***********************************************************************
// project3 command
int P3_project3(int argc, char* argv[])
{
char buf[32];
char* newArgv[2];
// start park
sprintf(buf, "jurassicPark");
newArgv[0] = buf;
createTask( buf, // task name
jurassicTask, // task
MED_PRIORITY, // task priority
1, // task count
newArgv); // task argument
// wait for park to get initialized...
while (!parkMutex) SWAP;
printf("\nStart Jurassic Park...");
//?? create car, driver, and visitor tasks here
//myPark.numInCarLine = myPark.numInPark = 9;
for (int i = 0; i < NUM_CARS; i++)
{
char str[15]; SWAP;
sprintf(str, "%d", i); SWAP;
newArgv[0] = str; SWAP;
createTask("Car", carTask, MED_PRIORITY, 1, newArgv); SWAP;
}
for (int i = 0; i < NUM_VISITORS; i++)
{
char str[15]; SWAP;
sprintf(str, "%d", i); SWAP;
newArgv[0] = str; SWAP;
createTask("Visitor", visitorTask, MED_PRIORITY, 1, newArgv); SWAP;
}
for (int i = 0; i < NUM_DRIVERS; i++)
{
char str[15]; SWAP;
sprintf(str, "%d", i); SWAP;
newArgv[0] = str; SWAP;
createTask("Driver", driverTask, MED_PRIORITY, 1, newArgv); SWAP;
}
createTask("dinoTask", dinoTask, MED_PRIORITY, 1, newArgv); SWAP;
return 0;
} // end project3
// ***********************************************************************
// ***********************************************************************
// delta clock command
int P3_dc(int argc, char* argv[])
{
printf("\nDelta Clock");
//print delta clock contents
printf("\nTo Be Implemented!");
return 0;
} // end CL3_dc
/*
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// delta clock command
int P3_dc(int argc, char* argv[])
{
printf("\nDelta Clock");
// ?? Implement a routine to display the current delta clock contents
//printf("\nTo Be Implemented!");
int i;
for (i=0; i<numDeltaClock; i++)
{
printf("\n%4d%4d %-20s", i, deltaClock[i].time, deltaClock[i].sem->name);
}
return 0;
} // end CL3_dc
// ***********************************************************************
// display all pending events in the delta clock list
void printDeltaClock(void)
{
int i;
for (i=0; i<numDeltaClock; i++)
{
printf("\n%4d%4d %-20s", i, deltaClock[i].time, deltaClock[i].sem->name);
}
return;
}
// ***********************************************************************
// test delta clock
int P3_tdc(int argc, char* argv[])
{
createTask( "DC Test", // task name
dcMonitorTask, // task
10, // task priority
argc, // task arguments
argv);
timeTaskID = createTask( "Time", // task name
timeTask, // task
10, // task priority
argc, // task arguments
argv);
return 0;
} // end P3_tdc
// ***********************************************************************
// monitor the delta clock task
int dcMonitorTask(int argc, char* argv[])
{
int i, flg;
char buf[32];
// create some test times for event[0-9]
int ttime[10] = {
90, 300, 50, 170, 340, 300, 50, 300, 40, 110 };
for (i=0; i<10; i++)
{
sprintf(buf, "event[%d]", i);
event[i] = createSemaphore(buf, BINARY, 0);
insertDeltaClock(ttime[i], event[i]);
}
printDeltaClock();
while (numDeltaClock > 0)
{
SEM_WAIT(dcChange)
flg = 0;
for (i=0; i<10; i++)
{
if (event[i]->state ==1) {
printf("\n event[%d] signaled", i);
event[i]->state = 0;
flg = 1;
}
}
if (flg) printDeltaClock();
}
printf("\nNo more events in Delta Clock");
// kill dcMonitorTask
tcb[timeTaskID].state = S_EXIT;
return 0;
} // end dcMonitorTask
extern Semaphore* tics1sec;
// ********************************************************************************************
// display time every tics1sec
int timeTask(int argc, char* argv[])
{
char svtime[64]; // ascii current time
while (1)
{
SEM_WAIT(tics1sec)
printf("\nTime = %s", myTime(svtime));
}
return 0;
} // end timeTask
*/
/*struct DeltaC
{
int time;
Semaphore* sem;
struct DeltaC *next;
};
struct DeltaC *head = NULL;
struct DeltaC *back = NULL;
struct DeltaC* create_list(int time, Semaphore* sem)
{
struct DeltaC *ptr = (struct DeltaC*)malloc(sizeof(struct DeltaC)); SWAP;
if (ptr == NULL)
{
printf("\n Node creation failed \n"); SWAP;
return NULL;
}
ptr->time = time; SWAP;
ptr->sem = sem; SWAP;
ptr->next = NULL; SWAP;
head = back = ptr; SWAP;
return ptr;
}
struct DeltaC* add_to_Dclock(int time, Semaphore* sem)
{
if (head == NULL)
{
return (create_list(time, sem)); SWAP;
}
struct DeltaC *ptr = head; SWAP;
struct DeltaC *ptr_add = (struct DeltaC*)malloc(sizeof(struct DeltaC)); SWAP;
if (ptr == NULL)
{
printf("\n Node creation failed \n"); SWAP;
return NULL;
}
if (ptr->time < time)
{
while (ptr->next != NULL)
{
if (ptr->next->time < time)
{
time = time - ptr->time; SWAP;
ptr = ptr->next; SWAP;
}
else
{
ptr_add->time = time; SWAP;
ptr->sem = sem; SWAP;
ptr->next->time -= time; SWAP;
ptr_add->next = ptr->next; SWAP;
ptr->next = ptr_add; SWAP;
return ptr_add;
}
}
ptr_add->time = time - ptr->time; SWAP;
ptr->sem = sem; SWAP;
back->next = ptr_add; SWAP;
back = ptr_add; SWAP;
return ptr_add;
}
else
{
ptr_add->time = time; SWAP;
ptr->sem = sem; SWAP;
head->time -= time; SWAP;
ptr_add->next = head; SWAP;
head = ptr_add; SWAP;
return ptr_add;
}
}
struct DeltaC* search_in_list(int time, struct DeltaC **prev)
{
struct DeltaC *ptr = head;
struct DeltaC *tmp = NULL;
bool found = FALSE;
printf("\n Searching the list for value [%d] \n", time);
while (ptr != NULL)
{
if (ptr->time == time)
{
found = TRUE;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
if (TRUE == found)
{
if (prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
int delete_from_Dclock()
{
int status = -1;
if (head != NULL)
{
while (head->time == 0)
{
struct DeltaC *del = head; SWAP;
head = head->next; SWAP;
SEM_SIGNAL(del->sem); SWAP;
free(del); SWAP;
del = NULL; SWAP;
status = 0; SWAP;
}
head->time--; SWAP;
}
return status;
}
void print_list(void)
{
struct DeltaC *ptr = head; SWAP;
printf("\n -------Printing list Start------- \n"); SWAP;
while (ptr != NULL)
{
printf("\n [%d] %s \n", ptr->time, ptr->sem->name); SWAP;
ptr = ptr->next; SWAP;
}
printf("\n -------Printing list End------- \n"); SWAP;
return;
}*/