Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Este problema se propone en la teoría 4 del turno tarde, en la diapo 33
- # Si en lugar de administrar tenedores, cada filósofo tiene un estado
- # (comiendo, pensando, con hambre) y debe consultar a sus dos filósofos
- # laterales para saber si puede comer, tendremos una solución distribuida.
- # ¿Se podría usar la técnica de “passing the baton” para resolverlo?.
- # los filosofos pueden estar en tres estados: pensando, hambriento o comiendo
- enum state[N];
- sem s[N] = ([N] 0);
- sem e = 1;
- function left(i){
- return (i - 1) mod N;
- }
- function right(i){
- return (i + 1) mod N;
- }
- process Philosopher [i := 0 to N - 1] {
- while (true){
- state[i] = THINKING;
- pensarComoUnFilosofo();
- P(e);
- if (state[left(i)] == EATING or state[right(i)] == EATING){
- state[i] = HUNGRY;
- V(e);
- P(s[i]);
- }
- state[i] = EATING;
- V(e);
- comerComoUnCampeon();
- # a partir de acá viene el código de lo que vendría a ser el SIGNAL
- P(e);
- # me fijo si el tipo que tengo a la izquierda esta esperando mi tenedor
- if (state[left(i)] == HUNGRY and state[left(left(i))] != EATING {
- V(s[left(i)]);
- }
- # me fijo si el tipo que tengo a la derecha esta esperando mi tenedor
- elif (state[right(i)] == HUNGRY and state[right(right(i))] != EATING {
- V(s[right(i)]);
- }
- else {
- V(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment