Last active
September 27, 2017 02:35
-
-
Save mgiagante/1631170dde728a08e928ee20470f2c22 to your computer and use it in GitHub Desktop.
Semaphores 4
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
assignment assignments[40] = ([40] nil) // Inicializa las tareas en nulo. | |
error_report corrections[40] = ([40] nil) // Inicializa las correcciones en nulo. | |
boolean has_left[40] = ([40] false) // Indica si el alumno se ha ido. | |
boolean doing_assignment[40] = ([40] false) // Indica si el alumno i esta ocupado haciendo tarea. | |
sem can_do_assignment[40] = ([40] 0) | |
sem can_see_correction[40] = ([40] 0) | |
sem can_check_assignment = 0 // Alumnos esperando corrección | |
process Teacher { | |
for [i = 0 to 39] { | |
assignments[i] = homework_for(i) | |
v(can_do_assignment[i]) // Permite al alumno i trabajar en su tarea | |
} | |
while !all(has_left) { | |
p(can_check_assignment) | |
int i = 0 | |
while has_left[i] || doing_assignment[i] { // Busca al primer alumno del arreglo que esté esperando corrección | |
i++ | |
} | |
corrections[i] = check_assignment(assignments[i]) // check_assignment(), función que retorna los errores en una tarea | |
v(can_see_correction[i]) // Permite al alumno i revisar la corrección de su tarea | |
} | |
} | |
process Student [i = 0 to 39] { | |
p(can_do_assignment[i]) // Empieza a hacer la tarea cuando la maestra haya terminado de dársela. | |
while !has_left[i] { | |
doing_assignment[i] = true | |
assignments[i] = do_assignment(assignments[i]) // Hace la tarea. | |
doing_assignment[i] = false // Señaliza que ya no está haciendo la tarea. | |
v(can_check_assignment) // Indica a la maestra que puede revisar la tarea. | |
p(can_see_correction[i]) // Espera a que la maestra le entregue la corrección de su tarea. | |
if has_no_errors(corrections[i]) { | |
has_left[i] = true // Si su corrección no muestra errores, se retira. | |
} | |
else { | |
consider_errors_for_next_time(corrections[i]) | |
} | |
} | |
} | |
function all(boolean_array) { // Devuelve true si todos los elementos de boolean_array son true o false en caso contrario. | |
boolean are_all_true = true | |
for [i = 0 to size(boolean_array) - 1] { | |
are_all_true = are_all_true && boolean_array[i] | |
} | |
return are_all_true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment