-
Notifications
You must be signed in to change notification settings - Fork 0
/
ferry_module.c
executable file
·514 lines (461 loc) · 11.5 KB
/
ferry_module.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* ---------------------------------------
Greg Lombardo && Josh Rendek
Ferry Module - Project 3
COP4610 - Operating Systems
Fall 2011
--------------------------------------- */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/proc_fs.h>
#include <linux/mutex.h>
MODULE_LICENSE("GPL");
/* Function prototypes */
void init_vars(void);
void ferry_loop(void);
void begin_ferry(void);
void release_passengers(void);
void arrive_north(void);
void arrive_south(void);
void print_proc(void);
void travel(void);
void redeposit_south(void);
void redeposit_north(void);
/* Extern system call stub declarations */
extern long (*STUB_ferry_start)(void);
extern long (*STUB_ferry_request)(int passenger_type, char start_bank);
extern long (*STUB_ferry_stop)(void);
/* PROC declarations */
static struct proc_dir_entry *proc_entry;
static const char *PROC_NAME = "ferry";
int proc_read(char *page, char **pages, off_t offset, int page_size, int *eof, void *data);
/* Global Vars */
struct bank {
int queen;
int nobles;
int peasants;
};
struct ferry {
int queen;
int nobles;
int peasants;
int compartments;
int curr_bank; //0 = north, 1 = south, 2 = in transit
};
struct bank north_bank;
struct bank south_bank;
struct ferry curr_ferry;
int curr_ferry_passengers;
int total_ferried;
int stop_ferry_flag;
int queen_north_flag;
int queen_south_flag;
int ferry_active = 0; //0 = not active, 1 = active
struct task_struct *ferry_thread;
struct mutex lock;
long my_ferry_start()
{
//Handle if ferry gets a start request after already starting
if(ferry_active == 1)
{
printk("%s: Ferry has already been started\n", __FUNCTION__);
return 1;
}
printk("%s: Ferry started!\n", __FUNCTION__);
//Initialize mutex
mutex_init(&lock);
//Initialize all variables
init_vars();
//Initialize proc entry
proc_entry = create_proc_entry(PROC_NAME, 0444, NULL);
//begin ferry thread
ferry_thread = kthread_run(begin_ferry, NULL, "ferry_thread");
return 0;
}
void begin_ferry()
{
//Continue looping ferry until stopped
while(stop_ferry_flag != 1)
ferry_loop();
printk("%s: **THREAD STOPPING**\n", __FUNCTION__);
}
int proc_read(char *page, char **pages, off_t offset, int page_size, int *eof, void *data)
{
//Writing to proc file
int ret, pass_aboard;
char * bank;
if(curr_ferry.curr_bank == 0)
bank = "North";
else if(curr_ferry.curr_bank == 1)
bank = "South";
else if(curr_ferry.curr_bank == 2)
bank = "In Transit";
else
bank = "Unknown";
pass_aboard = curr_ferry.queen + curr_ferry.nobles + curr_ferry.peasants;
ret = sprintf(page,
"Current bank: %s\nPassengers aboard: %d\nCurrent passengers:\nQueen: %d\nNobles: %d\nPeasants: %d\nNorth bank:\nQueen: %d\nNobles: %d\nPeasants: %d\nSouth bank:\nQueen: %d\nNobles: %d\nPeasants: %d\nTotal ferried: %d\n",
bank, pass_aboard, curr_ferry.queen, curr_ferry.nobles, curr_ferry.peasants,
north_bank.queen, north_bank.nobles, north_bank.peasants, south_bank.queen,
south_bank.nobles, south_bank.peasants, total_ferried);
return ret;
}
void release_passengers()
{
curr_ferry.queen = 0;
curr_ferry.nobles = 0;
curr_ferry.peasants = 0;
curr_ferry.compartments = 0;
curr_ferry_passengers = 0;
}
void arrive_north()
{
//lock
mutex_lock_interruptible(&lock);
printk("%s: **Locked**\n", __FUNCTION__);
//arrive at north_bank
curr_ferry.curr_bank = 0;
printk("%s: Ferry arrived at north bank\n", __FUNCTION__);
print_proc();
release_passengers();
printk("%s: Passengers released, unlocking and waiting\n", __FUNCTION__);
//release lock
mutex_unlock(&lock);
//Wait 2 seconds for any extra passengers
msleep(2000);
//Lock again and begin loading passengers
mutex_lock_interruptible(&lock);
printk("%s: Waiting over, locked and loading passengers\n", __FUNCTION__);
//load passengers
if(north_bank.queen == 1)
{
printk("%s: Picking up north queen\n", __FUNCTION__);
curr_ferry.queen++;
north_bank.queen--;
curr_ferry.compartments+=5;
curr_ferry_passengers++;
total_ferried++;
}
if(north_bank.nobles > 0)
{
printk("%s: Picking up north nobles: %d\n", __FUNCTION__, north_bank.nobles);
while((curr_ferry.compartments + 3) < 15 && north_bank.nobles > 0)
{
curr_ferry.nobles++;
north_bank.nobles--;
curr_ferry.compartments+=3;
curr_ferry_passengers++;
total_ferried++;
}
}
if(north_bank.peasants > 0)
{
printk("%s: Picking up north peasants: %d\n", __FUNCTION__, north_bank.peasants);
while(curr_ferry.compartments < 15 && north_bank.peasants > 0)
{
curr_ferry.peasants++;
north_bank.peasants--;
curr_ferry.compartments++;
curr_ferry_passengers++;
total_ferried++;
}
}
//releaseLock()
mutex_unlock(&lock);
printk("%s: **Unlocked**\n", __FUNCTION__);
}
void arrive_south()
{
//lock
mutex_lock_interruptible(&lock);
printk("%s: **Locked**\n", __FUNCTION__);
//arrive at south_bank
printk("%s: Arrived at south bank\n", __FUNCTION__);
curr_ferry.curr_bank = 1;
print_proc();
release_passengers();
printk("%s: Passengers released, unlocking and waiting\n", __FUNCTION__);
//release lock
mutex_unlock(&lock);
//Wait 2 seconds for any extra passengers
msleep(2000);
//Lock again and begin loading passengers
mutex_lock_interruptible(&lock);
printk("%s: Waiting over, locked and loading passengers\n", __FUNCTION__);
//load passengers
if(south_bank.queen == 1)
{
printk("%s: Picking up south queen\n", __FUNCTION__);
curr_ferry.queen++;
south_bank.queen--;
curr_ferry.compartments+=5;
curr_ferry_passengers++;
total_ferried++;
}
if(south_bank.nobles > 0)
{
printk("%s: Picking up south nobles: %d\n", __FUNCTION__, south_bank.nobles);
while((curr_ferry.compartments + 3) <= 15 && south_bank.nobles > 0)
{
curr_ferry.nobles++;
south_bank.nobles--;
curr_ferry.compartments+=3;
curr_ferry_passengers++;
total_ferried++;
}
}
if(south_bank.peasants > 0)
{
printk("%s: Picking up south peasants: %d\n", __FUNCTION__, south_bank.peasants);
while(curr_ferry.compartments < 15 && south_bank.peasants > 0)
{
curr_ferry.peasants++;
south_bank.peasants--;
curr_ferry.compartments++;
curr_ferry_passengers++;
total_ferried++;
}
}
//release lock
mutex_unlock(&lock);
printk("%s: **Unlocked**\n", __FUNCTION__);
}
void travel()
{
printk("%s: Travelling: Watching for arriving queens\n", __FUNCTION__);
//WATCHING FOR QUEEN ARRIVAL
int n;
for(n = 10; n > 0; n--)
{
msleep(200);
if(queen_north_flag==1)
{
printk("%s: QUEEN NORTH FLAG; Going to north bank\n", __FUNCTION__);
break;
//shit hit the fan!
}
else if(queen_south_flag==1)
{
printk("%s: QUEEN SOUTH FLAG; Going to south bank\n", __FUNCTION__);
break;
//shit also hit the fan!
}
}
}
void redeposit_north()
{
if(curr_ferry.queen == 1)
{
north_bank.queen++;
curr_ferry.queen--;
}
if(curr_ferry.nobles > 0)
{
while(curr_ferry.nobles > 0)
{
north_bank.nobles++;
curr_ferry.nobles--;
}
}
if(curr_ferry.peasants > 0)
{
while(curr_ferry.peasants > 0)
{
north_bank.peasants++;
curr_ferry.peasants--;
}
}
printk("%s: North bank passengers redeposited\n", __FUNCTION__);
}
void redeposit_south()
{
if(curr_ferry.queen == 1)
{
south_bank.queen++;
curr_ferry.queen--;
}
if(curr_ferry.nobles > 0)
{
while(curr_ferry.nobles > 0)
{
south_bank.nobles++;
curr_ferry.nobles--;
}
}
if(curr_ferry.peasants > 0)
{
while(curr_ferry.peasants > 0)
{
south_bank.peasants++;
curr_ferry.peasants--;
}
}
printk("%s: South bank passengers redeposited\n", __FUNCTION__);
}
void ferry_loop()
{
/*
Start, check if the south flag is checked, if so
we want to skip doing north and just go straight to south.
Once we get to south we want to check if the flag was set
and if so we want to redeposit passengers before we pick up again.
The same goes for the second loop, we check to see if the north flag
was checked, if so we want to skip the south and go straight back
to north and pick up the queen there.
If the flag is set and we return to a bank that we were travelling away
from, we want to re-deposit the passengers currently on the ferry back
onto the shore, then reload them in the correct order (queen first)
*/
if(queen_south_flag!=1)
{
if(queen_north_flag==1)
{
queen_north_flag=0;
//redeposit passengers
redeposit_north();
}
arrive_north();
//Set bank to transit
curr_ferry.curr_bank = 2;
print_proc();
travel();
//We reset the south flag as to not deposit passengers that wanted
//To go to south in the first place, then they get mixed up with north
//Passengers and go back to where they came from
queen_south_flag=0;
}
//if north has queen waiting skip south and go north
if(queen_north_flag!=1)
{
//if south has queen we are already here so just reset flag
if(queen_south_flag==1)
{
queen_south_flag=0;
//reposit passengers
redeposit_south();
}
arrive_south();
//Set bank to transit
curr_ferry.curr_bank = 2;
print_proc();
travel();
queen_north_flag=0;
}
}
void print_proc()
{
//Write to proc file
if(proc_entry != NULL)
proc_entry->read_proc = proc_read;
}
void init_vars()
{
//Initialize all variables
north_bank.queen = 0;
north_bank.nobles = 0;
north_bank.peasants = 0;
south_bank.queen = 0;
south_bank.nobles = 0;
south_bank.peasants = 0;
curr_ferry.queen = 0;
curr_ferry.nobles = 0;
curr_ferry.peasants = 0;
curr_ferry.curr_bank = 0; //starts at north
curr_ferry.compartments = 0;
curr_ferry_passengers = 0;
total_ferried = 0;
stop_ferry_flag = 0; //0 = keep going, 1 = stop
queen_north_flag = 0;
queen_south_flag = 0;
ferry_active = 1; //setting ferry to active
}
long my_ferry_request(int passenger_type, char start_bank)
{
printk("%s: Ferry request %i, %c \n", __FUNCTION__, passenger_type, start_bank);
//lock
mutex_lock_interruptible(&lock);
if(passenger_type == 0)
{
if(start_bank == 'n')
{
if(north_bank.queen == 1)
{
printk("%s: Too many north queens\n", __FUNCTION__);
mutex_unlock(&lock);
return 1;
}
else
{
north_bank.queen++;
//We don't want to flag the boat to return to a queen
//if the boat already has a queen on board
//because then the bank would have two queens
if(curr_ferry.queen == 0)
queen_north_flag=1;
}
}
else if(start_bank == 's')
{
if(south_bank.queen == 1)
{
printk("%s: Too many south queens\n", __FUNCTION__);
mutex_unlock(&lock);
return 1;
}
else
{
south_bank.queen++;
if(curr_ferry.queen == 0)
queen_south_flag=1;
}
}
}
else if(passenger_type == 1)
{
if(start_bank == 'n')
north_bank.nobles++;
else if(start_bank == 's')
south_bank.nobles++;
}
else if(passenger_type == 2)
{
if(start_bank == 'n')
north_bank.peasants++;
else if(start_bank == 's')
south_bank.peasants++;
}
else
{
mutex_unlock(&lock);
return 1;
}
//release lock
mutex_unlock(&lock);
return 0;
}
long my_ferry_stop(void)
{
long ret = 0;
printk("%s: Ferry stop \n", __FUNCTION__);
if(stop_ferry_flag == 1)
ret = 1; //Already stopping
else
stop_ferry_flag = 1; //ALL STOP!
remove_proc_entry(PROC_NAME, 0);
ferry_active = 0;
return ret;
}
my_module_init() {
STUB_ferry_start=&(my_ferry_start);
STUB_ferry_request=&(my_ferry_request);
STUB_ferry_stop=&(my_ferry_stop);
return 0;
}
my_module_exit() {
STUB_ferry_start=NULL;
STUB_ferry_request=NULL;
STUB_ferry_stop=NULL;
}
module_init(my_module_init);
module_exit(my_module_exit);