Created
December 20, 2018 16:41
-
-
Save higuoxing/cff7b113d68d566a46b2414199f98f9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
"math/rand" | |
) | |
type semaphore chan int | |
var ( | |
smoker_match = make(semaphore, 1) | |
smoker_paper = make(semaphore, 1) | |
smoker_tobacco = make(semaphore, 1) | |
smoking_done = make(semaphore, 1) | |
) | |
func (sema *semaphore) P() { | |
for { | |
if len(*sema) > 0 { | |
<- *sema | |
break | |
} | |
runtime.Gosched() | |
} | |
} | |
func (sema *semaphore) V() { | |
for { | |
if len(*sema) < cap(*sema) { | |
*sema <- 1 | |
break | |
} | |
runtime.Gosched() | |
} | |
} | |
func provider() { | |
for { | |
random := rand.Intn(3) | |
switch (random) { | |
case 0: | |
smoker_match.V() | |
case 1: | |
smoker_paper.V() | |
case 2: | |
smoker_tobacco.V() | |
} | |
smoking_done.P() | |
} | |
} | |
func smoker_0() { | |
for { | |
smoker_match.P() | |
fmt.Println("Smoker who has match is smoking") | |
smoking_done.V() | |
} | |
} | |
func smoker_1() { | |
for { | |
smoker_paper.P() | |
fmt.Println("Smoker who has paper is smoking") | |
smoking_done.V() | |
} | |
} | |
func smoker_2() { | |
for { | |
smoker_tobacco.P() | |
fmt.Println("Smoker who has tobacco is smoking") | |
smoking_done.V() | |
} | |
} | |
func init() { | |
smoking_done.V() | |
} | |
func main() { | |
go provider() | |
go smoker_0() | |
go smoker_1() | |
go smoker_2() | |
time.Sleep(time.Duration(5) * time.Millisecond) | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
type semaphore chan int | |
var ( | |
chopstics = make([]semaphore, 5) | |
mutex = make(semaphore, 1) | |
) | |
func (sema *semaphore) P() { | |
for { | |
if len(*sema) > 0 { | |
<- *sema | |
break | |
} | |
runtime.Gosched() | |
} | |
} | |
func (sema *semaphore) V() { | |
for { | |
if len(*sema) < cap(*sema) { | |
*sema <- 1 | |
break | |
} | |
runtime.Gosched() | |
} | |
} | |
func dining(i int) { | |
for { | |
mutex.P() | |
chopstics[i].P() | |
chopstics[(i+1)%5].P() | |
mutex.V() | |
fmt.Printf("Philosopher %v is eating\n", i+1) | |
chopstics[i].V() | |
chopstics[(i+1)%5].V() | |
fmt.Printf("Philosopher %v is thinking\n", i+1) | |
} | |
} | |
func init() { | |
for i := 0; i < 5; i ++ { | |
chopstics[i] = make(semaphore, 1) | |
chopstics[i].V() | |
} | |
mutex.V() | |
} | |
func main() { | |
for i := 0; i < 5; i ++ { | |
go dining(i) | |
} | |
time.Sleep(time.Duration(5) * time.Millisecond) | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"fmt" | |
) | |
type semaphore chan int | |
const maxSize = 3 | |
var ( | |
emptyCount = make(semaphore, maxSize) | |
fullCount = make(semaphore, maxSize) | |
useQueue = make(semaphore, 1) | |
items = make(semaphore, maxSize) | |
) | |
func (sema *semaphore) P() { | |
for { | |
if len(*sema) > 0 { | |
<- *sema | |
break | |
} | |
} | |
} | |
func (sema *semaphore) V() { | |
for { | |
if len(*sema) < cap(*sema) { | |
*sema <- 1 | |
break | |
} | |
} | |
} | |
func producer() { | |
for { | |
emptyCount.P() | |
useQueue.P() | |
items.V() | |
fmt.Printf("[Producer] Produce 1. Now we have %v items.\n", len(items)) | |
useQueue.V() | |
fullCount.V() | |
} | |
} | |
func consumer() { | |
for { | |
fullCount.P() | |
useQueue.P() | |
items.P() | |
fmt.Printf("[Consumer] Consume 1. Now we have %v items.\n", len(items)) | |
useQueue.V() | |
emptyCount.V() | |
} | |
} | |
func init() { | |
for i := 0; i < maxSize; i ++ { | |
emptyCount <- 1 | |
} | |
useQueue <- 1 | |
} | |
func main() { | |
go producer() | |
go consumer() | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment