Last active
October 2, 2017 02:02
-
-
Save mgiagante/837208c9dbe5ebd23728888778419c3d to your computer and use it in GitHub Desktop.
Semaphores 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sem task_chosen[0..49] = ([50] 0) | |
sem able_to_work[0..49] = ([50] 0) | |
sem notify_completion[0..9] = ([10] 1) // Mutex array for notifying each shared task was finished by a single student. | |
sem grade_mutex = 1 | |
sem some_task_was_finished = 0 | |
task tasks[0..9] = ([10] generate_task()) | |
int students_that_finished[0..9] = ([10] 0) // Amount of students that finished working on each task. | |
int grade[0..9] // Puntaje. Es el orden en el que cada equipo 0..9 terminó de trabajar en su tarea. | |
int finished_task // Sólo la declaro. No la inicializo porque su valor inicial no importa. | |
process Student [student = 0 to 49] { | |
int my_task = choose(tasks, student) | |
v(task_chosen[student]) // Avisa que eligió una tarea. | |
p(able_to_work[student]) // Espera a que todos hayan elegido tarea para poder trabajar en la suya. | |
work_on(tasks[my_task]) // Trabaja en su tarea. | |
p(notify_completion[my_task]) | |
students_that_finished[my_task] ++ // Notifica que terminó su tarea. | |
v(notify_completion[my_task]) | |
if students_that_finished[my_task] == 5 { | |
p(grade_mutex) | |
finished_task = my_task | |
v(some_task_was_finished) | |
} | |
} | |
process Professor { | |
for [student = 0 to 49] { // Espera por cada uno hasta que señalice que eligió una tarea. | |
p(task_chosen[student]) | |
} | |
for [student = 0 to 49] { // Cuando todos eligieron, les permite empezar a trabajar. | |
v(able_to_work[student]) | |
} | |
// Una vez que empezaron a trabajar... | |
for [order_of_completion = 1 to 10] { | |
p(some_task_was_finished) | |
grade[finished_task] = order_of_completion // Asigna el orden en el que llegó a la variable que contiene el puntaje del equipo. | |
v(grade_mutex) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment