Hi,
I have made a class, called CommandRunner, that I have been experiencing
some unusual behaviour with. I have lived with it for around two months
now, hoping I will stumble across a solution eventually. But alas, no.
I designed the class to provide the following functions:
- runs other programs in a child process (with exec()),
- allows each command-line argument to be explicitly passed in as
individual arguments of the CommandRunner's constructor, and
- provides IO streams to the parent for the child's standard out,
standard error, and standard in streams.
As far as I know, the existing available functions (such as Kernel.popen
and Kernel.system) can provide these, but one that does them all does
not exist. For instance, popen gives you only one IO stream for reading
the standard in AND error, and writing standard in; not seperate IO
streams for standard in and error. It also doesn't let you specify
command-line arguments seperately (according to the documentation,
atleast). If one of your arguments have spaces, then I think you will
need to quote it or something - I'm not sure.
Basically, when I run it and redirect output from the script to the
shell, I get additional output from the child process being run by
CommandRunner, when it should not be there.
To better explain, run the command_runner_shell_test.rb (file attached)
as follows, to see the wackiness:
ruby command\_runner\_shell\_test\.rb > output\.txt
cat output.txt
Hello, world! 2 times!
Hello, world! 2 times!
Hello, world! 2 times!
It should only print two times...
Increase the loop in command_runner_shell_test.rb to five times, and
watch the excess prints grow (what seems to be roughly) exponentially.
Uncomment the second block in the command_runner_shell_test.rb script,
and see it all get jumbled. More wackiness. o_O
All the weird thread and waiting for the 'R' character, trickery is so
that I can guaranty that by the time I am calling the waitpid function,
the process and PID still exist. In other words, it is possible for the
child process to be so quick that it finishes executing before waitpid()
gets called). So on a sidenote, does anyone know a cleaner way of
performing interprocess communication? Like how do I tell the forked,
child process that the parent is ready and waiting.
Another problem is sometimes it just hangs at the 'exec' line. I see the
process and command line being run from 'ps aux', but it doesn't seem to
ever exit. But one problem at a time. I suspect this one might be linked
with the above. Something to do with the file streams not being ready, I
think?
Take a look, and have fun figuring it out.
Regards,
Steven
command_runner.rb (2.75 KB)
command_runner_shell_test.rb (381 Bytes)
路路路
--
NAUTRONIX LTD
Marine Technology Solutions
Steven Wong
Undergraduate Software Engineer
Nautronix Ltd ABN 28 009 019 603
108 Marine Terrace, Fremantle, WA 6160, Australia
T +61 (0)8 9431 0000, F +61 (0)8 9430 5901, http://www.nautronix.com
E steven.wong@nautronix.com.au
T +61 (0)8 9431 0024
M +61 (0)413 332 005
--
This email is confidential and intended solely for the use of the individual to whom it is addressed.
Any views or opinions presented are solely those of the author and do not necessarily represent those of NAUTRONIX LTD.
If you are not the intended recipient, you have received this email in error and use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this email in error please contact the sender.
Although our computer systems use active virus protection software, and we take various measures to reduce the risk of viruses being transmitted in e-mail messages and attachments sent from this company, we cannot guarantee that such e-mail messages and attachments are free from viruses on receipt. It is a condition of our using e-mail to correspond with you, that any and all liability on our part arising directly or indirectly out of any virus is excluded. Please ensure that you run virus checking software on all e-mail messages and attachments before reading them.