Skip to content

Instantly share code, notes, and snippets.

@mgiagante mgiagante/semaphores_6
Last active Oct 6, 2017

Embed
What would you like to do?
sem arrived[0..E - 1] = ([E] 0)
sem start_working[0..E - 1] = ([E] 0)
sem finish_task_mutex = 1
int remaining_tasks = T
int tasks_done[0..E - 1] = ([E] 0)
task tasks[0..T - 1] = ([T] generate_task())
reward rewards[0..E - 1] = ([E] null)
process Boss {
for [e = 0 to E - 1] {
p(arrived[e])
}
for [e = 0 to E - 1] {
v(start_working[e])
}
p(all_tasks_finished) // Sleep until all remaining_tasks == 0
int winner = index_with_max_value(tasks_done) // Find the winner
rewards[winner] = generate_reward() // Reward employee who finished the biggest number of tasks
}
process Employee [e = 0 to E - 1] {
v(arrived[e])
p(start_working[e])
while remaining_tasks > 0 {
int chosen_task = choose_task(tasks) // Take a task
work_on(tasks[chosen_task]) // Work on it
p(finish_task_mutex)
tasks_done[e] ++ // When done, increment tasks_done[e]
remaining_tasks -- // Decrement remaining_tasks.
if remaining_tasks == 0 // Then, if it's 0, awake the Coordinator.
v(all_tasks_finished)
}
v(finish_task_mutex)
}
}
function index_with_max_value(int_array) {
// Find the index with max value and return it.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.