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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

processes, all three processes will be suspended. When a subsequent nextPut: message is sent to the<br />

shared queue, the first process to have sent a next message will be resumed. An additional message<br />

available is the peek message. This returns the next item i n the shared queue, however unlike the<br />

next message, it does not remove it from the queue. The inst<strong>an</strong>ce creation methods for a shared queue<br />

are:<br />

SharedQueue new.<br />

SharedQueue new: <strong>an</strong>InitialSize.<br />

Both these messages return a shared queue. The three messages used with shared queues are:<br />

aSharedQueue next.<br />

aSharedQueue nextPut:<br />

aSharedQueue peek.<br />

The following passage is taken from page 36 [Lalonde <strong><strong>an</strong>d</strong> Pugh 1991b].<br />

“In general, creating new shared classes is relatively easy. A specialization of the origina l<br />

class is created with two new inst<strong>an</strong>ce variables to play the role of the two semaphores (one<br />

semaphore for co -ordinating user access <strong><strong>an</strong>d</strong> <strong>an</strong>other for mutual exclusion while executing<br />

critical code). Then, all operations with side effects are revised using the following template.<br />

If a method being revised is called aMethod, the critical section code is simply a vari<strong>an</strong>t of<br />

“super aMethod.”.”<br />

For example:<br />

aMethod: <strong>an</strong><strong>Object</strong><br />

mutualExclusionSemaphore critical: [super aMethod: <strong>an</strong><strong>Object</strong>].<br />

readingSynchronisationSemaphore signal.<br />

or<br />

aMethod<br />

| <strong>an</strong>Element |<br />

readingSynchronisationSemaphore wait.<br />

mutualExclusionSemaphore critical: [<strong>an</strong>Element := super aMethod].<br />

^<strong>an</strong>Element.<br />

31.4 A concurrent <strong>Smalltalk</strong> example<br />

This section presents <strong>an</strong> example t o show how time slicing could be done within <strong>Smalltalk</strong> using the<br />

current process scheduler (based on <strong>an</strong> example produced by Hubert Baumeister - see end of section).<br />

The basic idea behind the effective control of multiple processes within <strong>Smalltalk</strong> is the i ntroduction of<br />

a high priority process. This process wakes up every few milliseconds <strong><strong>an</strong>d</strong> regroups the processes in the<br />

waiting queue of the highest priority that has more th<strong>an</strong> one process. It then goes back to “sleep” for a<br />

few milliseconds. It is thus time slicing between the various processes at the lower priority.<br />

The approach taken in this section is that if it is necessary to develop a preemptive scheduler (that is<br />

one in which the processor time is shared between a given number of processes, rather th<strong>an</strong> dedicated to<br />

a single process), then this c<strong>an</strong> be done by using the facilities provided by the st<strong><strong>an</strong>d</strong>ard scheduler. In<br />

effect this results in two schedulers. One, is the system scheduler, the other is a user defined scheduler.<br />

The system scheduler is still used to actually enable a process to execute. For example, it is still this<br />

scheduler which c<strong>an</strong> suspend a process when a signal is sent to a semaphore.<br />

The user defined scheduler is used to m<strong>an</strong>age the queues of user processes waiting to be h<strong><strong>an</strong>d</strong>ed to<br />

the system scheduler. These queues are controlled by the user scheduler when ever it “wakes up”. Of<br />

course, in order for it to gain control of the processor it MUST HAVE a HIGHER PRIORITY th<strong>an</strong> all<br />

other user processes. It c<strong>an</strong> then interrupt the user processes <strong><strong>an</strong>d</strong> select which process to initiate.<br />

The key to the user defined scheduler is that it “wakes up” every few milliseconds. This c<strong>an</strong> be<br />

simply <strong><strong>an</strong>d</strong> easily achieved using a Delay. This will send the process to sleep for the period of the<br />

delay. Actually, it wi ll cause the process to suspend until the timer associated with the delay period<br />

sends the process a resume. It is this resume which will cause it to take control of the processor again.<br />

264

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

Saved successfully!

Ooh no, something went wrong!