/* Time Sharing Computer Using Discrete State Simulation Test Program 3 CS509 Assignment 2 Winter 1995 G. Lindstrom */ import sim.*; import random.*; class global { static CPU peruvian; static CPU asylum; static CPU lal; static job payroll; static job taxes; static job billing; static job file_save; static job doom; static int jobs = 0; // counts job processes in existence static int cpus = 0; // counts cpu objects in existence public static void main(String av[]) { scheduler.activate(payroll = new my_job("payroll", new urand(10, 15), new urand(0, 10), 4)); scheduler.activate(taxes = new my_job("taxes", new urand(20, 45), new urand(5, 15), 2)); scheduler.activate(billing = new my_job("billing", new urand(5, 35), new urand(10, 150), 3)); scheduler.activate(file_save = new my_job("file save", new urand(15, 45),new urand(30, 50), 2)); scheduler.activate(doom = new my_job("doom", new urand(5, 20), new urand(20, 30), 5)); scheduler.activate(new CPU_gen("CPU generator")); scheduler.activate(new CPU_retire("CPU killer", new urand(50, 150))); scheduler.run_simulation(); if (peruvian != null) peruvian.remove_cpu(); if (asylum != null) asylum.remove_cpu(); if (lal != null) lal.remove_cpu(); System.out.println(jobs + " jobs and " + cpus + " CPUs remaining"); System.exit(0); } static boolean has_cpu(job j, CPU cpu) { return j != null && j.running_on_CPU != null && j.running_on_CPU.equals(cpu); } static boolean is_busy(CPU cpu) { return has_cpu(payroll, cpu) || has_cpu(taxes, cpu) || has_cpu(billing, cpu) || has_cpu(file_save, cpu) || has_cpu(doom, cpu); } } class my_job extends job { urand dur_rand; urand hold_rand; int cycles; my_job(String n, urand dr, urand hr, int c) { super(n); dur_rand = dr; hold_rand = hr; cycles = c; global.jobs++; } void body() { for (int i = 0; i 0) { scheduler.hold(interval.draw()); if (try_retire(global.peruvian)) global.peruvian = null; if (try_retire(global.asylum)) global.asylum = null; if (try_retire(global.lal)) global.lal = null; } terminate(); } boolean try_retire(CPU cpu) { boolean can_retire = cpu != null && !global.is_busy(cpu) && (global.cpus>1 || global.jobs == 0); if (can_retire) { cpu.remove_cpu(); global.cpus--; System.out.println("Time "+scheduler.clock+": CPU retired; " +global.cpus+" remain(s)"); } return can_retire; } }