From 70c16c9a4a8eafdc009c87f4801c23e57f5211b7 Mon Sep 17 00:00:00 2001 From: MarcStorm Date: Mon, 26 Jun 2017 10:47:13 +0200 Subject: [PATCH] Changes to the philosophers --- .../philosophers/main.go | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/examples/diningPhilosophersRemote/philosophers/main.go b/examples/diningPhilosophersRemote/philosophers/main.go index 54e37f5..53b8aa5 100644 --- a/examples/diningPhilosophersRemote/philosophers/main.go +++ b/examples/diningPhilosophersRemote/philosophers/main.go @@ -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 @@ -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) @@ -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.") + } + } } }