11 func random_between (begin
, end
int64) int64 {
12 random_byte
:= make([]byte, 1)
13 rand
.Read(random_byte
)
14 return int64(random_byte
[0]) % (end
- begin
+ 1) + begin
17 func random_sleep () {
18 time
.Sleep(random_between(0, MAX_SLEEP_TIME
) * 10e7
)
21 func signal (channel
chan int) {
22 if len(channel
) < cap(channel
) ||
cap(channel
) == 0 {
27 func wait (channel
chan int) {
37 mutex
= make(chan int, 1);
44 for i
:= 0; ; i
++ { // laço infinito
47 Println("producing:", i
)
49 rear
= (rear
+ 1) % BUFFER_SIZE
57 for { // laço infinito
61 front
= (front
+ 1) % BUFFER_SIZE
62 Println("\t\t\tconsuming:", v
)
70 buffer
= make([]int, BUFFER_SIZE
)
71 empty
= make(chan int, BUFFER_SIZE
);
72 full
= make(chan int, BUFFER_SIZE
)
73 for i
:= 0; i
< BUFFER_SIZE
; i
++ { // inicializa empty com o valor BUFFER\_SIZE
76 signal(mutex
) // inicializa mutex com o valor 1
79 wait(make(chan int)) // espera indefinidamente