Skip to content

Instantly share code, notes, and snippets.

View mrnkr's full-sized avatar
🏠
Working from home

Alvaro Nicoli mrnkr

🏠
Working from home
View GitHub Profile
@mrnkr
mrnkr / peterson.swift
Created October 23, 2018 22:40
Peterson's algorithm for mutual exclusion
import UIKit
var n: Int = 0;
var favoredProcess: String = "first"
var processOneWantsIn: Bool = false
var processTwoWantsIn: Bool = false
func processOne(task: () -> Void) {
while n < 50 {
processOneWantsIn = true
@mrnkr
mrnkr / dekker.swift
Created October 23, 2018 22:41
Dekker's algorithm for mutual exclusion
import UIKit
var ran: Int = 0;
var favoredProcess: String = "first"
var processOneWantsIn: Bool = false
var processTwoWantsIn: Bool = false
func processOne(task: () -> Void) {
while ran < 50 {
processOneWantsIn = true
@mrnkr
mrnkr / semaphore.swift
Created October 23, 2018 23:27
Semaphores for solving the mutual exclusion problem
import UIKit
class Semaphore {
var n: Int
init(n: Int) {
self.n = n
}
func P() {
@mrnkr
mrnkr / prod-cons.swift
Created October 24, 2018 01:54
Producer-Consumer problem implemented in Swift using Semaphores
import UIKit
class Semaphore {
var n: Int
init(n: Int) {
self.n = n
}
func P() {
@mrnkr
mrnkr / read-write.swift
Created October 24, 2018 18:38
Readers and writers problem solved in swift using semaphores
import UIKit
class Semaphore {
var n: Int
init(n: Int) {
self.n = n
}
func P() {
@mrnkr
mrnkr / philosophers.swift
Created October 24, 2018 18:52
Philosophers problem solved with semaphores on swift
import UIKit
class Semaphore {
var n: Int
init(n: Int) {
self.n = n
}
func P() {
@mrnkr
mrnkr / monitors.swift
Created October 24, 2018 21:14
Semaphores implemented using monitors and condition variables
import UIKit
class Condition {
var waiting: Bool
init() {
waiting = false
}
func wait() {
Chopsticks: Array 0..4 of Chopstick;
Philosophers: Array 0..4 of Philosopher;
task type Philosopher is
end
task body Philosopher is
Left: Int;
Enumerator.Gimme_My_Number(Left);
Right: Int := (Left + 1) mod 5;
begin
Philosophers: Array 0..4 of Philosopher;
task type Philosopher is
end
task body Philosopher is
begin
loop
Table.Enter
Chopsticks.Request
Chopsticks.Request
task Alice is
end
task body Alice is
begin
loop
Yard.Request;
Walk_Dog;
Yard.Release;
end loop
end Alice;