/
lib-sfs.c
628 lines (522 loc) · 15.3 KB
/
lib-sfs.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include "lib-sfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#define SHARED_MEM_SIZE 32768
int segment_id;
memory_layout *memory;
char *shared_memory;
// Private methods:
/**
* Get the shared memory segment id corresponding to the given key
*/
int get_segment_id(int sys_key) {
int id = shmget(sys_key, SHARED_MEM_SIZE, S_IRUSR|S_IWUSR|IPC_CREAT);
return id;
}
/**
* Initialize the memory with our memory_layout struct at the very beginning
*/
memory_layout *shared_mem_init(char* mem_base) {
memset(mem_base, 0, sizeof(memory_layout));
memory_layout *layout = (memory_layout *) mem_base;
layout->next_free = mem_base + sizeof(memory_layout);
layout->open_nodes = NULL;
layout->processes = NULL;
layout->resources = NULL;
pthread_mutexattr_init(&layout->mutexattr);
pthread_mutexattr_setpshared(&layout->mutexattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&layout->mutex, &layout->mutexattr);
pthread_condattr_init(&layout->condattr);
pthread_condattr_setpshared(&layout->condattr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&layout->no_cycle, &layout->condattr);
return layout;
}
/**
* Get the lock all processes should use
*/
pthread_mutex_t *get_lock(memory_layout* mem) {
return &mem->mutex;
}
/**
* Acquire the lock
*/
void mutex_lock(memory_layout* mem) {
pthread_mutex_lock(get_lock(mem));
}
/**
* Release the lock
*/
void mutex_unlock(memory_layout* mem) {
pthread_mutex_unlock(get_lock(mem));
}
/**
* Get the condition variable that all processes waiting to open a file which would result in a cycle should wait on
*/
pthread_cond_t *get_cycle_cond(memory_layout* mem) {
return &mem->no_cycle;
}
/**
* Reset the fields of the given node object
*/
void reset_node(node *loc) {
memset(loc, 0, sizeof(node));
loc->out_edges = NULL;
loc->next = NULL;
loc->pid = 0;
loc->state = UNVISITED;
loc->data = NULL;
loc->fp = NULL;
}
/**
* Allocate a new node in the shared memory segment
*
* Implementation: look to see if we can re-use any old node objects.
* If not, allocate at location pointed to by mem->next_free and increment it accordingly
*/
node *create_new_node(memory_layout* mem) {
node *loc = NULL;
if(mem->open_nodes != NULL) {
loc = mem->open_nodes;
mem->open_nodes = mem->open_nodes->next;
}
else {
loc = (node *) mem->next_free;
mem->next_free += sizeof(node);
if(mem->next_free > shared_memory + SHARED_MEM_SIZE) {
return NULL;
}
}
reset_node(loc);
return loc;
}
/**
* "Free" the given node, or reset it and add it to the list of available nodes.
*/
void free_node(memory_layout *mem, node *cur) {
reset_node(cur);
cur->next = mem->open_nodes;
mem->open_nodes = cur;
}
/**
* Initialize the given node to be a process node belonging to this process
*/
void init_process_node(node *process) {
process->pid = getpid();
}
/**
* Find the process node with the given pid
*/
node *find_process_node(memory_layout* mem, pid_t pid) {
node *cur = mem->processes;
while(cur != NULL && cur->pid != pid) {
cur = cur->next;
}
return cur;
}
/**
* Find the file node with the given name
*/
node *find_file_node(memory_layout* mem, char *name) {
node *cur = mem->resources;
while(cur != NULL && strcmp(cur->name, name) != 0) {
cur = cur->next;
}
return cur;
}
/**
* Find the file node with the given file pointer
*/
node *find_file_node_fp(memory_layout* mem, FILE *fp) {
node *cur = mem->resources;
while(cur != NULL && cur->fp != fp) {
cur = cur->next;
}
return cur;
}
/**
* If a file node with the given name exists, return it
* Else create a new one with the given name, add it to the list, and return it
*/
node *find_or_create_file_node(memory_layout* mem, char *name) {
node *cur = find_file_node(mem, name);
if(cur == NULL) {
cur = create_new_node(mem);
if(cur == NULL) return NULL;
cur->fp = NULL;
strncpy(cur->name, name, sizeof(cur->name));
cur->next = mem->resources;
mem->resources = cur;
}
return cur;
}
/**
* Create a new node for the calling process and add it to the list.
*/
node *create_process_node(memory_layout *mem) {
node *process = create_new_node(mem);
if(process == NULL) return NULL;
init_process_node(process);
process->next = mem->processes;
mem->processes = process;
return process;
}
/**
* Create a linked list node pointing to the data object from the head list
*/
node *create_list_node(memory_layout *mem, node *data, node **head) {
node *cur = create_new_node(mem);
if(cur == NULL) return NULL;
cur->next = *head;
*head = cur;
cur->data = data;
return cur;
}
/**
* Delete the edge from the given start node to the given end node (processes or resources)
*/
int delete_out_edge(memory_layout *mem, node *start, node *end) {
node *cur = start->out_edges;
node *prev = NULL;
while(cur != NULL && cur->data != end) {
prev = cur;
cur = cur->next;
}
if(cur == NULL) return 1;
if(prev == NULL) {
start->out_edges = cur->next;
}
else {
prev->next = cur->next;
}
free_node(mem, cur);
return 1;
}
/**
* Add an outgoing edge from the start node to the end node (process or resource)
*/
int add_out_edge(memory_layout *mem, node *start, node *end) {
if(create_list_node(mem, end, &start->out_edges) != NULL) {
return 1;
}
return 0;
}
/**
* Return 1 if the given resource has any incoming edges from any processes, 0 otherwise
*/
int resource_has_incoming_edges(memory_layout *mem, node *given_resource) {
node *cur_process = mem->processes;
while(cur_process != NULL) {
node *cur_ptr = cur_process->out_edges;
while(cur_ptr != NULL) {
node *cur_resource = cur_ptr->data;
if(cur_resource == given_resource) {
return 1;
}
cur_ptr = cur_ptr->next;
}
cur_process = cur_process->next;
}
return 0;
}
/**
* Delete the current resource by removing it from the list and freeing it.
*/
void delete_resource_node(memory_layout *mem, node *given_resource) {
node* cur_resource = mem->resources;
node* prev_resource = NULL;
while(cur_resource != NULL && cur_resource != given_resource) {
prev_resource = cur_resource;
cur_resource = cur_resource->next;
}
if(prev_resource != NULL) {
prev_resource->next = cur_resource->next;
}
else {
mem->resources = cur_resource->next;
}
free_node(mem, given_resource);
}
/**
* Depth first search from the given node to all outgoing edges. Returns 1 if there is a cycle in this subgraph, 0 otherwise
*/
int cycle_recursive(node *cur) {
// For outgoing edges
cur->state = VISITED;
node *cur_list_node = cur->out_edges;
while(cur_list_node != NULL) {
node *cur_node = cur_list_node->data;
if(cur_node->state == UNVISITED) {
// Visit that guy
if(cycle_recursive(cur_node)) return 1;
}
else if(cur_node->state == VISITED) {
// CYCLE
return 1;
}
else if(cur_node->state == PROCESSED) {
// No need to check
}
cur_list_node = cur_list_node->next;
}
cur->state = PROCESSED;
return 0;
}
/**
* Search for a cycle in the resource allocation graph. Return 1 if so, 0 otherwise
*/
int cycle_exists(memory_layout *mem) {
// Initialize nodes' status
node *cur = mem->processes;
while(cur != NULL) {
cur->state = UNVISITED;
cur = cur->next;
}
cur = mem->resources;
while(cur != NULL) {
cur->state = UNVISITED;
cur = cur->next;
}
// Start recursion in a loop or two
cur = mem->processes;
while(cur != NULL) {
if(cur->state == UNVISITED) {
if(cycle_recursive(cur)) return 1;
}
cur = cur->next;
}
cur = mem->resources;
while(cur != NULL) {
if(cur->state == UNVISITED) {
if(cycle_recursive(cur)) return 1;
}
cur = cur->next;
}
return 0;
}
// Public API implementation:
/**
* Called once to initialize the shared memory data structures for this library.
*
* Parameters: sys_key - The ID of the shared memory segment all participating processes should use.
* Returns: 1 on success, 0 otherwise
*/
int sfs_init(int sys_key) {
// Initialize shared memory
int id = get_segment_id(sys_key);
if(id == -1) return 0;
char *shared_mem = (char *) shmat(id, NULL, 0);
if(shared_mem == (void *)-1) return 0;
shared_mem_init(shared_mem);
int result = shmdt(shared_mem);
if(result == -1) return 0;
return 1;
}
/**
* Declares the shared memory segment this process will be using and the files it will ever possibly open. This must be called before sfs_fopen().
*
* Parameters: sys_key - The ID of the shared memory segment initialized in sfs_init.
* file_num - The number of files this process might open (or size of filenames)
* filenames - The names of the files this process wishes to open (in the future).
* Returns: 1 on success, 0 otherwise
*/
int sfs_declare(int sys_key, int file_num, char *filenames[]) {
// Initialize shared memory pointers
segment_id = get_segment_id(sys_key);
if(segment_id == -1) return 0;
shared_memory = (char *) shmat(segment_id, NULL, 0);
if(shared_memory == (void *)-1) return 0;
memory = (memory_layout *) shared_memory;
mutex_lock(memory);
// Create process node for this process
node *process = create_process_node(memory);
if(process == NULL) {
mutex_unlock(memory);
return 0;
}
// Create or get resource nodes for all files
int i;
for(i = 0; i < file_num; i++) {
char *name = filenames[i];
node *resource = find_or_create_file_node(memory, name);
if(resource == NULL) {
mutex_unlock(memory);
return 0;
}
// Record that this process has claim edges to the given files
node *list_node_cur = create_list_node(memory, resource, &process->out_edges);
if(list_node_cur == NULL) {
mutex_unlock(memory);
return 0;
}
}
mutex_unlock(memory);
return 1;
}
/**
* Open and lock the given file, ensuring that no deadlock will occur now or in the future over contention for this file.
* If opening this file immediately would cause a deadlock, this method will block until the file can be safely opened and locked.
* If the file cannot be opened, NULL is returned.
*
* Parameters: path - path to the file you wish to open and lock
* mode - mode in which to open the file (same as the argument to fopen())
* Returns: A file pointer to the opened file or NULL on error
*/
FILE *sfs_fopen(char *path, char *mode) {
mutex_lock(memory);
// Turn claim edge to assignment edge
node *resource = find_file_node(memory, path);
node *process = find_process_node(memory, getpid());
if(resource == NULL || process == NULL) {\
mutex_unlock(memory);
return NULL;
}
delete_out_edge(memory, process, resource);
add_out_edge(memory, resource, process);
// While a cycle exists
while(cycle_exists(memory)) {
// Convert back to claim edge
delete_out_edge(memory, resource, process);
add_out_edge(memory, process, resource);
// Wait
pthread_cond_wait(get_cycle_cond(memory), get_lock(memory));
// Add edge back
delete_out_edge(memory, process, resource);
add_out_edge(memory, resource, process);
}
// Upon getting the lock and assuring no cycle, open the file
FILE *res = fopen(path, mode);
resource->fp = res;
mutex_unlock(memory);
return res;
}
/**
* Close and unlock a file which was previously opened and locked using sfs_fopen.
*
* Parameters: fp - file pointer to the file which you wish to close
* Returns: 1 on success, 0 otherwise
*/
int sfs_fclose(FILE *fp) {
if(fp == NULL) return 0;
mutex_lock(memory);
// Find this process and file resource
node *resource = find_file_node_fp(memory, fp);
node *process = find_process_node(memory, getpid());
if(resource == NULL || process == NULL) {
mutex_unlock(memory);
return 0;
}
resource->fp = NULL;
// Convert back to claim edge
delete_out_edge(memory, resource, process);
add_out_edge(memory, process, resource);
// Close file
int result = fclose(fp);
// Broadcast conditional variable
pthread_cond_broadcast(get_cycle_cond(memory));
mutex_unlock(memory);
return (result != EOF);
}
/**
* End this process's access to the shared files. All files opened by this process are closed and unlocked,
* this process is removed from the system, and the shared memory segment is detached.
*
* If the current process wishes to use this library any further (except calling sfs_destroy), it must re-call sfs_declare().
*
* Parameters: sys_key - the unique ID of the shared memory segment used in sfs_init and sfs_declare
* Returns: 1 on success, 0 otherwise
*/
int sfs_leave(int sys_key) {
mutex_lock (memory);
// Remove this process from overall list
node* cur_process = memory->processes;
node* prev_process = NULL;
while(cur_process != NULL && cur_process->pid != getpid()) {
prev_process = cur_process;
cur_process = cur_process->next;
}
// Remove the process
if(prev_process != NULL) prev_process->next = cur_process->next;
else memory->processes = cur_process->next;
// Loop through our open files and close them
// For each resource
node *cur_resource = memory->resources;
while(cur_resource != NULL) {
// If we are the target of this resource's outgoing edge
if(cur_resource->out_edges != NULL && cur_resource->out_edges->data == cur_process) {
// Close file and flip edge back
delete_out_edge(memory, cur_resource, cur_process);
add_out_edge(memory, cur_process, cur_resource);
// Close file
int result = fclose(cur_resource->fp);
if(result == EOF) {
mutex_unlock(memory);
return 0;
}
}
cur_resource = cur_resource->next;
}
// Loop through this process's files and delete ones with no other users
// For each outgoing edge
node *cur_resource_list = cur_process->out_edges;
while(cur_resource_list != NULL) {
cur_resource = cur_resource_list->data;
// If has an outgoing edge to someone else, go on
if(cur_resource->out_edges == NULL) {
// Otherwise, loop through all processes and search for outgoing edges to resource
// If none are found, remove this resource
if(!resource_has_incoming_edges(memory, cur_resource)) {
// delete this node (remove from resource list and free it)
delete_resource_node(memory, cur_resource);
}
}
cur_resource_list = cur_resource_list->next;
}
// Remove this process's process node
free_node(memory, cur_process);
// Broadcast conditional variable
pthread_cond_broadcast(get_cycle_cond(memory));
mutex_unlock(memory);
int result = shmdt(shared_memory);
// Update local stuff
memory = NULL;
shared_memory = NULL;
segment_id = -1;
if(result == -1) return 0;
return 1;
}
/**
* Close any open files and destroy the shared memory segment. This must be the last method of this library called.
*
* Parameters: sys_key - the unique ID of the shared memory segment used in sfs_init and sfs_declare
* Returns: 1 on success, 0 otherwise
*/
int sfs_destroy(int sys_key) {
// Reattach to shared memory
segment_id = get_segment_id(sys_key);
if(segment_id == -1) return 0;
shared_memory = (char *) shmat(segment_id, NULL, 0);
if(shared_memory == (void *)-1) return 0;
memory = (memory_layout *) shared_memory;
// Close leftover files
// For each resource, if it has outgoing edges, close that file
node *cur_resource = memory->resources;
while(cur_resource != NULL) {
// For each outgoing edge
if(cur_resource->out_edges != NULL) {
// Close file
int result = fclose(cur_resource->fp);
if(result == EOF) return 0;
}
}
// Detach and destroy shared memory segment
int result = shmdt(shared_memory);
if(result == -1) return 0;
result = shmctl(segment_id, IPC_RMID, NULL); // Remove the shared memory block forever
if(result == -1) return 0;
return 1;
}