Download presentation
Presentation is loading. Please wait.
Published byReynard Goodwin Modified over 9 years ago
1
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 25 CPU Simulator
2
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.2 Operating System Concepts Simulations using a Next Event model Simulations using a next event model make use of events that change the state of the simulation at specified times. For each cycle of the simulation, the event that will occur next is retrieved from a list of events. In our simulations, events will consist of processes changing state (e.g. new-ready, ready-blocked, etc) When each event is retrieved, the simulation updates the state of the system to reflect the consequences of that event. The simulation also updates the time to match the time of occurrence of the most recently retrieved event. The consequences of an event may include adding new events that will occur in the future. Events are stored in an event queue in the order in which they will occur (each event is associated with a time of occurrence).
3
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.3 Operating System Concepts Diagram of Event queue Event A time 0 Event B time 15 Event C time 34... The first event processed will be Event A at time 0. Suppose Event A causes the creation of a new event at time 27 (e.g. event D). This event is inserted into the event queue. Event B time 15 Event D time 27... Event C time 34 The next event processed will be event B. When event B is retrieved, the time will skip to time 15.
4
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.4 Operating System Concepts No Time Travel Allowed! Events are processed in order by time. One cannot skip forward in time to process an event out of order. One cannot insert past events into the event queue. One can only add events at the current time or later into the event queue.
5
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.5 Operating System Concepts Typical Simulation Pseudocode... initialize event queue get next event while (next event exists) { handle event get next event }...
6
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.6 Operating System Concepts Code for Simulation Code for an event queue is included in nextEvent.h and nextEvent.cc. Events are stored in an event struct: struct Event { int time;//time event should take place int type;//type of event int processId;//process that event belongs to };
7
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.7 Operating System Concepts Event Types Each event is given a type. Our events will all be transitions of a process from one state to another. The type is specified by an integer identifier. Examples: (we will fill the list out in class) Eventid New - > Ready0 Question: How can we specify types in a way that makes the program more readable?
8
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.8 Operating System Concepts Creating a new event Suppose processes are represented by a struct: struct Process { int pid; int arrivalTime;... }; An event for process i in a list of processes, processList[i], could be created as follows: Event *theEvent;//a pointer to an event. theEvent = new Event;//allocate memory theEvent -> time = processList[i].arrivalTime; theEvent ->processId = processList[i].pid; theEvent->type = NEW_RDY;
9
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.9 Operating System Concepts The Event Queue The NextEventSimulator class provides access to the event queue and the current time. When an event is retrieved from the event queue, the simulator automatically updates the time of the simulation to the time of that event. When an event is inserted in the event queue: The simulator checks to make sure it is not scheduled for a time in the past. The simulator inserts the event in the appropriate position in the queue (according to time).
10
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.10 Operating System Concepts The NextEventSimulator class class NextEventSimulator { private: list * eventQueue; // queue of events, sorted by time int time; // current time of simulation public: NextEventSimulator(); // constructor ~NextEventSimulator(); // destructor bool insertEvent(Event* event); // insert event in queue Event* nextEvent(); // remove next event, skip clock forward, // and return event int getTime(); // return current time of simulation void display(); // display event queue };
11
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.11 Operating System Concepts The event loop NextEventSimulator eventQ; //initialize event queue //we will write the event loop in class.
12
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.12 Operating System Concepts Questions How would you output the current time? How would you print out the contents of the event queue? How would you insert an event in the event queue? How would you check to see if the insertion was successful?
13
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.13 Operating System Concepts Initializing the event queue We will write this code in class.
14
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.14 Operating System Concepts Handling events We will write this code in class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.