[squeak-dev] Running process after image save/stop/restart

Eliot Miranda eliot.miranda at gmail.com
Tue Mar 5 20:32:13 UTC 2013


On Thu, Feb 28, 2013 at 2:30 PM, Bert Freudenberg <bert at freudenbergs.de>wrote:

> On 2013-02-28, at 22:38, Louis LaBrunda <Lou at Keystone-Software.com> wrote:
>
> > If a process is running when an image is saved, is the process stopped at
> > any point in particular?
>
> The active process is stopped in the snapshot primitive (a.k.a. "image
> saving") and resumes after it on startup. All other processes are waiting
> on some semaphore anyway.
>

Nope.  No processes are stopped.  Processes are simply unable to run while
in the snapshot.  The key to understanding this is understanding the
scheduler (the Processor global, an instance of ProcessorSheduler).  It
maintains an activeProcess and a set of runnable processes, implemented as
an Array of lists of processes at the same priority.  A runnable process
(one not sent suspend or not waiting on a semaphore) is either the
activeProcess or on one of the lists.  This state is saved to the snapshot.
 At any time the scheduler's activeProcess is the first highest priority
process in the set of runnable processes.  It will only be deposed as
activeProcess when either another higher-priority process becomes runnable
or it yields or suspends or waits on a semaphore.  If it suspends or waits
on a semaphore it is removed from the set of runnable processes.  If it
yields it gets sent to the back of its run-queue and the next process in
that queue becomes the runnable process.  If it is the only runnable
process at its priority yield is a noop.

On loading the image the VM activates the activeProcess; the activeProcess
will typically be in a method that has called the snapshot primitive and
the system will continue, with the snapshot primitive answering true.  All
the other processes in the run queues are runnable but, just as before the
snapshot, can't be run because the activeProcess is still runnable.  But as
soon as the activeProcess suspends, waits or yields, one of these processes
may run.

Several processes are terminated (terminate is suspend + run unwinds) on
resuming the image.  This is done to inform the VM of additional state to
make these processes run.  For example, the Delay process needs to tell the
VM what the next delay expiry is on start-up.  The delay semaphore (the
semaphore the VM signals when the current active delay expires) is saved in
the image in the specialObjects array, so it persists across a snapshot,
but the VM doesn't persist the current delay expiry time.

So any long-running process which doesn't need to inform the VM of anything
special at start-up will just keep truckin' along, and nothing need be
done.  A snapshot really is like a fermata, a pause.  It is not some kind
of shut-down.

As a side-note what happens if one does

     [Semaphore new wait] fork.
    Processor activeProcess yield.

?

This creates a new process and adds it to the Processor's run queue.  Once
the process has been sent fork the current process doesn't reference it
since it has been popped from the current process's stack, and so the only
reference to the new process is from one of the Processors' run queues, but
it isn't running yet because the activeProcess (the one that sent fork) is
running.  Wen the activeProcess yields the new process gets to run.  Once
it has created the new semaphore it sends wait to it.  At this point it is
removed from the Processor's run-queue and added to the semaphore's queue.
 So now there is a circular reference between the process and the semaphore
and these are the only references to the process and the semaphore, and so
both get garbage collected.



>
> > If the saved image is started, is there any way the process can tell?
>
> Not the process itself. But you surely keep the process in a class
> somewhere, and the class can arrange to get notified on startup by adding
> itself to the startup list. See addToStartUpList:.
>

exactly. one could also e.g. poll OSProcess to get the process ID of the VM
process and see if that changes, but that's a horrible hack, and once in a
blue moon will fail (cuz process ids are not unique and get reused).

> I'm running a process that keeps running for a look time.  It loops with a
> > delay and in the loop gets the date and time.  If the date and time were
> > obtained just before the save, they would be old at the time of the image
> > restart and need to be refreshed.
>
>
> Delays get adjusted after resuming from snapshot. So it should just work.
>

exactly.  the snapshot will look just like a long time on the run-queue
whole the process is preempted by higher-priroity processes,


> - Bert -
>
-- 
HTH,
Eliot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20130305/d597d586/attachment.htm


More information about the Squeak-dev mailing list