Parallele_Verteilte_Systeme/EWD123/ewd123a/ewd123a.go

80 lines
1.4 KiB
Go

/*
* 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:
//wait till turn = 2 (program can run)
if turn == 2 {
//log.Printf("Process 1 waiting\n")
goto L1
}
controller.EnterCriticalSection(1)
controller.InsideCriticalSection(1, 50)
controller.LeaveCriticalSection(1)
turn = 2
//Process 2 can run now (and will in some cases)
controller.OutsideCriticalSection(1, 100)
if controller.ProcessCrashed(0.01) {
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
//Process can run now (and will in some cases)
controller.OutsideCriticalSection(2, 100)
if controller.ProcessCrashed(0.01) {
log.Printf("Process 2 crashed\n")
return
}
goto L2
}