forked from naelag/lab2-f17
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshm.c
93 lines (87 loc) · 2.83 KB
/
shm.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
#include "param.h"
#include "types.h"
#include "defs.h"
#include "x86.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
const uint SHM_SIZE = 64; //ADDED LAB4 (64 pages in shared memory)
struct {
struct spinlock lock;
struct shm_page {
uint id;
char *frame;
int refcnt;
} shm_pages[64];
} shm_table;
void shminit() {
int i;
initlock(&(shm_table.lock), "SHM lock");
acquire(&(shm_table.lock));
for (i = 0; i< 64; i++) {
shm_table.shm_pages[i].id =0;
shm_table.shm_pages[i].frame =0;
shm_table.shm_pages[i].refcnt =0;
}
release(&(shm_table.lock));
}
int shm_open(int id, char **pointer) {
int i;
acquire(&(shm_table.lock));
for (i = 0; i< SHM_SIZE; i++) { //looks through reference table to see if segment exsists
if (shm_table.shm_pages[i].id == id) { //if it does exsist
if (mappages(myproc()->pgdir, (char *)PGROUNDUP(myproc()->sz), PGSIZE, V2P(shm_table.shm_pages[i].frame), PTE_W|PTE_U) == -1) {
release(&(shm_table.lock));
return -1;
}
shm_table.shm_pages[i].refcnt += 1; //case 1
*pointer = (char *)PGROUNDUP(myproc()->sz);
myproc()->sz += PGSIZE; //update size
release(&(shm_table.lock));
return 0;
}
}
for (i = 0; i< SHM_SIZE; i++) {
if (shm_table.shm_pages[i].id == 0) { //CASE 2
shm_table.shm_pages[i].id = id; //and store this information in the shm_table
if ((shm_table.shm_pages[i].frame = kalloc()) == 0) { //get physical address
release(&(shm_table.lock));
return -1;
}
memset(shm_table.shm_pages[i].frame, 0, PGSIZE); //If it doesn’t then it needs to allocate a page
shm_table.shm_pages[i].refcnt = 1;
if (mappages(myproc()->pgdir, (char *)PGROUNDUP(myproc()->sz), PGSIZE, V2P(shm_table.shm_pages[i].frame), PTE_W|PTE_U) == -1) { //and map it
release(&(shm_table.lock));
return -1;
}
*pointer = (char *)PGROUNDUP(myproc()->sz); //pointer to virtual address
myproc()->sz += PGSIZE; //update size
release(&(shm_table.lock));
return 0;
}
}
//release the lock but shouldn't get here
release(&(shm_table.lock));
return 0;
}
//ADDED LAB 4
int shm_close(int id) {
//you write this too!
int i;
acquire(&(shm_table.lock));
for (i = 0; i < SHM_SIZE; i++) {
if (shm_table.shm_pages[i].id == id) {
shm_table.shm_pages[i].refcnt -= 1;
if (shm_table.shm_pages[i].refcnt <= 0) {
shm_table.shm_pages[i].id = 0;
shm_table.shm_pages[i].frame = 0;
shm_table.shm_pages[i].refcnt = 0;
}
release(&(shm_table.lock));
return 0;
}
}
release(&(shm_table.lock));
return -1;
}