Skip to content

Commit

Permalink
Changes to the philosophers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcStorm committed Jun 26, 2017
1 parent 847dd3f commit 70c16c9
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions examples/diningPhilosophersRemote/philosophers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import (
)

func main() {
//table := tuplespace.CreateTupleSpace(portTable)
//fmt.Println(table)
//pttable := topology.CreatePointToPoint("me", "localhost", portTable)
//waiterTS := tuplespace.CreateTupleSpace(portWaiter)
//fmt.Println(waiterTS)
//ptwaiter := topology.CreatePointToPoint("me", "localhost", portWaiter)

portTable, err := strconv.Atoi(os.Args[1])
if err != nil {
// handle error
Expand Down Expand Up @@ -52,7 +45,11 @@ func main() {
}

placeForks(ptTable, n)
//go waiter(ptwaiter, n)
// Inform waiter of how many philosopher are at the table.
for !tuplespace.Put(ptWaiter, "philosophers", n) {
fmt.Println("Error in placing the number of philosophers at Waiter.")
}

go philosopher(ptTable, ptWaiter, 0, 0, n-1)
for i := 1; i < n; i++ {
go philosopher(ptTable, ptWaiter, i, i-1, i)
Expand All @@ -74,19 +71,24 @@ func philosopher(ptTable topology.PointToPoint, ptWaiter topology.PointToPoint,
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
fmt.Printf("Philosopher %d is hungry\n", n)
//sends request to eat
tuplespace.Put(ptWaiter, "request", n)
//look for permission
tuplespace.Query(ptWaiter, "permission", n)
//grab forks
tuplespace.Get(ptTable, "fork", fork1)
tuplespace.Get(ptTable, "fork", fork2)
//eat
fmt.Printf("Philosopher %d is eating for the %d. time\n", n, i)
i++
//return forks
tuplespace.Put(ptTable, "fork", fork1)
tuplespace.Put(ptTable, "fork", fork2)
//remove permission
tuplespace.Get(ptWaiter, "permission", n)
if tuplespace.Put(ptWaiter, "request", n) {
//look for permission
for !tuplespace.Query(ptWaiter, "permission", n) {
fmt.Println("Error in receiving permission.")
}
//grab forks
tuplespace.Get(ptTable, "fork", fork1)
tuplespace.Get(ptTable, "fork", fork2)
//eat
fmt.Printf("Philosopher %d is eating for the %d. time\n", n, i)
i++
//return forks
tuplespace.Put(ptTable, "fork", fork1)
tuplespace.Put(ptTable, "fork", fork2)
//remove permission
for !tuplespace.Get(ptWaiter, "permission", n) {
fmt.Println("Error in getting the permission.")
}
}
}
}

0 comments on commit 70c16c9

Please sign in to comment.