home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 2.3 KB | 94 lines |
-
- package examples.computer;
-
-
- import simula.*;
- import simula.simset.* ;
- import simula.simset.simulation.*;
- import simula.random.* ;
- /**
- * The server executes jobs and, if needed, reschedules them.
- * @author Andrea Poltronieri
- * @version 1.0 12 Feb 1998
- */
-
- public class Server extends simula.simset.simulation.Process {
-
-
-
- /**
- * @param _sim The active Simulation
- */
-
- public Server (Simulation _sim){
- super(_sim);
- }
- /**
- * Executes actions for this object. Called by simulation on activate
- * @see simula.simset.simulation.Simulation
- */
-
-
-
- public void run() {
- ComputerSimulation sim = (ComputerSimulation)this.sim ;
-
-
- Job next ;
- Job first_outer ;
- simula.simset.Head tmp ;
- Uniform uniform = new Uniform (sim.seed,0,1) ;
- try {
- // Main loop
- while (true) {
- tmp=new Head() ;
- // Scan inner queue and serve jobs
- while (! sim.inner.empty()) {
- next=(Job)(sim.inner.first()) ;
- next.out() ;
-
- sim.result += (sim.inner.cardinal()) * (sim.time() - sim.lastevent);
- sim.lastevent = sim.time();
- sim.hold(sim.s) ;
-
- // Decide if the job needs another slice.
- if (uniform.draw().asReal()<=sim.q) {
- // Job not finished -- reschedule
- next.into(sim.inner) ;
- }
- else{
- sim.free+=next.req;
- tmp=new Head() ;
- // Job in inner queue finished. Select a new job from outer
- // Scan outer queue to schedule new jobs
- //System.err.println(">> Scannig outer...") ;
- while (! (sim.outer.empty())) {
-
- next=(Job)(sim.outer.first()) ;
- //System.err.println(">> Before out in outer : "+sim.outer.cardinal()) ;
- //Lang.inint() ;
- next.out() ;
- //System.err.println(">> After out in outer : "+sim.outer.cardinal()) ;
- // If the job cant be served, move it to a temporary queue.
- // Not elegant, but i can read only the first element of the queue...
- if (next.req<=sim.free) {
- next.into(sim.inner);
- sim.free-=next.req ;
- }
- else
- next.into(tmp);
- }
- sim.outer= tmp ;
-
- }
- }
- // Inner queue is now empty
- // If outer is not empty, there is jobs wich need more memory than existant.
- sim.passivate() ;
- }
- } catch (Exception e) {
- System.err.println ("Removals exception caught: " + e) ;
- System.exit(1) ;
- }
- }
- }