Parallele_Verteilte_Systeme/go/EWD123/ewd123d/ewd123d.go

81 lines
1.4 KiB
Go
Raw Normal View History

2020-03-09 12:24:22 +01:00
/*
* 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
2020-03-09 14:04:05 +01:00
import (
"../controller"
2020-03-15 12:52:39 +01:00
"log"
2020-03-09 14:04:05 +01:00
)
2020-03-09 12:24:22 +01:00
// 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() {
2020-03-09 14:04:05 +01:00
L1:
c1 = 0
if c2 == 0 {
2020-03-11 13:31:57 +01:00
//when both processes are at this stage, they will wait for each other to clear this area
//for this to happen the processes need to be executed at exactly the same time.
2020-03-09 14:04:05 +01:00
c1 = 1
goto L1
}
controller.EnterCriticalSection(1)
controller.InsideCriticalSection(1, 50)
controller.LeaveCriticalSection(1)
c1 = 1
controller.OutsideCriticalSection(1, 100)
2020-03-15 12:52:39 +01:00
if controller.ProcessCrashed(0.1) {
2020-03-09 14:04:05 +01:00
log.Println("Process 1 crashed")
return
2020-03-15 12:52:39 +01:00
}
2020-03-09 14:04:05 +01:00
goto L1
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
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)
2020-03-15 12:52:39 +01:00
if controller.ProcessCrashed(0.1) {
2020-03-09 14:04:05 +01:00
log.Println("Process 2 crashed")
return
2020-03-15 12:52:39 +01:00
}
2020-03-09 14:04:05 +01:00
goto L2
2020-03-09 12:24:22 +01:00
}