pop up description layer
| Last modified: 20000101:000000/ | |
Laboratory Session 5
Introduction | The Event Class | Indices
The Events Module
1. Introduction
This document describes the Events module, which contains the definitions for the Event class. It is used in the Lab 5 program.
1.1 Use
To use the class of this module, include the statement
import Events
in the code of a referencing program. 2. The Event Class
What is an event? It is something happening in the computer system, and can be characterized by the time at which it happens, the type or nature of the event, and some set of parameters relating to the event.
2.1 Interface
- e=Event()
- return a new Event instance.
- e=EventCreate(time,type,p1,p2,p3,p4)
- return a new event, initialized with the supplied parameters. Note the difference between this call and the previous one, which only creates the event instance, but does not initialize the event values.
- e.time
- the simulated time at which event e happens. It is an integer, representing the number of (arbitrary) time units since the start of the simulation.
- e.type
- A string value representing the event type. It can have one of the values 'create', 'admit', 'dispatch', 'interrupt', 'iowait', 'iocomplete', and 'exit',
- e.p1/p2/p3
- parameters attached to the event. (The first parameter is usually the process number of the process affected by the event.)
- s=EventToString(e)
- return a string that represents the values of the event e
- currentTime
- an integer representing the current simulation time (in arbitrary time units)
- eventlist
- the current set of events that have yet to be simulated
- PrintEvents()
- print the set of current events (i.e., eventlist) in its string representation, as defined by EventToString
- AddEvent(e)
- Add the event e to the event list
- e=NextEvent()
- return the next event in the event list for the current time. Return None if there is no event scheduled at the current time. It is the user's responsibility to remove all events for the current time if there is more than one.
- tick()
- advance the current time one clock tick.
2.2 Implemention
We write the Event class as a module Events
"Events.py" 2.1 =<Event definitions 2.2> =class
Event: passdef
EventCreate(etime,etype,eparm1,eparm2,eparm3,eparm4): ev = Event() ev.time = etime ev.type = etype ev.p1 = eparm1 ev.p2 = eparm2 ev.p3 = eparm3 ev.p4 = eparm4 return ev
We model this with a class Event, and a creation routine.
What type of events can occur? Basically, these are drawn from the transitions in the process state model, using the five process states: created, ready, running, waiting, and terminated.
- create
- Create a new process and place in the created state.
- admit
- Admit a process to the ready queue.
- dispatch
- Move a process from the ready queue to the running state.
- interrupt
- Move a process from the running state to the ready queue.
- iowait
- Move a process from the running state to the waiting queue.
- iocomplete
- Move a process from the waiting queue to the ready queue
- exit
- Move a process to the terminated state.
Each of these events is represented by the corresponding string value, that is, as the values create, admit, dispatch, interrupt, iowait, iocomplete, and exit respectively. <Event definitions 2.3> =def
EventToString(ev): return "[" + str(ev.time) + "," + str(ev.type) + "," + \ str(ev.p1) + "," + str(ev.p2) + "," + str(ev.p3) + "]"
It will be very handy to produce a printable representation of an event. We could use the mechanism __str__ as above for <Process instance string representation >, but here's another way. A function that returns the string representation when called explicitly.
<Event definitions 2.4> =currentTime = 0def
tick(): global currentTime currentTime = currentTime + 1
What is time? Leaving aside the philosophical questions for the moment, we model time as a monotone increasing integer, initialized to zero.
<Event definitions 2.5> = We need to model an event queue. The properties of this queue is that it stores a sequence of events, along with their (simulated) time of happening. Use a list of events. We model the event list as a Python list, initially empty.
<Event definitions 2.6> =def
PrintEvents(): for e in eventlist: print EventToString(e)
<Event definitions 2.7> =def
AddEvent(newEvent): if len(eventlist) == 0: eventlist.append(newEvent) else: time = newEvent.time for i in range(len(eventlist)): checkevent = eventlist[i] if time < checkevent.time: eventlist.insert(i,newEvent) return eventlist.append(newEvent)
Add an event to the list. Events occur at a particular time, and we maintain the event list in chronological order.
<Event definitions 2.8> =def
NextEvent(): global currentTime if len(eventlist) == 0: return None event = eventlist[0] if currentTime == event.time: del eventlist[0] return event else: return None
This is the major change for Lab 4. The clock currentTime is advanced elsewhere, and here we only return an event if there is one for the current time. Return None if there is no event scheduled for the current time otherwise.
3. Indices
3.1 Files Defined by this Document
| File Name | Defined in |
|---|
| Events.py | 2.1 |
3.2 Macros Defined by this Document
3.3 Identifiers Defined by this Document
| Identifier | Defined in | Used in |
|---|
| AddEvent | 2.7 | |
| Event | 2.2 | |
| EventCreate | 2.2 | |
| EventToString | 2.3 | |
| NextEvent | 2.8 | |
| PrintEvents | 2.6 | |
| currentTime | 2.4 | |
| eventlist | 2.5 | |
| tick | 2.4 | |