matiascelasco

Dinner Philosophers problem: Passing the baton solution

Apr 20th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. # Este problema se propone en la teoría 4 del turno tarde, en la diapo 33
  2.  
  3. # Si en lugar de administrar tenedores, cada filósofo tiene un estado
  4. # (comiendo, pensando, con hambre) y debe consultar a sus dos filósofos
  5. # laterales para saber si puede comer, tendremos una solución distribuida.
  6. # ¿Se podría usar la técnica de “passing the baton” para resolverlo?.
  7.  
  8.  
  9. # los filosofos pueden estar en tres estados: pensando, hambriento o comiendo
  10. enum state[N];
  11. sem s[N] = ([N] 0);
  12. sem e = 1;
  13.  
  14. function left(i){
  15. return (i - 1) mod N;
  16. }
  17.  
  18. function right(i){
  19. return (i + 1) mod N;
  20. }
  21.  
  22. process Philosopher [i := 0 to N - 1] {
  23.  
  24. while (true){
  25.  
  26. state[i] = THINKING;
  27. pensarComoUnFilosofo();
  28.  
  29. P(e);
  30. if (state[left(i)] == EATING or state[right(i)] == EATING){
  31. state[i] = HUNGRY;
  32. V(e);
  33. P(s[i]);
  34. }
  35. state[i] = EATING;
  36. V(e);
  37.  
  38. comerComoUnCampeon();
  39.  
  40. # a partir de acá viene el código de lo que vendría a ser el SIGNAL
  41. P(e);
  42. # me fijo si el tipo que tengo a la izquierda esta esperando mi tenedor
  43. if (state[left(i)] == HUNGRY and state[left(left(i))] != EATING {
  44. V(s[left(i)]);
  45. }
  46. # me fijo si el tipo que tengo a la derecha esta esperando mi tenedor
  47. elif (state[right(i)] == HUNGRY and state[right(right(i))] != EATING {
  48. V(s[right(i)]);
  49. }
  50. else {
  51. V(e);
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment