pop up description layer
| Last modified: 20070911:092448/initial version |
FIT2022 AJH-2007-xx |
Laboratory Session 5
Introduction
| The Process Class
| The ProcessTable Class
| Indices
The Process Module
1. Introduction
This document describes the Process module, which
contains the definitions for two classes, Process and
ProcessTable. It is used in the Lab 5 program.
1.1 Use
To use the classes of this module, include the statement
import Process
in the code of a referencing program.
2. The Process Class
2.1 Interface
- p=Process()
- create a new Process instance, returned as
p
- p.num
- the process number of p
- p.state
- the process state of p (represented as a string:
the values created, ready, running,
waiting, and terminated are recognised)
- p.time
- The (expected) process running time of p
- p.slice
- The process run slice of p
- p.iotime
- The (expected) process time before io operations
- p.timetoio
- The time to execute before the next io operation is
started
- p.pri
- the process priority of p
- p.twait
- the accumulated process wait time of p
- p.trun
- the accumulated process run time of p
- p.history
- the process history, a list of tuples of (time,state)
pairs
- (print) p
- (print) the string representation of p
- p.tick
- update the process data for p
2.2 Implementation
"Process.py" 2.1 =
Define the Process module file. It contains
definitions for both the Process and
ProcessTable classes.
<Process definitions 2.2> =
What data defines a Process? Below is a list of the data items
we model.
- num
- The process number, a unique number
by which this process is known. It is also an index into the
process table.
- state
- The process state, or state of
the process, see below
- time
- The predicted process run time, or
remaining time the process requires to complete
execution.
- slice
- The allocated running time for
the next CPU slice allocated to this process in the Round
Robin scheduler.
- timetoio
- The time this process will execute before next issuing an i/o
operation.
- iotime
- The average time this process will execute before issuing an i/o
operation. If zero, no i/o.
- pri
- The process priority, or current
priority of the process
- twait
- The process waiting time, from
first admission to the ready queue, until fully executed (but
not including running time, note).
- trun
- The total process running
time.
- history
- the process history. This is recorded as a list of
pairs of values, the time at which the process changed
state, and the new state value. It will be used to analyse
the process scheduler behaviour.
The states of the process are defined as strings, representing
themselves. In other words, 'created', 'ready',
'running', 'waiting', 'terminated'
<Process instance initialization 2.3> =def __init__(self,pnum,pstate,ptime,pio,ppri):
self.num = pnum # an integer
self.state = pstate # this is a string
self.time = ptime # an integer
self.slice = ptime # an integer
self.iotime = pio # an integer
self.timetoio = ptime+1 # an integer
self.pri = ppri # an integer
self.twait = 0 # an integer
self.trun = 0 # an integer
self.history = [] # a sequence
We define the standard method __init__. This defines
how the values in a class get initialized.
The actual body of the method is pretty straightforward: it
just initializes the process variables to the supplied
parameters, or zero.
<Process instance string representation 2.4> =def __str__(self):
s = "(%d: %s, time:%d io:%d slice:%d wait:%d run:%d)" % \
(self.num,self.state,self.time,self.iotime,self.slice,\
self.twait,self.trun)
s = s + "\n " + str(self.history)
return s
Define the standard method, __str__, that returns a string
representation of the process instance. It's very handy if
you want to print a readable representation of the process
data. Note that we do not attempt to render the history.
<Process instance methods 2.5> =def statechange(self,t,s):
self.state = s
self.history.append((t,s))
Chunk referenced in 2.2Chunk defined in 2.5,
2.6
Record the process statechange in the history data for
subsequent analysis.
<Process instance methods 2.6> =def tick(self):
if self.state == 'running':
self.time = self.time - 1
self.slice = self.slice - 1
self.timetoio = self.timetoio - 1
self.trun = self.trun + 1
elif self.state == 'ready':
self.twait = self.twait + 1
Chunk referenced in 2.2Chunk defined in 2.5,
2.6
Update the process data. Running processes get their running
time incremented, and their time to run and time to io wait
data decremented, while ready processes get their waiting time
incremented.
3. The ProcessTable Class
3.1 Interface
- PT
- The (only) instance of the ProcessTable class.
This is created automatically, and no other instances should
be created.
- ProcessCreate(num,time,pri)
- create a new process numbered num, with the
time and pri attributes initialized to the
supplied parameters. The process state is set to
created, and the wait and running times are set to
zero.
- (print) PT
- (print) the string representation of the process table
instance PT
- tick()
- update all timing data for all processes in the process
table
3.2 Implementation
<ProcessTable definitions 3.1> =class
ProcessTable:
def __init__(self):
self.pt = {}
def __str__(self):
keylist = self.pt.keys()
s = ""
for i in keylist:
s = s + str(self.pt[i]) + "\n"
return s
PT = ProcessTable()
The second class in the Process module is the
ProcessTable class, which is used to generate a single
instance, PT. PT has only one variable,
pt, the process table itself, implemented as a hash
table indexed by process number. The process table contains
an entry for every process that ever exists in the life of the
simulation.
<ProcessTable definitions 3.2> =def
ProcessCreate(pnum,ptime,pio,ppri):
global ProcessTable
newProc = Process(pnum,'created',ptime,pio,ppri)
PT.pt[pnum] = newProc
return newProc
The process creation routine takes initial values for each of
the process data items as above, except the state value, which
is obviously 'created'. Note the assignment to the process
table variable pt which is a variable in the (sole)
ProcessTable instance PT. It is actually a hash
table assignment, but you can think of it as an array
assignment, where the elements get created as needed.
Note that process creation is defined as part of the
ProcessTable definitions, since it needs to update the
process table.
<ProcessTable definitions 3.3> =def tick():
global ProcessTable
keylist = PT.pt.keys()
for i in keylist:
PT.pt[i].tick()
return
<ProcessTable definitions 3.4> =def
ProcessTablePrint():
global ProcessTable
keylist = PT.pt.keys()
for i in keylist:
print "process",i,PT.pt[i]
return
For debugging purposes, we want to print out the process
table. Since this table is stored as a hash table, we must
first extract the set of keys and store them in the variable
keylist. The for loop then ranges over the values in
this set, and for each valid key, we print the key and a
representation of the process (see chunk <Process instance string representation 2.4> above).
4. Indices
4.1 Files Defined by this Document
| File Name |
Defined in |
| Process.py |
2.1 |
4.2 Macros Defined by this Document
| Chunk Name |
Defined in |
Used in |
| Process definitions |
2.2 |
2.1 |
| Process instance initialization |
2.3 |
2.2 |
| Process instance methods |
2.5, 2.6
|
2.2 |
| Process instance string representation |
2.4 |
2.2 |
| ProcessTable definitions |
3.1, 3.2, 3.3, 3.4
|
2.1 |
4.3 Identifiers Defined by this Document
| Identifier |
Defined in |
Used in |
| Process |
2.2 |
|
| ProcessCreate |
3.2 |
|
| ProcessTable |
3.1 |
|
| ProcessTablePrint |
3.4 |
|
Document History
| 20070911:092448 |
1.0.0 |
ajh |
initial version, developed from cse2302 |