From 37568c556c925ac9c35c4b5814f16cfdbf2ff566 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 9 Mar 2020 14:04:05 +0100 Subject: [PATCH] all algorithms implemented --- EWD123/ewd123.go | 2 +- EWD123/ewd123b/ewd123b.go | 50 ++++++++++++++++++++- EWD123/ewd123c/ewd123c.go | 48 +++++++++++++++++++- EWD123/ewd123d/ewd123d.go | 49 +++++++++++++++++++- EWD123/ewd123dekker/ewd123dekker.go | 70 ++++++++++++++++++++++++++++- 5 files changed, 210 insertions(+), 9 deletions(-) diff --git a/EWD123/ewd123.go b/EWD123/ewd123.go index 86d0004..a90b288 100644 --- a/EWD123/ewd123.go +++ b/EWD123/ewd123.go @@ -32,7 +32,7 @@ func main() { log.Printf("*** Start\n") - var variant = "a" + var variant = "dekker" if len(os.Args[1:]) > 0 { // do we have arguments? variant = os.Args[1] } diff --git a/EWD123/ewd123b/ewd123b.go b/EWD123/ewd123b/ewd123b.go index 14655ac..d6bbdf7 100644 --- a/EWD123/ewd123b/ewd123b.go +++ b/EWD123/ewd123b/ewd123b.go @@ -13,6 +13,11 @@ package ewd123b +import ( + "../controller" + "log" +) + // global synchronization variables var c1, c2 = 1, 1 @@ -24,10 +29,51 @@ func Start() { // process1 simulates the behaviour of the first process func process1() { - // TO DO +L1: + if c2 == 0 { + goto L1 + } + + c1 = 0 + + controller.EnterCriticalSection(1) + controller.InsideCriticalSection(1, 50) + controller.LeaveCriticalSection(1) + + c1 = 1 + + controller.OutsideCriticalSection(1, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 1 crashed") + return + } + + goto L1 } // process2 simulates the behaviour of the second process func process2() { - // TO DO +L2: + if c1 == 0 { + goto L2 + } + + c2 = 0 + + controller.EnterCriticalSection(2) + controller.InsideCriticalSection(2, 50) + controller.LeaveCriticalSection(2) + + c2 = 1 + + controller.OutsideCriticalSection(2, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 2 crashed") + return + } + + goto L2 + } diff --git a/EWD123/ewd123c/ewd123c.go b/EWD123/ewd123c/ewd123c.go index 3c742b9..f19d42a 100644 --- a/EWD123/ewd123c/ewd123c.go +++ b/EWD123/ewd123c/ewd123c.go @@ -13,6 +13,11 @@ package ewd123c +import ( + "../controller" + "log" +) + // global synchronization variables var c1, c2 = 1, 1 @@ -24,10 +29,49 @@ func Start() { // process1 simulates the behaviour of the first process func process1() { - // TO DO +A1: + c1 = 0 +L1: + if c2 == 0 { + goto L1 + } + + controller.EnterCriticalSection(1) + controller.InsideCriticalSection(1, 50) + controller.LeaveCriticalSection(1) + + c1 = 1 + controller.OutsideCriticalSection(1, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 1 crashed") + return + } + + goto A1 + } // process2 simulates the behaviour of the second process func process2() { - // TO DO +A2: + c2 = 0 +L2: + if c1 == 0 { + goto L2 + } + + controller.EnterCriticalSection(2) + controller.InsideCriticalSection(2, 50) + controller.LeaveCriticalSection(2) + + c2 = 1 + controller.OutsideCriticalSection(2, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 2 crashed") + return + } + + goto A2 } diff --git a/EWD123/ewd123d/ewd123d.go b/EWD123/ewd123d/ewd123d.go index 6e8752e..5fee576 100644 --- a/EWD123/ewd123d/ewd123d.go +++ b/EWD123/ewd123d/ewd123d.go @@ -13,6 +13,11 @@ package ewd123d +import ( + "../controller" + "log" +) + // global synchronization variables var c1, c2 = 1, 1 @@ -24,10 +29,50 @@ func Start() { // process1 simulates the behaviour of the first process func process1() { - // TO DO +L1: + c1 = 0 + if c2 == 0 { + c1 = 1 + goto L1 + } + + controller.EnterCriticalSection(1) + controller.InsideCriticalSection(1, 50) + controller.LeaveCriticalSection(1) + + c1 = 1 + + controller.OutsideCriticalSection(1, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 1 crashed") + return + } + + goto L1 } // process2 simulates the behaviour of the second process func process2() { - // TO DO +L2: + c2 = 0 + if c1 == 0 { + c2 = 1 + goto L2 + } + + controller.EnterCriticalSection(2) + controller.InsideCriticalSection(2, 50) + controller.LeaveCriticalSection(2) + + c2 = 1 + + controller.OutsideCriticalSection(2, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 2 crashed") + return + } + + goto L2 } diff --git a/EWD123/ewd123dekker/ewd123dekker.go b/EWD123/ewd123dekker/ewd123dekker.go index fb6f17a..fd24442 100644 --- a/EWD123/ewd123dekker/ewd123dekker.go +++ b/EWD123/ewd123dekker/ewd123dekker.go @@ -13,6 +13,11 @@ package ewd123dekker +import ( + "../controller" + "log" +) + // global synchronization variables var c1, c2 = 1, 1 var turn = 1 @@ -25,10 +30,71 @@ func Start() { // process1 simulates the behaviour of the first process func process1() { - // TO DO +A1: + c1 = 0 +L1: + if c2 == 0 { + if turn == 1 { + goto L1 + } + c1 = 1 + + B1: + if turn == 2 { + goto B1 + } + goto A1 + } + + controller.EnterCriticalSection(1) + controller.InsideCriticalSection(1, 50) + controller.LeaveCriticalSection(1) + + turn = 2 + c1 = 1 + + controller.OutsideCriticalSection(1, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 1 crashed") + return + } + + goto A1 + } // process2 simulates the behaviour of the second process func process2() { - // TO DO +A2: + c2 = 0 +L2: + if c1 == 0 { + if turn == 2 { + goto L2 + } + c2 = 1 + + B2: + if turn == 1 { + goto B2 + } + goto A2 + } + + controller.EnterCriticalSection(2) + controller.InsideCriticalSection(2, 50) + controller.LeaveCriticalSection(2) + + turn = 1 + c2 = 1 + + controller.OutsideCriticalSection(2, 100) + + if controller.ProcessCrashed(0.1) { + log.Println("Process 2 crashed") + return + } + + goto A2 }