first commit

This commit is contained in:
Johannes Theiner 2020-03-09 12:24:22 +01:00
commit ab9573e500
8 changed files with 363 additions and 0 deletions

20
.gitignore vendored Normal file
View File

@ -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

View File

@ -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
}

58
EWD123/ewd123.go Normal file
View File

@ -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 <variant>, 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
}

76
EWD123/ewd123a/ewd123a.go Normal file
View File

@ -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
}

33
EWD123/ewd123b/ewd123b.go Normal file
View File

@ -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
}

33
EWD123/ewd123c/ewd123c.go Normal file
View File

@ -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
}

33
EWD123/ewd123d/ewd123d.go Normal file
View File

@ -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
}

View File

@ -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
}