Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package gestorerisorse;
  2.  
  3. public class Process extends Thread {
  4.  
  5. private Manager manager;
  6. private int numRic;
  7. private int m;
  8.  
  9. public int pid; // Indice ID del processo
  10.  
  11.  
  12. // dichiaro il costruttore per il Processo che prende in input il GESTORE
  13. // e il numero di richieste
  14. public Process(Manager manager, int numRic, int m, int pid) {
  15.  
  16. this.manager = manager;
  17. this.numRic = numRic;
  18. this.m = m;
  19. this.pid = pid;
  20. }
  21.  
  22.  
  23. public void run() {
  24.  
  25. int k = 0;
  26. int waitingTime = 0;
  27.  
  28. try {
  29. while (k < numRic) {
  30.  
  31. // Genero un numero random tra 0...1
  32.  
  33. double random = Math.random();
  34.  
  35. // se il processo richiede due risorse lo setto a falso
  36. // per il calcolo della probabilità a 0.5
  37.  
  38. boolean due = false;
  39.  
  40. if (random > 0.5) {
  41.  
  42. due = true;
  43. }
  44.  
  45. //TODO force the process to request only two resource (TEST MODE)
  46. due = false;
  47.  
  48. if (due) {
  49. // Prendo due Risorse
  50.  
  51. // Calcolo il tempo d'attesa
  52. long start = System.currentTimeMillis();
  53.  
  54. manager.richiestaDue(this);
  55.  
  56. waitingTime = (int) (System.currentTimeMillis() - start);
  57. System.out.println("Il processo " +this.pid +" ha ottenuto 2 risorse, con waiting time di : "+waitingTime);
  58.  
  59. k = k + 2;
  60. } else {
  61.  
  62. // Calcolo il tempo d'attesa
  63. long start = System.currentTimeMillis();
  64.  
  65. // Altrimenti ne prendo solo una
  66. manager.richiestaUno(this);
  67.  
  68. waitingTime = (int) (System.currentTimeMillis() - start);
  69. System.out.println("Il processo " +this.pid +" ha ottenuto 1 risorsa, con waiting time di : "+waitingTime);
  70. k = k + 1;
  71. }
  72.  
  73.  
  74. // Mando a dormire i processi di 50ms
  75. Thread.sleep(50);
  76.  
  77. // Chiamo il metodo per calcolare la somma di tutti i tempi di attesa
  78. this.manager.sumTime(waitingTime);
  79.  
  80.  
  81. // rilascio le risorse
  82. if (due) {
  83. // Rilascio due risorse
  84. manager.rilascioDue();
  85. } else {
  86. // Rilascio una risorsa
  87. manager.rilascioUno();
  88. }
  89.  
  90. // long endTime = System.currentTimeMillis();
  91. // long time = endTime - startTime;
  92. // System.out.println("Waiting time: " + time);
  93. // sum += time;
  94. // count += 1;
  95.  
  96.  
  97. // sta system del cazzo è sbagliata!!! e fa casino
  98.  
  99. // System.out.println(Thread.currentThread().getName() + " ha richiesto " + k + " risorse");
  100.  
  101. }
  102.  
  103. // System.out.println("Average waiting time: " + sum/count);
  104.  
  105. } catch (InterruptedException e) {
  106. // il programma si interrompe
  107. }
  108. }
  109. }
Add Comment
Please, Sign In to add comment