Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gestorerisorse;
- public class Process extends Thread {
- private Manager manager;
- private int numRic;
- private int m;
- public int pid; // Indice ID del processo
- // dichiaro il costruttore per il Processo che prende in input il GESTORE
- // e il numero di richieste
- public Process(Manager manager, int numRic, int m, int pid) {
- this.manager = manager;
- this.numRic = numRic;
- this.m = m;
- this.pid = pid;
- }
- public void run() {
- int k = 0;
- int waitingTime = 0;
- try {
- while (k < numRic) {
- // Genero un numero random tra 0...1
- double random = Math.random();
- // se il processo richiede due risorse lo setto a falso
- // per il calcolo della probabilità a 0.5
- boolean due = false;
- if (random > 0.5) {
- due = true;
- }
- //TODO force the process to request only two resource (TEST MODE)
- due = false;
- if (due) {
- // Prendo due Risorse
- // Calcolo il tempo d'attesa
- long start = System.currentTimeMillis();
- manager.richiestaDue(this);
- waitingTime = (int) (System.currentTimeMillis() - start);
- System.out.println("Il processo " +this.pid +" ha ottenuto 2 risorse, con waiting time di : "+waitingTime);
- k = k + 2;
- } else {
- // Calcolo il tempo d'attesa
- long start = System.currentTimeMillis();
- // Altrimenti ne prendo solo una
- manager.richiestaUno(this);
- waitingTime = (int) (System.currentTimeMillis() - start);
- System.out.println("Il processo " +this.pid +" ha ottenuto 1 risorsa, con waiting time di : "+waitingTime);
- k = k + 1;
- }
- // Mando a dormire i processi di 50ms
- Thread.sleep(50);
- // Chiamo il metodo per calcolare la somma di tutti i tempi di attesa
- this.manager.sumTime(waitingTime);
- // rilascio le risorse
- if (due) {
- // Rilascio due risorse
- manager.rilascioDue();
- } else {
- // Rilascio una risorsa
- manager.rilascioUno();
- }
- // long endTime = System.currentTimeMillis();
- // long time = endTime - startTime;
- // System.out.println("Waiting time: " + time);
- // sum += time;
- // count += 1;
- // sta system del cazzo è sbagliata!!! e fa casino
- // System.out.println(Thread.currentThread().getName() + " ha richiesto " + k + " risorse");
- }
- // System.out.println("Average waiting time: " + sum/count);
- } catch (InterruptedException e) {
- // il programma si interrompe
- }
- }
- }
Add Comment
Please, Sign In to add comment