-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.go
More file actions
214 lines (168 loc) · 4.86 KB
/
request.go
File metadata and controls
214 lines (168 loc) · 4.86 KB
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
package main
import "fmt"
import "time"
import "sort"
const tick = time.Millisecond * 33
// --- RESOURCE ROUTINE --- //
/*
You will modify the resourceManager routine so that it... works...
(You know the drill by now...)
Note: The code in `main` does not contain checks that the final execution order is correct.
See how the request/reply/giveBack operations work in the `resourceUser` routine.
Hints:
- Remember that you can do things outside the `select` too:
```Go
for {
select {
cases...
}
other stuff...
}
```
- Use the priority queue (scroll down below `main()`. And remember the type-assertion thing:
```Go
request := queue.Front().(ResourceRequest)
```
*/
type Resource struct {
value []int // Resource type is []int. Each user appends its own id when executing.
}
type ResourceRequest struct {
id int
priority int
channel chan Resource
}
func resourceManager(askFor chan ResourceRequest, giveBack chan Resource){
res := Resource{}
//busy := false
//queue := PriorityQueue{}
for {
select {
case request := <-askFor:
//fmt.Printf("[resource manager]: received request: %+v\n", request)
request.channel <- res
case res = <-giveBack:
//fmt.Printf("[resource manager]: resource returned\n")
}
}
}
// --- RESOURCE USERS -- //
type ResourceUserConfig struct {
id int
priority int
release int
execution int
}
func resourceUser(cfg ResourceUserConfig, askFor chan ResourceRequest, giveBack chan Resource){
replyChan := make(chan Resource)
time.Sleep(time.Duration(cfg.release) * tick)
executionStates[cfg.id] = waiting
askFor <- ResourceRequest{cfg.id, cfg.priority, replyChan}
res := <- replyChan
executionStates[cfg.id] = executing
time.Sleep(time.Duration(cfg.execution) * tick)
res.value = append(res.value, cfg.id)
giveBack <- res
executionStates[cfg.id] = done
}
func main(){
askFor := make(chan ResourceRequest, 10)
giveBack := make(chan Resource)
go resourceManager(askFor, giveBack)
executionStates = make([]ExecutionState, 10)
cfgs := []ResourceUserConfig{
{0, 0, 1, 1},
{1, 0, 3, 1},
{2, 1, 5, 1},
{0, 1, 10, 2},
{1, 0, 11, 1},
{2, 1, 11, 1},
{3, 0, 11, 1},
{4, 1, 11, 1},
{5, 0, 11, 1},
{6, 1, 11, 1},
{7, 0, 11, 1},
{8, 1, 11, 1},
{0, 1, 25, 3},
{6, 0, 26, 2},
{7, 0, 26, 2},
{1, 1, 26, 2},
{2, 1, 27, 2},
{3, 1, 28, 2},
{4, 1, 29, 2},
{5, 1, 30, 2},
}
go executionLogger()
for _, cfg := range cfgs {
go resourceUser(cfg, askFor, giveBack)
}
// (no way to join goroutines, hacking it with sleep)
time.Sleep(time.Duration(45) * tick)
resourceCh := make(chan Resource)
askFor <- ResourceRequest{0, 1, resourceCh}
executionOrder := <-resourceCh
fmt.Println("Execution order:", executionOrder)
}
// --- PRIORITY QUEUE --- //
/*
Who needs type-safety anyway? Can take elements of any type, and also mix them...
Can take multiple elements for each priority (also of different types).
Ordering of same-priority elements is first-come-first-served.
You will have to use a "type assertion" to cast the interface{} to the correct type:
`first := queue.Front().(YourCustomType)`
*/
type PriorityQueue struct {
queue []struct {
val interface{}
priority int
}
}
func (pq *PriorityQueue) Insert(value interface{}, priority int){
pq.queue = append(pq.queue, struct{val interface{}; priority int}{value, priority})
sort.SliceStable(pq.queue, func(i, j int) bool {
return pq.queue[i].priority > pq.queue[j].priority
})
}
func (pq *PriorityQueue) Front() interface{} {
return pq.queue[0].val
}
func (pq *PriorityQueue) PopFront(){
pq.queue = pq.queue[1:]
}
func(pq *PriorityQueue) Empty() bool {
return len(pq.queue) == 0
}
// --- EXECUTION LOGGING --- //
type ExecutionState rune
const (
none ExecutionState = '\u0020'
waiting = '\u2592'
executing = '\u2593'
done = '\u2580'
)
var executionStates []ExecutionState
func executionLogger(){
time.Sleep(tick/2)
t := 0
fmt.Printf(" id:")
for id, _ := range executionStates {
fmt.Printf("%3d", id)
}
fmt.Printf("\n")
for {
grid := ' ';
if t % 5 == 0 {
grid = '\u2500'
}
fmt.Printf("%04d : ", t)
for id, state := range executionStates {
fmt.Printf("%c%c%c", state, grid, grid)
if state == done {
executionStates[id] = none
}
}
fmt.Printf("\n")
t++
time.Sleep(tick)
}
}