-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlamport-me.go
212 lines (185 loc) · 4.14 KB
/
lamport-me.go
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
package main
import( "fmt"
"sort"
"time"
"sync"
)
func max(a,b int) int {
var ret int
switch a>b{
case true:
ret=a
case false:
ret=b
}
return ret
}
type Pair struct {
pid int
TimeStamp int
}
type ByTimeStamp []Pair
func (q ByTimeStamp) Len() int {
return len(q)
}
func (q ByTimeStamp) Swap(i,j int) {
q[i],q[j]=q[j],q[i]
}
func (q ByTimeStamp) Less(i,j int) bool{
if (q[i].TimeStamp == q[j].TimeStamp){
return q[i].pid < q[j].pid
}
return q[i].TimeStamp< q[j].TimeStamp
}
type Process struct{
pid int
clock int
mux sync.Mutex
reqTimeStamp int
inCS bool
ReqQueue []Pair
}
func NewProcess(i int) *Process{
return &Process{pid :i,
clock:0,
inCS : false,
ReqQueue : make([]Pair,0),
}
}
var wg sync.WaitGroup
func (p *Process) SendRequest(ch1[] chan Pair){
curr :=p.pid
p.mux.Lock()
p.clock++
p.mux.Unlock()
p.reqTimeStamp = p.clock
p.ReqQueue = append(p.ReqQueue,Pair{curr,p.clock})
for i := 0 ; i < len(ch1) ; i++{
if i!=curr{
go func(i int){ ch1[i]<-Pair{curr,p.reqTimeStamp} }(i)
}
}
}
func (p *Process) RecvRequest(ch1 []chan Pair , ch2 [] chan Pair){
curr :=p.pid
for{
req:= <-ch1[curr]
p.mux.Lock()
p.clock = max(p.clock,req.TimeStamp)+1
p.mux.Unlock()
p.ReqQueue=append(p.ReqQueue,req)
sort.Sort(ByTimeStamp(p.ReqQueue))
p.mux.Lock()
p.clock++
p.mux.Unlock()
ch2[curr] <- Pair{curr,p.clock}
}
}
func (p *Process )RecvReply(ch2 [] chan Pair){
curr :=p.pid
for i:=0 ; i <len(ch2) ; i++{
if i!=curr{
wg.Add(1)
go func(i int){ for {
reply := <-ch2[i]
p.mux.Lock()
p.clock = max(p.clock,reply.TimeStamp)+1
p.mux.Unlock()
if reply.TimeStamp>p.reqTimeStamp { break }
}
wg.Done() }(i)
}
}
wg.Wait()
}
func (p *Process) DoSomething(){
fmt.Printf("Process %d Entered\n",p.pid)
time.Sleep(5*time.Second)
fmt.Printf("Process %d Exited\n",p.pid)
}
func (p *Process) SendRelease(ch3 []chan Pair){
curr := p.pid
p.mux.Lock()
p.clock++
p.mux.Unlock()
for i,_ :=range ch3{
if i!=curr{
go func(i int){ch3[i] <- Pair{curr,p.clock} }(i)
}
}
p.ReqQueue = p.ReqQueue[1:]
}
func (p *Process) RecvRelease(ch3 []chan Pair , ch []chan bool){
curr :=p.pid
for{
release:= <-ch3[curr]
p.ReqQueue = p.ReqQueue[1:]
p.mux.Lock()
p.clock = max(p.clock,release.TimeStamp)+1
p.mux.Unlock()
for{
if(len(p.ReqQueue)!=0) {break}
}
if p.ReqQueue[0].pid == curr{
ch[curr] <- true
}
}
}
func (p *Process) AcquireLock(ch1,ch2,ch3 []chan Pair,ch []chan bool,wg *sync.WaitGroup){
p.SendRequest(ch1)
p.RecvReply(ch2)
for{
status := <-ch[p.pid]
if status == true{
break
}
}
p.inCS = true
p.DoSomething()
p.inCS = false
p.SendRelease(ch3)
wg.Done()
}
func main(){
N:=5
ch1 := make([]chan Pair,N)
//Initialising Channels for Request Sending and Receiving
for i,_ := range ch1{
ch1[i] = make(chan Pair)
}
ch2 := make([]chan Pair,N)
//Initialising Channels for Reply Sending and Receiving
for i,_ := range ch2{
ch2[i] = make(chan Pair)
}
ch3 := make([]chan Pair,N)
//Initialising Channels for Release Sending and Receiving
for i,_ := range ch3{
ch3[i] = make(chan Pair)
}
ch := make([] chan bool,N)
//Initialising Channels for Signal Sending and Receiving
for i,_ := range ch{
ch[i] = make(chan bool)
}
//Initialising List of Processes
PList := make([] *Process,N)
for i:= 0;i < N ; i++{
PList[i] = NewProcess(i)
}
for i:=0;i<N;i++{
go PList[i].RecvRequest(ch1,ch2)
go PList[i].RecvRelease(ch3,ch)
}
var wg1 sync.WaitGroup
wg1.Add(1)
go PList[0].AcquireLock(ch1,ch2,ch3,ch,&wg1)
ch[0] <-true
wg1.Add(1)
go PList[1].AcquireLock(ch1,ch2,ch3,ch,&wg1)
wg1.Add(1)
go PList[2].AcquireLock(ch1,ch2,ch3,ch,&wg1)
wg1.Add(1)
go PList[3].AcquireLock(ch1,ch2,ch3,ch,&wg1)
wg1.Wait()
}