29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Once the process wakes up it c<strong>an</strong> determine which of the processes cur rently waiting to use the<br />

processor should be resumed next. One way in which this c<strong>an</strong> be done is to regroup the processes in the<br />

waiting queue of the highest priority queue that has more th<strong>an</strong> one process. Thus ensuring that a<br />

different process is at the he ad of the queue each time the user defined scheduler process “goes back to<br />

sleep”.<br />

Check to see if time slicing is already being used.<br />

If not set up the a new process to sleep for 5 milliseconds<br />

<strong><strong>an</strong>d</strong> then to initiate the time slicing process.<br />

Set the process to have the highest available priority.<br />

Schedule the process for execution.<br />

Figure 31.1: The time slicing algorithm<br />

31.4.1 The source code for the example<br />

As explained above the mech<strong>an</strong>ism which allows this time slicing scheduler to work is the user of a<br />

Delay. This delay forces the user extensio ns to the scheduler to sleep. When it wakes up it selects a<br />

new process for execution using the slice method. It then goes back to sleep.<br />

This continuous sleeping <strong><strong>an</strong>d</strong> slicing cycle c<strong>an</strong> be achieved by spawning a new process (with the<br />

highest allowable priority). This process continually loops, sleeps <strong><strong>an</strong>d</strong> slices. The method which initiates<br />

this behaviour is startTimeSlicing. This method, defined in time slice process protocol, uses the<br />

algorithm in Figure 31.1. The actual <strong>Smalltalk</strong> method is presented below:<br />

startTimeSlicing<br />

"self startTimeSlicing"<br />

TimeSliceProcess notNil ifTrue: [^self].<br />

TimeSliceProcess :=<br />

[[true] whileTrue:[(Delay forMilliseconds: 5) wait.<br />

Processor slice]] newProcess.<br />

TimeSliceProcess priority: (Processor highestPriority).<br />

TimeSliceProcess resume.<br />

Of course having set up such a process we must p rovide some way of killing the process. If this is<br />

not done, then this process will always run (until the image is deleted). This is in fact the reason why the<br />

new process was stored into a variable named TimeSliceProcess. This is of course a global<br />

variable <strong><strong>an</strong>d</strong> could easily have been stored as <strong>an</strong> inst<strong>an</strong>ce variable of the ProcessorScheduler class.<br />

However, for ease of debugging it is often easier to store a process such as this in a global variable. This<br />

is because it is at a higher priority level then most user processes. If <strong>an</strong> unexpected “feature” has been<br />

introduced, the process c<strong>an</strong> be killed by sending the message terminate to the contents of the<br />

TimeSliceProcess global variable, either in a normal debugger or <strong>an</strong> emergency debugger.<br />

The stopTimeSlicing method provides a rather more m<strong>an</strong>aged way of stopping the time slicing<br />

process. This does send the terminate message to the time slicing process <strong><strong>an</strong>d</strong> then sets the global<br />

variable to nil. This method is available in the 'time slice p rocess' method category. The method<br />

definition is:<br />

stopTimeSlicing<br />

"self stopTimeSlicing"<br />

TimeSliceProcess notNil<br />

ifTrue:[TimeSliceProcess terminate. TimeSliceProcess := nil]<br />

So far we have set up the me<strong>an</strong>s by which a new, user defined pr ocess, c<strong>an</strong> interrupt lower priority<br />

processes, <strong><strong>an</strong>d</strong> alter the process which will get run next. However, what we have not done is to define<br />

how this re-ordering of processes will occur.<br />

The method slice in the method protocol process state ch<strong>an</strong>ge of the ProcessorScheduler<br />

performs this reorder. This is where the real meat of the time slicing operation occurs. The algorithm<br />

describing this method’s operation is:<br />

1. Find the first process priority queue which contains more th<strong>an</strong> one process.<br />

2. Remove the front process from this list <strong><strong>an</strong>d</strong> add it to the end of the list.<br />

265

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!