public void InjectInQueue(missionrequest e) { // var res = _context.waitingQueues // .Include("mission1").Include("missionrequest").Where((e.collaborator.notea > 10 && this.Aff_antecedantM(e.collaborator.id) < 10)); if (e.collaborator.notea > 10 && this.Aff_antecedantM(e.collaborator.id) < 10) { this.affMreq(e.id).isconfirmed = true; _context.SaveChanges(); waitingQueue b = new waitingQueue(); b.miss = e.mission1; //b.idmission = e.idMission; b.mreq.Add(e); //b.mreq = e; _context.waitingQueues.Add(b); } }
static void Main(string[] args) { int simulationLength = 2 * 60 * 60; // simulation length is seconds int M = 20; // set mean job arrival time int P = 5; // set max number of processors int T = 20; // set mean exicution time // declare needed variables waitingQueue <Pevent> wQueue = new waitingQueue <Pevent>(5000); PriorityQueue <Pevent> pQueue = new PriorityQueue <Pevent>(5000); Random r = new Random(); Proccessor cores = new Proccessor(P); int time = 0; Job nextJob; Pevent newEvent, currentEvent; int jobnum = 0; double regJob = 0; double intJob = 0; int AvailabelCore; double waitTime = 0; Random random = new Random(); // creation of event list ( doesnt say to do this in assignment but its better and common practice for discrete event simulation // https://www.cs.cmu.edu/~music/cmsip/readings/intro-discrete-event-sim.html // Implementation of Discrete Event Simulation //Operationally, a discrete -event simulation is a chronologically nondecreasing sequence of event occurrences. //event record: //a pairing of an event with its event time //future event list (FEL) (or just event list): //a list ordered by nondecreasing simulation time (e.g., in a priority queue) //event (list) driven simulation: //simulation where time is advanced to the time given by the first event record from the event list //Requirements for support of discrete-event simulation: //maintain a future event list //enable event record creation and insertion into and deletion from event list //maintain simulation clock //(for stochastic simulations) provide utilities to generate random numbers from common probability distributions while (true) { time = time + Convert.ToInt32(Math.Round(-M * Math.Log(r.NextDouble()))); if (time > simulationLength) { break; } jobnum++; if (r.NextDouble() < 0.9) { nextJob = new Job(jobnum, T, P, random); regJob++; for (int i = 0; i < nextJob.processors; i++) { newEvent = new Pevent(nextJob, Pevent.Jobtype.Arrival, time); pQueue.Add(newEvent); } } else { nextJob = new Job(0, T, P, random); newEvent = new Pevent(nextJob, Pevent.Jobtype.IArrival, time, nextJob.processors); pQueue.Add(newEvent); intJob++; } } // TEST SCINARIO //nextJob = new Job(1, T, P,random); //nextJob.time = 30; //nextJob.processors = 5; //time = 30; //for (int i = 0; i < nextJob.processors; i++) //{ // newEvent = new Pevent(nextJob, Pevent.Jobtype.Arrival, time); // pQueue.Add(newEvent); //} //nextJob = new Job(2, T, P,random); //nextJob.time = 60; //nextJob.processors = 3; //time = 40; //for (int i = 0; i < nextJob.processors; i++) //{ // newEvent = new Pevent(nextJob, Pevent.Jobtype.Arrival, time); // pQueue.Add(newEvent); //} //nextJob = new Job(0, T, P,random); //nextJob.time = 35; //nextJob.processors = 4; //time = 45; //newEvent = new Pevent(nextJob, Pevent.Jobtype.IArrival, time); //pQueue.Add(newEvent); //nextJob = new Job(0, T, P, random); //nextJob.time = 35; //nextJob.processors = 4; //time = 60; //newEvent = new Pevent(nextJob, Pevent.Jobtype.IArrival, time); //pQueue.Add(newEvent); //nextJob = new Job(3, T, P,random); //nextJob.time = 40; //nextJob.processors = 4; //time = 115; //for (int i = 0; i < nextJob.processors; i++) //{ // newEvent = new Pevent(nextJob, Pevent.Jobtype.Arrival, time); // pQueue.Add(newEvent); //} //nextJob = new Job(4, T, P,random); //nextJob.time = 50; //nextJob.processors = 3; //time = 125; //for (int i = 0; i < nextJob.processors; i++) //{ // newEvent = new Pevent(nextJob, Pevent.Jobtype.Arrival, time); // pQueue.Add(newEvent); //} //regJob = 4; //intJob = 2; // processing of events while (!pQueue.Empty()) { currentEvent = pQueue.Front(); // get next event pQueue.Remove(); // remove it from the priority queue time = currentEvent.time; // update simulation clock Console.WriteLine(currentEvent.getTime()); // display time // proccess Arrival event if (currentEvent.type == Pevent.Jobtype.Arrival) { Console.WriteLine("arrival"); // display job type if (!cores.IsFull()) // check if all processors are active { // if not AvailabelCore = cores.NextAvailable(); // get first available processor newEvent = new Pevent(currentEvent.job, Pevent.Jobtype.Departure, time + currentEvent.job.time, AvailabelCore); // create new departure cores.Activate(AvailabelCore); // activate the core pQueue.Add(newEvent); // add departure Console.WriteLine("Job " + newEvent.job.number + " Arrives and is assigned to proccesor " + AvailabelCore); // display update } else { // if full wQueue.Add(currentEvent); // add event to waiting queue Console.WriteLine("job " + currentEvent.job.number + " Arrives and is assigned to the waitingQueue"); // display update } } // process departure event if (currentEvent.type == Pevent.Jobtype.Departure) { Console.WriteLine("departure"); // display status type cores.DeActivate(currentEvent.processor); // deactivate processor Console.WriteLine("Job " + currentEvent.job.number + " has finished exicution on proccessor " + currentEvent.processor); if (!wQueue.IsEmpty()) // if a event is in waiting queue { waitTime = waitTime + currentEvent.time - wQueue.Front().time; // caculate how long it waited and add it to total wait time newEvent = new Pevent(wQueue.Front().job, Pevent.Jobtype.Departure, time + wQueue.Front().job.time, currentEvent.processor); // create new departure event cores.Activate(newEvent.processor); // activate processor wQueue.Remove(); // remove from waiting queue pQueue.Add(newEvent); // add departure to priority queue Console.WriteLine("Job " + newEvent.job.number + " is removed form the waiting que and assigned to proccesor " + currentEvent.processor); // update status } } // process Inturupt Event if (currentEvent.type == Pevent.Jobtype.IArrival) { Console.WriteLine("interupt"); // display event type if (cores.getStatus(currentEvent.job.processors) != Proccessor.Status.Interrupted) // if processor is exicuting a inturupt { if (cores.getStatus(currentEvent.job.processors) == Proccessor.Status.Active) // if a process is exicuting on the processor { newEvent = pQueue.RemoveItem(currentEvent); // remove the departure event Console.WriteLine("Inturupt for Proccesor " + currentEvent.job.processors + " exicuted job " + newEvent.job.number + " is placed in waiting Q"); // display status newEvent.job.time = newEvent.time - currentEvent.time; // decrease exicution time by time it has been exicuting newEvent.type = Pevent.Jobtype.Arrival; // change job type back to arrival newEvent.time = currentEvent.time; // set its arrival time to current time wQueue.Add(newEvent); // add it back to waiting queue cores.Inturupt(newEvent.processor); // make processor status inturupted newEvent = new Pevent(currentEvent.job, Pevent.Jobtype.Departure, time + currentEvent.job.time, currentEvent.job.processors); // create inturupt departure pQueue.Add(newEvent); // add it to priority queue } else // no event is being exicuted { Console.WriteLine("interupt begins on proccesor " + currentEvent.job.processors); // update status cores.Inturupt(currentEvent.processor); // set core status newEvent = new Pevent(currentEvent.job, Pevent.Jobtype.Departure, time + currentEvent.job.time, currentEvent.job.processors); // create departure pQueue.Add(newEvent); // add it to priority queue } } else // processor is exicuting a intureupt ignore the inturupt { Console.WriteLine("Inturupt for proccessor " + currentEvent.job.processors + "Ignored"); } } cores.Print(); // print cores status Console.WriteLine(); } Console.WriteLine(); Console.WriteLine(regJob + ": regular jobs arrived " + intJob + ": inturupt jobs occured"); // display number or reg jobs and inturupt jobs double totaltime = waitTime / regJob; // average waittime in seconds // convert to hours min seconds double h = Math.Floor(Convert.ToDouble(totaltime / 3600)); double m = Math.Floor(Convert.ToDouble((totaltime - (h * 3600)) / 60)); double s = Math.Floor(Convert.ToDouble(totaltime % 60)); Console.WriteLine("average wait time: " + h + ":" + m + ":" + s); // display average waittime Console.ReadLine(); }