2020-03-09 12:24:22 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2020-03-09 14:04:05 +01:00
|
|
|
import (
|
|
|
|
"../controller"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2020-03-09 12:24:22 +01:00
|
|
|
// 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() {
|
2020-03-09 14:04:05 +01:00
|
|
|
A1:
|
|
|
|
c1 = 0
|
|
|
|
L1:
|
|
|
|
if c2 == 0 {
|
2020-03-11 13:31:57 +01:00
|
|
|
//if both processes are stuck here, the turn variable will decide which process runs first
|
2020-03-09 14:04:05 +01:00
|
|
|
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
|
|
|
|
|
2020-03-09 12:24:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// process2 simulates the behaviour of the second process
|
|
|
|
func process2() {
|
2020-03-09 14:04:05 +01:00
|
|
|
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)
|
|
|
|
|
2020-04-21 11:04:32 +02:00
|
|
|
/*if controller.ProcessCrashed(0.1) {
|
2020-03-09 14:04:05 +01:00
|
|
|
log.Println("Process 2 crashed")
|
|
|
|
return
|
2020-04-21 11:04:32 +02:00
|
|
|
}*/
|
2020-03-09 14:04:05 +01:00
|
|
|
|
|
|
|
goto A2
|
2020-03-09 12:24:22 +01:00
|
|
|
}
|