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.

31.3 Processes in <strong>Smalltalk</strong><br />

For further details on processes in <strong>Smalltalk</strong> see the appropriate system documentation, for example in<br />

VisualWorks see Chapter 8 in the <strong>Smalltalk</strong> User's Guide, Release 4.1 <strong><strong>an</strong>d</strong> pages 25 -40 of [Lalonde <strong><strong>an</strong>d</strong><br />

Pugh 1991b]. In this section we shall only provide a brief introduction to this subject.<br />

A <strong>Smalltalk</strong> process is a non -preemptive light-weight process. That i s, a <strong>Smalltalk</strong> process will run<br />

to completion unless a higher priority process attempts to gain control <strong>Smalltalk</strong> does not attempt to<br />

share the processor time amongst processes of the same priority. When the highest priority is held by<br />

multiple processes, the active process c<strong>an</strong> be bumped to the back of the line with the expression:<br />

Processoryield - otherwise it will run until it is suspended or terminated before giving up the processor.<br />

A process that has been bumped will regain control before a process of lower priority.<br />

This of course implies that every process has <strong>an</strong> associated priority. By default a process inherits the<br />

same priority as the process which spawned it. The priority of a process c<strong>an</strong> be ch<strong>an</strong>ged using the<br />

priority: message.<br />

The process which is currently being executed by the processor is termed the active process. A<br />

process c<strong>an</strong> also be waiting to use the processor or stopped, waiting for some resource. There are a<br />

number of message related to the state of a process these are:<br />

• aProcess resume. This schedules a process.<br />

• aProcess suspend. This stops the process from executing.<br />

• aProcess terminate. This unschedules the process perm<strong>an</strong>ently.<br />

There are a number of messages which a re sent directly to the process scheduler which c<strong>an</strong> be useful in<br />

working with active processes. These are:<br />

• Processor activeProcess . This returns the process which is currently executing.<br />

• Processor activePriority . This returns the priority of the active process; i.e. the<br />

priority of the process which is currently executing.<br />

• Processor terminateActivity . Perm<strong>an</strong>ently removes the active process from<br />

execution.<br />

Note that the messages are sent to a global variable Processor, which holds the inst<strong>an</strong>ce of<br />

ProcessorScheduler.<br />

For a process to be spawned from the current process there must be some way of creating a new<br />

process. This is done using one of four messages to a block. These messages are:<br />

aBlock fork<br />

aBlock forkAt:<br />

aPriority<br />

This creates <strong><strong>an</strong>d</strong> schedules a process which will execute<br />

the block. The priority of this process is inherited from the<br />

parent process.<br />

This creates <strong><strong>an</strong>d</strong> schedules a process executing the block at<br />

a specific priority.<br />

aBlock newProcess This creates a new pro cess. It does not schedule the<br />

process. The process is created with the same priority as<br />

the parent block. It c<strong>an</strong> be scheduled using the resume<br />

message.<br />

aBlock newProcessWith:<br />

<strong>an</strong>ArrayOfParameters<br />

This message creates a new process with the same priority<br />

as the parent process. An array of parameters is passed to<br />

the block.<br />

31.3.1 Semaphores<br />

Semaphores provide a (simple) me<strong>an</strong>s of synchronization between multiple processes. For example, if<br />

two processes are executing <strong><strong>an</strong>d</strong> one must not pass a certain point until the other has completed some<br />

operation, then a semaphore between the two processes c<strong>an</strong> be used as a flag to indicate to the first<br />

process that the operation has been completed.<br />

262

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

Saved successfully!

Ooh no, something went wrong!