[squeak-dev] Stupid Squeak tricks

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Stupid Squeak tricks

David T. Lewis
What is the greatest number of CPU cycles that could possibly be expended on
the problem of adding 2 + 2? Have you ever wondered how to utilize the latest
multi-core hardware in the most pointless and inefficient manner possible?
Read on...

Motivated by a question from Casimiro de Almeida Barreto <[hidden email]>,
I have added dup2() support to OSProcess, and wired up OS pipes to forked Squeak
images to enable sending and receiving objects through reference streams to a
headless, transient child Squeak. Code is in the latest OSProcessPlugin, OSProcess,
and CommandShell on SqueakSource. The result of this is a mechanism for sending
objects to a forked image for remove evaluation, returning a result to the parent
Squeak image as follows:

  addTwoPlusTwoInChildSqueak
      "Fork a headless child Squeak. Send three objects to the child, and read one object
      returned from the child. The child Squeak uses the three objects to compute a result,
      which it sends back to its parent Squeak before exiting."

      "PipeableOSProcess addTwoPlusTwoInChildSqueak"

      | childBlock in out rcv op param result childProxy writer sum s inputStream |
      "Create block to be evaluated in a Squeak child process."
      childBlock := ["First set standard input to blocking. This avoids a delay to wait for
          data to be available, but note that it is dangerous to do this unless you
          are certain that you know in advance that the correct number of characters
          will be available on the input. If this is not the case, then the VM will block
          on the read and lock up the child squeak process."
          OSProcess thisOSProcess stdIn setBlocking.
          in := ReferenceStream on: OSProcess thisOSProcess stdIn.
          out := ReferenceStream on: OSProcess thisOSProcess stdOut.
          "read three objects"
          rcv := in next.
          op := in next.
          param := in next.
          "add two plus two"
          result := rcv perform: op with: param.
          "answer the result"
          out nextPut: result].
      "Fork a child Squeak that will evaluate the childBlock then exit"
      childProxy := PipeableOSProcess forkHeadlessSqueakAndDoThenQuit: childBlock.
      "Write three objects to the child Squeak process"
      writer := ReferenceStream on: childProxy pipeToInput writer.
      writer nextPut: 2.
      writer nextPut: #+.
      writer nextPut: 2.
      "Read the result object from the child Squeak process"
      s := RWBinaryOrTextStream with: childProxy pipeFromOutput upToEndOfFile.
      s reset.
      inputStream := ReferenceStream on: s.
      sum := inputStream next.
      "show result of 2 + 2 evaluated in the child Squeak"
      self inform: '2 + 2 equals ', sum asString.
      sum



Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Stupid Squeak tricks

Frank Shearar
David T. Lewis wrote:

> What is the greatest number of CPU cycles that could possibly be expended on
> the problem of adding 2 + 2? Have you ever wondered how to utilize the latest
> multi-core hardware in the most pointless and inefficient manner possible?
> Read on...
>
> Motivated by a question from Casimiro de Almeida Barreto <[hidden email]>,
> I have added dup2() support to OSProcess, and wired up OS pipes to forked Squeak
> images to enable sending and receiving objects through reference streams to a
> headless, transient child Squeak. Code is in the latest OSProcessPlugin, OSProcess,
> and CommandShell on SqueakSource. The result of this is a mechanism for sending
> objects to a forked image for remove evaluation, returning a result to the parent
> Squeak image as follows:
>  
I think that's pretty awesome!

Of course, you're really supposed to be evaluating "3 + 4", aren't you? :)

frank