From ab9573e500638fbcde8ac44d6d4b9f7e1d210b9c Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 9 Mar 2020 12:24:22 +0100 Subject: [PATCH] first commit --- .gitignore | 20 ++++++++ EWD123/controller/controller.go | 76 +++++++++++++++++++++++++++++ EWD123/ewd123.go | 58 ++++++++++++++++++++++ EWD123/ewd123a/ewd123a.go | 76 +++++++++++++++++++++++++++++ EWD123/ewd123b/ewd123b.go | 33 +++++++++++++ EWD123/ewd123c/ewd123c.go | 33 +++++++++++++ EWD123/ewd123d/ewd123d.go | 33 +++++++++++++ EWD123/ewd123dekker/ewd123dekker.go | 34 +++++++++++++ 8 files changed, 363 insertions(+) create mode 100644 .gitignore create mode 100644 EWD123/controller/controller.go create mode 100644 EWD123/ewd123.go create mode 100644 EWD123/ewd123a/ewd123a.go create mode 100644 EWD123/ewd123b/ewd123b.go create mode 100644 EWD123/ewd123c/ewd123c.go create mode 100644 EWD123/ewd123d/ewd123d.go create mode 100644 EWD123/ewd123dekker/ewd123dekker.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ec6a17 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# Created by .ignore support plugin (hsz.mobi) +### Go template +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +.idea +*.iml \ No newline at end of file diff --git a/EWD123/controller/controller.go b/EWD123/controller/controller.go new file mode 100644 index 0000000..137cdcc --- /dev/null +++ b/EWD123/controller/controller.go @@ -0,0 +1,76 @@ +/* + * controller.go + * + * The controller provides several check routines for a simulated critical section. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package controller + +import ( + "log" + "math/rand" + "time" +) + +/* global synchronization variable */ +var currentProcess = 0 + +// EnterCriticalSection is called by process 'process' before entering the critical section. +// If another process is already occupying the critical section an error message is generated. +func EnterCriticalSection(process int) { + if currentProcess != 0 { + log.Fatalf("Process %d tried to enter the critical section while it was already occupied by: %d\n", process, currentProcess) + } else { + currentProcess = process + log.Printf("entered CS: %d\n", process) + } +} + +// LeaveCriticalSection is called by process 'process' before leaving the critical section. +// If this process is not currently occupying the critical section an error message is generated. +func LeaveCriticalSection(process int) { + if currentProcess != process { + if currentProcess == 0 { + log.Fatalf("Process %d tried to leave the critical section while it was not occupied\n", process) + } else { + log.Fatalf("Process %d tried to leave the critical section while it was occupied by: %d\n", process, currentProcess) + } + } else { + currentProcess = 0 + log.Printf("left CS: %d\n", process) + } +} + +// InsideCriticalSection is called by process 'process' to simulate it is performing some tasks +// inside the critical section for msec milliseconds. +func InsideCriticalSection(process int, msec time.Duration) { + if process != currentProcess { + log.Fatalf("Process %d tried to work inside the critical section while it has not occupied it\n", process) + } + log.Printf("inside CS: %d (%d msecs)\n", process, msec) + time.Sleep(msec * time.Millisecond) +} + +// OutsideCriticalSection is called by process 'process' to simulate it is performing some tasks +// outside the critical section for msec milliseconds. +func OutsideCriticalSection(process int, msec time.Duration) { + if process == currentProcess { + log.Fatalf("Process %d tried to work outside the critical section while it occupies the critical section\n", process) + } + log.Printf("outside CS: %d (%d msecs)\n", process, msec) + time.Sleep(msec * time.Millisecond) +} + +// ProcessCrashed determines whether the process crashes. +// The parameter probability determines the chance that a process crashes. +// If the process crashes true is returned, false otherwise. +func ProcessCrashed(probability float32) bool { + return rand.Float32() < probability +} diff --git a/EWD123/ewd123.go b/EWD123/ewd123.go new file mode 100644 index 0000000..86d0004 --- /dev/null +++ b/EWD123/ewd123.go @@ -0,0 +1,58 @@ +/* + * ewd123.go + * + * A program to represent the first mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package main + +import ( + "./ewd123a" + "./ewd123b" + "./ewd123c" + "./ewd123d" + "./ewd123dekker" + "fmt" + "log" + "os" +) + +// main is the entry point for execution. +func main() { + var quitChannel = make(chan string) // channel to send a quit signal + + log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile) // set specific logging attributes + + log.Printf("*** Start\n") + + var variant = "a" + if len(os.Args[1:]) > 0 { // do we have arguments? + variant = os.Args[1] + } + + switch variant { + case "a": + ewd123a.Start() + case "b": + ewd123b.Start() + case "c": + ewd123c.Start() + case "d": + ewd123d.Start() + case "dekker": + ewd123dekker.Start() + default: + log.Printf("unknown variant: '%s'!", variant) + fmt.Printf("Usage: %s , where variant is a,b,c,d or empty for the Dekker solution\n", os.Args[0]) + os.Exit(-1) // signal error code -1 + } + + log.Printf("*** Finish: %s\n", <-quitChannel) // wait for a quit signal +} diff --git a/EWD123/ewd123a/ewd123a.go b/EWD123/ewd123a/ewd123a.go new file mode 100644 index 0000000..e6084c6 --- /dev/null +++ b/EWD123/ewd123a/ewd123a.go @@ -0,0 +1,76 @@ +/* + * ewd123a.go + * + * A program to represent the first mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package ewd123a + +import ( + "../controller" + "log" +) + +// global synchronization variable +var turn = 1 + +// Start starts the execution of EWD123a. +func Start() { + go process1() + go process2() +} + +// process1 simulates the behaviour of the first process +func process1() { +L1: + if turn == 2 { + //log.Printf("Process 1 waiting\n") + goto L1 + } + + controller.EnterCriticalSection(1) + controller.InsideCriticalSection(1, 50) + controller.LeaveCriticalSection(1) + + turn = 2 + + controller.OutsideCriticalSection(1, 100) + + if controller.ProcessCrashed(0.1) { + log.Printf("Process 1 crashed\n") + return + } + + goto L1 +} + +// process2 simulates the behaviour of the second process +func process2() { +L2: + if turn == 1 { + //log.Printf("Process 2 waiting\n") + goto L2 + } + + controller.EnterCriticalSection(2) + controller.InsideCriticalSection(2, 50) + controller.LeaveCriticalSection(2) + + turn = 1 + + controller.OutsideCriticalSection(2, 100) + + if controller.ProcessCrashed(0.1) { + log.Printf("Process 2 crashed\n") + return + } + + goto L2 +} diff --git a/EWD123/ewd123b/ewd123b.go b/EWD123/ewd123b/ewd123b.go new file mode 100644 index 0000000..14655ac --- /dev/null +++ b/EWD123/ewd123b/ewd123b.go @@ -0,0 +1,33 @@ +/* + * ewd123b.go + * + * A program to represent the second mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package ewd123b + +// global synchronization variables +var c1, c2 = 1, 1 + +// Start starts the execution of EWD123b. +func Start() { + go process1() + go process2() +} + +// process1 simulates the behaviour of the first process +func process1() { + // TO DO +} + +// process2 simulates the behaviour of the second process +func process2() { + // TO DO +} diff --git a/EWD123/ewd123c/ewd123c.go b/EWD123/ewd123c/ewd123c.go new file mode 100644 index 0000000..3c742b9 --- /dev/null +++ b/EWD123/ewd123c/ewd123c.go @@ -0,0 +1,33 @@ +/* + * ewd123c.go + * + * A program to represent the third mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package ewd123c + +// global synchronization variables +var c1, c2 = 1, 1 + +// Start starts the execution of EWD123c. +func Start() { + go process1() + go process2() +} + +// process1 simulates the behaviour of the first process +func process1() { + // TO DO +} + +// process2 simulates the behaviour of the second process +func process2() { + // TO DO +} diff --git a/EWD123/ewd123d/ewd123d.go b/EWD123/ewd123d/ewd123d.go new file mode 100644 index 0000000..6e8752e --- /dev/null +++ b/EWD123/ewd123d/ewd123d.go @@ -0,0 +1,33 @@ +/* + * ewd123d.go + * + * A program to represent the fourth mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 21 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package ewd123d + +// global synchronization variables +var c1, c2 = 1, 1 + +// Start starts the execution of EWD123d. +func Start() { + go process1() + go process2() +} + +// process1 simulates the behaviour of the first process +func process1() { + // TO DO +} + +// process2 simulates the behaviour of the second process +func process2() { + // TO DO +} diff --git a/EWD123/ewd123dekker/ewd123dekker.go b/EWD123/ewd123dekker/ewd123dekker.go new file mode 100644 index 0000000..fb6f17a --- /dev/null +++ b/EWD123/ewd123dekker/ewd123dekker.go @@ -0,0 +1,34 @@ +/* + * ewd123dekker.go + * + * A program to represent the Dekker mutex strategy, as described in EWD123. + * + * Copyright (c) 2019-2019 HS Emden/Leer + * All Rights Reserved. + * + * version 1.00 - 23 Oct 2019 - GJV - initial version + * + * author: Gert Veltink, gert.veltink@hs-emden-leer.de (GJV) + */ + +package ewd123dekker + +// global synchronization variables +var c1, c2 = 1, 1 +var turn = 1 + +// Start starts the execution of Dekker's solution form EWD123. +func Start() { + go process1() + go process2() +} + +// process1 simulates the behaviour of the first process +func process1() { + // TO DO +} + +// process2 simulates the behaviour of the second process +func process2() { + // TO DO +}