Parallele_Verteilte_Systeme/EWD123/ewd123a/ewd123a.go

86 lines
1.5 KiB
Go
Raw Normal View History

2020-03-09 12:24:22 +01:00
/*
* 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()
2020-03-11 13:31:57 +01:00
/**
Prozesse wechseln sich immer nur ab.
Ablauf ist 1,2,1,2,1,2,1,2,1,2
*/
2020-03-09 12:24:22 +01:00
}
// process1 simulates the behaviour of the first process
func process1() {
L1:
2020-03-11 13:31:57 +01:00
//wait till turn = 2 (program can run)
2020-03-09 12:24:22 +01:00
if turn == 2 {
//log.Printf("Process 1 waiting\n")
goto L1
}
controller.EnterCriticalSection(1)
controller.InsideCriticalSection(1, 50)
controller.LeaveCriticalSection(1)
turn = 2
2020-03-11 13:31:57 +01:00
//Process 2 can run now (and will in some cases)
2020-03-09 12:24:22 +01:00
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
2020-03-11 13:31:57 +01:00
//Process can run now (and will in some cases)
2020-03-09 12:24:22 +01:00
controller.OutsideCriticalSection(2, 100)
if controller.ProcessCrashed(0.1) {
log.Printf("Process 2 crashed\n")
return
}
goto L2
}