spawn/sync vs. fork/wait-pid

Started by cormullion, August 15, 2008, 08:32:21 AM

Previous topic - Next topic

cormullion

I'm not totally clear how the spawn/sync features in newLISP have affected the existing fork features that were there already (if they do).



I know that spawn/sync uses fork etc., but what are the reasons to use one rather than the other?



You can presumably use share and semaphore to communicate between spawned processes as before...?

Jeff

#1
Quote from: "cormullion"I know that spawn/sync uses fork etc., but what are the reasons to use one rather than the other?


Spawn is more straightforward to use.  In order to access the result of something evaluated within a forked process, you must first set up a shared page of ram and a semaphore to control access to it.  You fork the process and continue on, and at some point wait for the process to end and access the share.



With spawn, you pass it an expression to evaluate in a forked process and a symbol.  Later, when you want to access it, you use sync to tell newLISP to wait for the expression to finish evaluation.  Once sync returns, the symbol contains the value.  The logical flow is:


  • 1. x is an unevaluated expression

    2. y is where we want the result of evaluating x

    3. spawn(x, y)

    4. continue to do work

    5. sync

    6. y is now the result of evaluating x


Quote from: "cormullion"You can presumably use share and semaphore to communicate between spawned processes as before...?


Yes, you can.  But the idea behind spawn is to make sharing of data between processes explicit and clear, so that the logic can be followed without bending your mind into a pretzel.  With care, you can avoid using semaphores and shares in many cases using spawn.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

cormullion

#2
thanks Jeff!