1 ////////////////////////////////////////////////////////////////////////////
2 // Model procesor                 SIMLIB/C++
3 //
4 // Testovac� model (viz. skripta)
5 //
6 
7 #include "simlib.h"
8 
9 #define minut         * (60 * 1.0e3)
10 #define minuty        minut
11 #define sekund        * 1.0e3
12 #define sekundy       sekund
13 #define milisekund    * 1.0
14 
15 // doba simulace
16 const double TSIM     = 30 minut ;
17 
18 // po�ty objekt�
19 const int N_CPU       = 3;
20 const int N_DISK      = 2;
21 const int N_CHANNEL   = 1;
22 
23 const double ARRTM    = 9 sekund ;
24 const double PROCTM   = 6 sekund ;
25 const double T_SEARCH = 80 milisekund ;
26 const double T_TR     = 30 milisekund ;
27 
28 // pomocn� funkce:
Uniform(int min,int max)29 int Uniform(int min, int max) {           // bez max
30   return int( Uniform(double(min), double(max/*+1*/)) );
31 }
32 // deklarace glob�ln�ch objekt�
33 Facility  cpu[N_CPU];
34 Queue     q_cpu("q_cpu");
35 
36 Facility  disk[N_DISK];
37 
38 Facility  channel[N_CHANNEL];
39 Queue     q_channel("q_channel");
40 
41 Store     memory("Pam�",128);
42 
43 Histogram table("Doba zpracov�n�", 0, 2 sekundy, 25);
44 
45 class Job : public Process {  // t��da po�adavk�
46   double t0;
47   double tcpu;
48   double tio;
49   double deltaT;
50   int mem;
51   int recs;
52   int d;
Behavior()53   void Behavior() {
54     t0 = Time;
55     Enter(memory,mem);
56     while(tcpu>0)
57     {
58        int i,j;
59        //do {
60 	   for(i=0; i<N_CPU; i++)
61 	      if(!cpu[i].Busy()) {
62                   Seize(cpu[i]);
63                   break;
64               }
65 	   if(i==N_CPU) {
66               Into(q_cpu);
67               Passivate();  // wait in queue
68               //Out();
69            }
70        //}while(i==N_CPU);
71        deltaT = Exponential(tio);
72        Wait(deltaT);  // in cpu
73        tcpu -= deltaT;
74        for(i=0; i<N_CPU; i++)
75          if(cpu[i].in==this) Release(cpu[i]);
76        d = Uniform(0,N_DISK);
77        Seize(disk[d]);
78        Wait(Uniform(0.0,T_SEARCH));  // search
79        //do {
80            for(j=0; j<N_CHANNEL; j++)
81                if(!channel[j].Busy()) { Seize(channel[j]); break; }
82 	   if(j==N_CHANNEL) {
83               Into(q_channel);
84               Passivate();  // wait in queue
85               //Out();
86            }
87        //}while(j==N_CHANNEL);
88        Wait(T_TR/10 + Uniform(0.0,T_TR));  // transfer
89        for(j=0; j<N_CHANNEL; j++)
90          if(channel[j].in==this) Release(channel[j]);
91        Release(disk[d]);
92     }
93     Leave(memory,mem);
94     table(Time-t0);
95   }
96 public:
Job()97   Job() {
98     tcpu = Exponential(PROCTM);
99     mem  = Uniform(20,61);
100     recs = (tcpu/1000 * Uniform(2.0,10.0)) + 1;
101     tio  = tcpu/recs;
102     Activate();
103   }
104 };
105 
106 class Generator : public Event {  // gener�tor po�adavk�
Behavior()107   void Behavior() {
108     new Job;
109     Activate(Time+Exponential(ARRTM));
110   }
111  public:
Generator()112   Generator() { Activate(); }
113 };
114 
main()115 int main() {
116   //DebugON();
117   SetOutput("procesor.out");
118   Print("PROCESOR --- model po��ta�ov�ho syst�mu\n");
119   // nastaven� spole�n�ch front
120   int i;
121   for(i=0; i<N_CPU; i++)      cpu[i].SetQueue(q_cpu);
122   for(i=0; i<N_CHANNEL; i++)  channel[i].SetQueue(q_channel);
123   Init(0,TSIM);
124   new Generator;
125   Run();
126   table.Output();
127   SIMLIB_statistics.Output();     // print run statistics
128 }
129 
130 //
131