-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudocode.txt
77 lines (67 loc) · 1.41 KB
/
pseudocode.txt
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
Using semaphores in FIFO priority, without the semaphore.h librabry.
//*********************SEMAPHORE FIFO PSEUDOCODE*********
struct Semaphore {
int value = 1;
Queue *Q = new Queue();
void sem_init(int data){
value=data;
}
void sem_wait(int pid){
value--;
if(value<0){
Q->push(pid);
block();
}
}
void sem_post(){
value++;
if(value<=0){
int pid=Q.front();
Q.pop();
wakeup(pid);
}
}
}
//*********************************************************
//The code submitted uses the semaphore librabry, so the pseudocode provides FIFO implementation of semaphores
INITIALIZATION:
begin: semaphore(1)
finish: semaphore(1)
writer_wait: semaphore(0)
reader_enter: int(0)
reader_exit: int(0)
writer_waiting: bool(false)
READER function pseudocode:
void *reader(void* id){
//ENTRY SECTION
begin.wait()
reader_enter+=1
begin.signal()
//CRITICAL SECTION
Read DATA
//EXIT SECTION
finish.wait()
reader_exit+=1
if(writer_waiting==true && reader_enter==reader_exit)
then writer_wait.signal()
finish.signal()
}
WRITER function pseudocode:
void *writer(void* id){
//ENTRY SECTION
begin.wait()
finish.wait()
if(reader_enter==reader_exit)
then
finish.signal()
else{
writer_waiting=true
finish.signal()
writer_wait.wait()
writer_waiting=false
}
//CRITICAL SECTION
Process and Modify Data
//EXIT SECTION
begin.signal()
}