Advertisement
techno-

vvs

Oct 17th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. // Raúl Fernández Domínguez
  2. // Javier Loureiro Pérez
  3.  
  4. // Global variables
  5.  
  6. #define N 3
  7. #define CAP 7
  8. short max[N] = {2, 4, 3}; // Maximum money needed by each client
  9. short loan[N] = {0, 0, 0}; // Current amount lent to each client
  10. short cash = CAP; // Money in the bank (initially, the capital CAP)
  11.  
  12. active [N] proctype Client() {
  13. byte request;
  14.  
  15. do
  16. :: (loan[_pid] < max[_pid]) -> // Si el préstamo actual es menor que el máximo
  17.  
  18. byte max_request = max[_pid] - loan[_pid]; // Cantidad máxima que puede pedir
  19. select (request: 1.. max_request);
  20.  
  21. atomic {
  22. // Verificar si hay suficiente efectivo disponible
  23. (cash >= request) -> {
  24. cash = cash - request; // Disminuye el efectivo del banco
  25. loan[_pid] = loan[_pid] + request; // Aumenta el préstamo del cliente
  26. }
  27. }
  28.  
  29.  
  30. req: printf("C%d requests %d. Cash: %d [%d,%d,%d]\n", _pid, request, cash, loan[0], loan[1], loan[2]);
  31.  
  32. // Impresión de petición
  33. printf("C%d got the money %d. Cash: %d [%d,%d,%d]\n", _pid, request, cash, loan[0], loan[1], loan[2]);
  34.  
  35. // Verificación de consistencia del sistema
  36. assert(cash >= 0 && cash <= CAP);
  37. assert(loan[_pid] >= 0 && loan[_pid] <= max[_pid]);
  38. assert(cash + loan[0] + loan[1] + loan[2] == CAP);
  39.  
  40. :: (loan[_pid] > 0) -> // Devolver el préstamo si tiene un préstamo activo
  41. short return_amount;
  42. return_amount = loan[_pid]; // Todo el préstamo
  43.  
  44. atomic {
  45. cash = cash + return_amount; // El efectivo en el banco aumenta
  46. loan[_pid] = 0; // El préstamo del cliente se devuelve por completo
  47. }
  48.  
  49. // Impresión de devolución
  50. pay: printf("C%d returns loan %d. Cash: %d [%d,%d,%d]\n", _pid, return_amount, cash, loan[0], loan[1], loan[2]);
  51.  
  52. // Verificación consistencia del sistema
  53. assert(cash >= 0 && cash <= CAP);
  54. assert(loan[_pid] == 0);
  55. assert(cash + loan[0] + loan[1] + loan[2] == CAP);
  56. od;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement