Threads, pipes, popen, whatever

I have never before had occasion to do the following kind of problem, so
I haven’t a clue where to start. I have a C filter that accepts a
string on stdin and returns a boolean based on it’s analysis of the
string. In a ruby program I want to pass a list of strings, one at a
time, to the filter and take appropriate action based on each return
value. Could someone point me in the right direction? I have never
worked with threads or pipes before.

Albert

A simple solution seems to be this: Write a shell script like
#! /bin/sh -f
echo “$*” | your_filter

or echo “$1” | your_filter

Then, from within ruby

result = system(“your_script”, “a string”)

result is boolean

Normally you would use IO.popen but I can’t see a way to access the exit
status of the process directly. Something like this comes to mind, but I
could not convice “popen” to behave like documented, because “-” is not
treated as documented

def f( word )
result = nil

IO.popen(“-”, “r+”) do |io|
if io
# parent process
io.puts word
result = io.readlines.shift.chomp
else
# child process
puts system(“more”)
end
end

result == “true”
end

Any other hints?

robert

“Albert Wagner” alwagner@tcac.net schrieb im Newsbeitrag
news:20030224054714.04780080.alwagner@tcac.net

···

I have never before had occasion to do the following kind of problem, so
I haven’t a clue where to start. I have a C filter that accepts a
string on stdin and returns a boolean based on it’s analysis of the
string. In a ruby program I want to pass a list of strings, one at a
time, to the filter and take appropriate action based on each return
value. Could someone point me in the right direction? I have never
worked with threads or pipes before.

Albert

Thanks, Robert. I’ll give that a try.

···

On Tue, 25 Feb 2003 17:52:08 +0900 “Robert Klemme” bob.news@gmx.net wrote:

$? won’t work?

···

On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:

Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.


---- WBR, Michael Shigorin mike@altlinux.ru
------ Linux.Kiev http://www.linux.kiev.ua/

It does appear to work as advertised:

f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256

It would be nice if the status hung around as an instance variable of f
though. It would also be nice if you didn’t have to split it into
WEXITSTATUS and WTERMSIG by hand: e.g.

puts $?.exited? # WIFEXITED
puts $?.exitstatus # WEXITSTATUS
puts $?.termsig # WTERMSIG etc

But it looks like $? is actually just a Fixnum, in 1.6.8 anyway.

Regards,

Brian.

···

On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:

On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:

Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.

$? won’t work?

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030227105024.A89394@linnet.org

Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.

$? won’t work?

It does appear to work as advertised:

Thanks for the hint!

f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256

It would be nice if the status hung around as an instance variable of f
though.

Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure any
more that the value you read from $? is the correct one.

Kind regards

robert
···

On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:

On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:

Nope, $? is thread local.

···

On Thu, Feb 27, 2003 at 09:19:10PM +0900, Robert Klemme wrote:

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030227105024.A89394@linnet.org

On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:

On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:

Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.

$? won’t work?

It does appear to work as advertised:

Thanks for the hint!

f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256

It would be nice if the status hung around as an instance variable of f
though.

Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure any
more that the value you read from $? is the correct one.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

*** Rince is wagner@schizo.DAINet.de (We have Joey, we have Fun, we have Linux on a Sun)
– Seen on #Debian

“Mauricio Fernández” batsman.geo@yahoo.com schrieb im Newsbeitrag
news:20030227122555.GA406@student.ei.uni-stuttgart.de

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030227105024.A89394@linnet.org

Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.

$? won’t work?

It does appear to work as advertised:

Thanks for the hint!

f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256

It would be nice if the status hung around as an instance variable of
f
though.

Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure
any
more that the value you read from $? is the correct one.

Nope, $? is thread local.

Just out of curiosity: where is that documented? I could not find it at
http://www.ruby-lang.org/en/man-1.4/variable.html#question Thanks!

robert
···

On Thu, Feb 27, 2003 at 09:19:10PM +0900, Robert Klemme wrote:

On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:

On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:

Pickaxe p. 218.
I couldn’t locate the pertinent table in the HTML version, believe it’s
not there…

···

On Thu, Feb 27, 2003 at 10:59:36PM +0900, Robert Klemme wrote:

Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure
any
more that the value you read from $? is the correct one.

Nope, $? is thread local.

Just out of curiosity: where is that documented? I could not find it at
http://www.ruby-lang.org/en/man-1.4/variable.html#question Thanks!


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Fairlight: udp is the light margarine of tcp/ip transport protocols :slight_smile:
– Seen on #Linux