On Sun, May 24, 2009 at 02:06:49PM -0300, Casimiro de Almeida Barreto wrote:
David T. Lewis escreveu:
(...) Hi Casimiro,
I was looking into this just last night, and have gotten far enough so that the following works, and I can verify that the Unix file descriptors are properly duplicated for child processes to inherit correctly.
OSProcess command: 'mkfifo myPipe'. fd := FileStream fileNamed: 'myPipe'. OSProcess thisOSProcess redirectStdOutTo: fd. OSProcess thisOSProcess redirectStdErrTo: fd. OSProcess thisOSProcess stdOut nextPutAll: 'hello '; flush. OSProcess thisOSProcess stdErr nextPutAll: 'world!'; flush. fd next: 11 ==> 'hello world'
This requires some updates to OSProcessPlugin (a primitive for the dup2() system call) and to OSProcess. I'm not done yet, but I'll try to get this cleaned up and posted by this weekend.
Dave
Will it be available at Universes, SMPackageLoader or from specific URL?
All of the updates are now on SqueakSource in the projects OSProcessPlugin, OSProcess, and CommandShell. You will need to build your own OSPP plugin to make use of this.
I also think that I have a solution to your overall problem. You want to call a long-running function using FFI, and be able to see the stdout output from that function within Squeak. You also want to run the FFI call without blocking the VM (otherwise your Squeak image will be blocked for the duration of the FFI call).
I have made updates to OSProcessPlugin, OSProcess, and CommandShell that permit this to be done. The changes add support for the dup2() system call to OSPP, for redirection of the standard IO streams for a running Squeak image, and for a PipeableOSProcess proxy that forks a headless background Squeak connected to your Squeak image through OS pipes. The basic idea is to call this to run the FFI function:
PipeableOSProcess forkHeadlessSqueakAndDoThenQuit: aLongRunningCallToFFI.
Here is a complete example:
"Put your FFI call into a block. This is just an example, so replace it with your actual method that calls FFI." aLongRunningCallToFFI := [ "self myFFIFunctionCall" (1 to: 5) do: [:i | (Delay forSeconds: 2) wait. OSProcess thisOSProcess stdOut nextPutAll: 'loop ', i printString, ' with output from FFI on stdout'; nextPut: Character cr; flush]].
"Fork a child Squeak to evaluate the block without hanging up your Squeak image" child := PipeableOSProcess forkHeadlessSqueakAndDoThenQuit: aLongRunningCallToFFI. "Read the output of the child Squeak. This will be reading data from the standard output stream that your FFI function is using." watcherProcess := [20 timesRepeat: [(Delay forSeconds: 1) wait. someResponseCharactersFromChild := child upToEnd. Transcript show: someResponseCharactersFromChild]] fork. If you need to obtain the result of your FFI function call in the form of objects (as opposed to just reading text from the stdout stream), there are examples of how to do this with (using ReferenceStream to pass the objects) in the unit tests for CommandShell. See PipeableOSProcessTestCase>>testForkSqueakReadAndWriteReferenceStream for one example.
Hope this helps,
Dave
squeak-dev@lists.squeakfoundation.org