[squeak-dev] Stupid Squeak tricks

David T. Lewis lewis at mail.msen.com
Sun May 24 18:56:04 UTC 2009


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 <casimiro.barreto at gmail.com>,
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





More information about the Squeak-dev mailing list