Tkgnuplot problem: fork() on win

Hi,

I just downloaded tkgnuplot, hoping I could use it to write a little
frontend for gnuplot. It stopped with the message “The fork() function
is unimplemented on this machine” (Ruby 1.6.8-8 on Win2000)
Searching the archives for an answer I found, that “fork” is in fact not
implemented on my machine. Has a solution been found or is anyone at
least still looking for one?
Is there a simple way to change a program (replace “fork” by "thread"
etc.?) so it runs on Win?
Thanks!

Ralf.

Hi,

···

From: Ralf lausianne@gmx.net
Subject: tkgnuplot problem: fork() on win
Date: Tue, 28 Jan 2003 02:37:44 +0900
Message-ID: 3e356aee$1@epflnews.epfl.ch

I just downloaded tkgnuplot, hoping I could use it to write a little
frontend for gnuplot. It stopped with the message “The fork() function
is unimplemented on this machine” (Ruby 1.6.8-8 on Win2000)
(snip)
Is there a simple way to change a program (replace “fork” by “thread”
etc.?) so it runs on Win?

Tkgnuplot creates a gnuplot process and keeps inter process
communication with the process. I’m sorry, but, to create the
process, ‘fork’ is required. Therefore, I think the trouble is
not on Tkgnuplot. If anyone tells me the method instead of
‘open3.rb’ on Win, I’ll be able to replace the part of creating
a gnuplot process by same way.

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

In article 20030128122113W.nagai@ai.kyutech.ac.jp,

···

nagai@ai.kyutech.ac.jp wrote:

Hi,

From: Ralf lausianne@gmx.net
Subject: tkgnuplot problem: fork() on win
Date: Tue, 28 Jan 2003 02:37:44 +0900
Message-ID: 3e356aee$1@epflnews.epfl.ch

I just downloaded tkgnuplot, hoping I could use it to write a little
frontend for gnuplot. It stopped with the message “The fork() function
is unimplemented on this machine” (Ruby 1.6.8-8 on Win2000)
(snip)
Is there a simple way to change a program (replace “fork” by “thread”
etc.?) so it runs on Win?

Tkgnuplot creates a gnuplot process and keeps inter process
communication with the process. I’m sorry, but, to create the
process, ‘fork’ is required. Therefore, I think the trouble is
not on Tkgnuplot. If anyone tells me the method instead of
‘open3.rb’ on Win, I’ll be able to replace the part of creating
a gnuplot process by same way.

Didn’t someone recently announce that they implemented popen3 for Windows?
(Heesob Park?)

Phil

“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid journalism’)
Senior VP, Communications
Recording Industry Association of America

Hi,

···

From: ptkwt@shell1.aracnet.com (Phil Tomson)
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Tue, 28 Jan 2003 14:40:20 +0900
Message-ID: b152lh0kc4@enews2.newsguy.com

Didn’t someone recently announce that they implemented popen3 for Windows?

Thank you for your information.
Well, Ralf, could you try the following patch?
Because I have no environment to test it.

— …/TkGnuplot.old/gnuplot.rb Thu Oct 3 10:52:39 2002
+++ ./gnuplot.rb Wed Jan 29 11:57:49 2003
@@ -2,12 +2,50 @@

gnuplot.rb : create and communicate with Gnuplot process

by Hidetoshi NAGAI nagai@ai.kyutech.ac.jp

-# Time-stamp: “2002-10-03 10:52:39 nagai”
+# Time-stamp: “03/01/29 11:57:49 nagai”

require ‘thread’

class GnuplotProcess

  • def _win32_open_process(*cmd)
  • require ‘win32_popen’
  • IO.win32_popen4(cmd.join(’ '))
  • end
  • def _open_process(*cmd)
  • to_cmd_r, to_cmd_w = IO.pipe
  • from_cmd_r, from_cmd_w = IO.pipe
  • create gnuplot process

  • begin
  •  Process.waitpid fork {
    
  •    fork {
    
  •      to_cmd_w.close
    
  •      from_cmd_r.close
    
  •      STDIN.reopen(to_cmd_r)
    
  •      STDOUT.reopen(from_cmd_w)
    
  •      STDERR.reopen(from_cmd_w)
    
  •      STDOUT.sync = true
    
  •      exec(*cmd)
    
  •    }
    
  •    exit!
    
  •  }
    
  •  to_cmd_r.close
    
  •  from_cmd_w.close
    
  •  to_cmd_w.sync = true
    
  • rescue NotImplementedError
  •  to_cmd_w, from_cmd_r = _win32_open_process(*cmd)
    
  • end
  • [to_cmd_w, from_cmd_r]
  • end
  • private :_open_process, :_win32_open_process
  • def initialize(*args)

    head arg == gnuplot command path ?

    if args[0].kind_of? String
    @@ -23,32 +61,9 @@
    params = {}
    end
  • to_cmd_r, to_cmd_w = IO.pipe
  • from_cmd_r, from_cmd_w = IO.pipe
  • create gnuplot process

  • Process.waitpid fork {
  •  fork {
    
  • to_cmd_w.close
  • from_cmd_r.close
  • STDIN.reopen(to_cmd_r)
  • STDOUT.reopen(from_cmd_w)
  • STDERR.reopen(from_cmd_w)
  • STDOUT.sync = true
  • opt =
  • params.collect{|key,value| opt << ‘-’ + key.to_s << value.to_s}
  • exec(gnuplot, *opt)
  •  }
    
  •  exit!
    
  • }
  • to_cmd_r.close
  • from_cmd_w.close
  • @to_cmd = to_cmd_w
  • @from_cmd = from_cmd_r
  • @to_cmd.sync = true
  • cmd = [gnuplot]

  • params.each{|key,value| cmd << ‘-’ + key.to_s << value.to_s}

  • @to_cmd, @from_cmd = _open_process(*cmd)

    @mutex = Mutex.new # for Thread-safe
    @mode = 0 # 0 : no plot, 2 : 2D plot, 3 : 3D plot

Hi,
thanks for the effort …
What exactly am I supposed to do with the patch? Running it gives the
following message:

C:\DOCUME~1\longwitz\LOCALS~1\Temp\rb57.tmp:1: parse error
— …/TkGnuplot.old/gnuplot.rb Thu Oct 3 10:52:39 2002
^
Completed(1)

So I replaced all the lines “+” for “-” in gnuplot.rb, but found that I
don’t have ‘win32_popen’ … installed after a quick Google search …

Now I get this:
C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in write': Invalid argument (Errno::EINVAL) from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in synchronize' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:154:in cmd' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:184:in set’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:88:in method_missing' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:420 from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419:in synchronize’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419
Completed(1)

???

Cheers, Ralf

nagai@ai.kyutech.ac.jp wrote:

Hi,

From: ptkwt@shell1.aracnet.com (Phil Tomson)
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Tue, 28 Jan 2003 14:40:20 +0900
Message-ID: b152lh0kc4@enews2.newsguy.com

Didn’t someone recently announce that they implemented popen3 for Windows?

Thank you for your information.
Well, Ralf, could you try the following patch?
Because I have no environment to test it.

— …/TkGnuplot.old/gnuplot.rb Thu Oct 3 10:52:39 2002
+++ ./gnuplot.rb Wed Jan 29 11:57:49 2003
@@ -2,12 +2,50 @@

Hi,

···

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Wed, 29 Jan 2003 18:05:11 +0900
Message-ID: 3e379486$1@epflnews.epfl.ch

Now I get this:
C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in write': Invalid argument (Errno::EINVAL) from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in synchronize' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:154:in cmd' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:184:in set’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:88:in method_missing' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:420 from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419:in synchronize’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419
Completed(1)

Hmm…
It maybe fail to start gnuplot process.
The test routine on gnuplot.rb is witten for X environment.
Therefore, if gnuplot on windows does not support ‘-geometry’ option,
fail to create a gnuplot process on line 416
( gp = GnuplotProcess.new(‘geometry’=>‘500x200’) ).

Please try the following check on irb.

irb(main):001:0> require ‘./gnuplot.rb’
irb(main):002:0> gp = GnuplotProcess.new
irb(main):003:0> gp.cmd(‘plot sin(x)’)

If you get a graph of a sin wave, you are ready to use TkGnuplot.rb.
But, it depends on win32_popen whether tkgnuplot.rb works collectry.
If there is a buffering problem, tkgnuplot.rb may have been hunged up.

Original gnuplot.rb sets sync mode of subprocess’s stdout to true

before exec ‘gnuplot’.


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

nagai@ai.kyutech.ac.jp wrote:

Now I get this:
C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in write': Invalid argument (Errno::EINVAL) from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:105:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in synchronize' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:96:in _send_cmd’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:154:in cmd' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:184:in set’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:88:in method_missing' from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:420 from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419:in synchronize’
from C:\Program Files\ruby\tkgnuplot\gnuplotP.rb:419
Completed(1)

It maybe fail to start gnuplot process.
In Gordon Millers Gnuplot.rb this works to start gnuplot:

@cmd = ‘c:\program files\design
gnuplot\gp371w32\pgnuplot.exe’ # path to my gnuplot
@gnuplot = IO::popen(@cmd, “w”)

Please try the following check on irb.

irb(main):001:0> require ‘./gnuplot.rb’
irb(main):002:0> gp = GnuplotProcess.new
irb(main):003:0> gp.cmd(‘plot sin(x)’)

same error message as before …

Original gnuplot.rb sets sync mode of subprocess’s stdout to true

before exec ‘gnuplot’.

Sorry that I can’t be of more help to fix this. It would need someone
more Ruby-literate than myself … at least “fork” seems not to be a
problem.
cheers, Ralf.

Hi,

···

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Fri, 31 Jan 2003 03:23:02 +0900
Message-ID: 3e396ad1$1@epflnews.epfl.ch

In Gordon Millers Gnuplot.rb this works to start gnuplot:
@cmd = ‘c:\program files\design
gnuplot\gp371w32\pgnuplot.exe’ # path to my gnuplot
@gnuplot = IO::popen(@cmd, “w”)

Please try the following check on irb.

irb(main):001:0> require ‘./gnuplot.rb’
irb(main):002:0> gp = GnuplotProcess.new
irb(main):003:0> gp.cmd(‘plot sin(x)’)

same error message as before …

Well, what happen when you give the gnuplot command path to 1st argument of
GnuplotProcess.new method ?

irb(main):001:0> require ‘./gnuplot.rb’
irb(main):002:0> gp = GnuplotProcess.new(‘c:\program files\design\gnuplot\gp371w32\pgnuplot.exe’)
irb(main):003:0> gp.cmd(‘plot sin(x)’)

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

nagai@ai.kyutech.ac.jp wrote:

Well, what happen when you give the gnuplot command path to 1st argument of
GnuplotProcess.new method ?

same thing.
Then I found this in our script:
gp.terminal = ‘x11 1’
Smells like Unix to me … well, commenting it out didn’t help …

Now I stepped through gnuplot.rb with the debugger, and the pipes are
open before a command is issued:
@from_cmd => #IO:0x2af0da8
@to_cmd => #IO:0x2af0cb8

but still …
C:\Program Files\ruby\tkgnuplot\rb9F.tmp:104:in write': Invalid argument (Errno::EINVAL) from C:\Program Files\ruby\tkgnuplot\rb9F.tmp:104:in _send_cmd’

Cheers, Ralf.

Hi,

···

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Fri, 31 Jan 2003 23:25:17 +0900
Message-ID: 3e3a3587$1@epflnews.epfl.ch

nagai@ai.kyutech.ac.jp wrote:

Well, what happen when you give the gnuplot command path to 1st argument of
GnuplotProcess.new method ?
same thing.
Then I found this in our script:
gp.terminal = ‘x11 1’
Smells like Unix to me … well, commenting it out didn’t help …

When only require it, the part which include “gp.terminal = ‘x11 1’”
must not be evaluated.
Did you execute “ruby gnuplot.rb”?
I want to know the result with all messages
of running the following script.
-----<gp-test.rb>-------------------------------------------------------
require ‘./gnuplot.rb’ # maybe need change the path of library
gp = GnuplotProcess.new(‘c:\program files\design\gnuplot\gp371w32\pgnuplot.exe’)
gp.cmd(‘plot sin(x)’)
slesp 5

If you can check the running process list,
please check the exsistence of gnuplot process just after executing
the second line of the script.
The information of “When is the gnuplot process lost?”
gives me hints of the problem.

If the problem depends on win32_popen,

I cannot fix the problem immediately.

Because I must finish to write two papers in this five days. :slight_smile:


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Here you go:

Code:
require ‘gnuplot.rb’ # maybe need change the path of library
gp = GnuplotProcess.new(‘c:\program
files\design\gnuplot\gp371w32\pgnuplot.exe’)
gp.cmd(‘plot sin(x)’)
sleep 5

Result:
C:/Program Files/ruby/lib/ruby/1.6/gnuplot.rb:105:in write': Invalid argument (Errno::EINVAL) from C:/Program Files/ruby/lib/ruby/1.6/gnuplot.rb:105:in _send_cmd’
from C:/Program Files/ruby/lib/ruby/1.6/gnuplot.rb:96:in synchronize' from C:/Program Files/ruby/lib/ruby/1.6/gnuplot.rb:96:in _send_cmd’
from C:/Program Files/ruby/lib/ruby/1.6/gnuplot.rb:154:in `cmd’
from C:\DOCUME~1\longwitz\LOCALS~1\Temp\rb13C.tmp:3
Completed(1)

nagai@ai.kyutech.ac.jp wrote:

···

Hi,

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Fri, 31 Jan 2003 23:25:17 +0900
Message-ID: 3e3a3587$1@epflnews.epfl.ch

nagai@ai.kyutech.ac.jp wrote:

Well, what happen when you give the gnuplot command path to 1st argument of
GnuplotProcess.new method ?

same thing.
Then I found this in our script:
gp.terminal = ‘x11 1’
Smells like Unix to me … well, commenting it out didn’t help …

When only require it, the part which include “gp.terminal = ‘x11 1’”
must not be evaluated.
Did you execute “ruby gnuplot.rb”?
I want to know the result with all messages
of running the following script.
-----<gp-test.rb>-------------------------------------------------------
require ‘./gnuplot.rb’ # maybe need change the path of library
gp = GnuplotProcess.new(‘c:\program files\design\gnuplot\gp371w32\pgnuplot.exe’)
gp.cmd(‘plot sin(x)’)
slesp 5

If you can check the running process list,
please check the exsistence of gnuplot process just after executing
the second line of the script.
The information of “When is the gnuplot process lost?”
gives me hints of the problem.

If the problem depends on win32_popen,

I cannot fix the problem immediately.

Because I must finish to write two papers in this five days. :slight_smile:


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

···

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Mon, 3 Feb 2003 18:33:26 +0900
Message-ID: 3e3e32ea$1@epflnews.epfl.ch

Here you go:

Thank you for your report.
Well, could you try the following test?

  1. replace line 71–80
    —------------------------------
    begin
    ret = ‘’
    @to_cmd.write “print ‘’\n”
    while s = @from_cmd.gets
    break if s == “\n”
    ret << s
    end
    rescue Errno::EPIPE
    raise $!,‘Lost gnuplot process (gnuplot argument error?)’
    end

    >
    v

—-------------------------------
begin
ret = ‘’
STDERR.print “before first write to the pipe\n”
@to_cmd.write “print ‘’\n”
STDERR.print “after first write to the pipe\n”
while s = @from_cmd.gets
break if s == “\n”
ret << s
end
STDERR.print “succeed first read of the pipe\n”
rescue Errno::EPIPE
raise $!,‘Lost gnuplot process (gnuplot argument error?)’
end

  1. replace line 105
    —------------------------------
    @to_cmd.write cmd.chomp + “\n”

    >
    v

—-------------------------------
STDERR.print(“before cmd write:”,cmd.chomp.inspect,“\n”)
@to_cmd.write(cmd.chomp + “\n”)
STDERR.print(“after cmd write\n”)

  1. try the test script

require ‘gnuplot.rb’ # maybe need change the path of library
STDERR.sync = true
gp = GnuplotProcess.new(‘c:\program files\design\gnuplot\gp371w32\pgnuplot.exe’)
sleep 1
gp.cmd(‘plot sin(x)’)
sleep 5

  1. please report the result of the test script


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

  1. try the test script

require ‘gnuplot.rb’ # maybe need change the path of library
STDERR.sync = true
gp = GnuplotProcess.new(‘c:\program files\design\gnuplot\gp371w32\pgnuplot.exe’)

I guess qoutes surround command path are necessary.

gp = GnuplotProcess.new('"c:\program files\design\gnuplot\gp371w32\pgnuplot.exe"')

Or, this seems better, put quotes in command for win32_popen4.

def _win32_open_process(*cmd)
require ‘win32_popen’

  • cmd.collect! {|s| /\s/ =~ s ? %{“#{s}”} : s}
    IO.win32_popen4(cmd.join(’ '))
    end
···

At Tue, 4 Feb 2003 03:52:28 +0900, nagai@ai.kyutech.ac.jp wrote:


Nobu Nakada

Hi,

···

From: nobu.nokada@softhome.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Tue, 4 Feb 2003 09:01:02 +0900
Message-ID: 200302040001.h14011L19403@sharui.nakada.kanuma.tochigi.jp

I guess qoutes surround command path are necessary.
gp = GnuplotProcess.new(‘“c:\program files\design\gnuplot\gp371w32\pgnuplot.exe”’)
Or, this seems better, put quotes in command for win32_popen4.
def _win32_open_process(*cmd)
require ‘win32_popen’

  • cmd.collect! {|s| /\s/ =~ s ? %{“#{s}”} : s}
    IO.win32_popen4(cmd.join(’ '))
    end

How foolish I am!
Ralf, could you try the test script after applying Nobu’s patch
and report the result ?

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

nagai@ai.kyutech.ac.jp wrote:

Hi,

From: nobu.nokada@softhome.net

I guess qoutes surround command path are necessary.
gp = GnuplotProcess.new(‘“c:\program
files\design\gnuplot\gp371w32\pgnuplot.exe”’)
Or, this seems better, put quotes in command for win32_popen4.
def _win32_open_process(*cmd)
require ‘win32_popen’

  • cmd.collect! {|s| /\s/ =~ s ? %{“#{s}”} : s}
    IO.win32_popen4(cmd.join(’ '))
    end

How foolish I am!
Ralf, could you try the test script after applying Nobu’s patch
and report the result ?

BINGO!
The gnuplot process is started and the print’’ command sent.
But … the appears in the gnuplot window, and …
in gnuplot.rb:initalize

 begin
   ret = ''
   @to_cmd.write "print '<OK>'\n"
   while s = @from_cmd.gets	# gets stuck here !!!
break if s == "<OK>\n"	# because <ok> never arrives.
ret << s
   end
 rescue Errno::EPIPE
   raise $!,'Lost gnuplot process (gnuplot argument error?)'
 end

So gnuplot maybe needs a parameter, so the window doesn’t open, and the output
goes from gnuplot to “stdout” ?
We’re close!
Thanks, Ralf.

Hi,

···

From: Ralf lausianne@gmx.net
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Tue, 4 Feb 2003 22:53:21 +0900
Message-ID: 3e3fc1d0$1@epflnews.epfl.ch

BINGO!
The gnuplot process is started and the print’’ command sent.
But … the appears in the gnuplot window, and …
(snip)
So gnuplot maybe needs a parameter, so the window doesn’t open, and the
output goes from gnuplot to “stdout” ?

Probably you are right.
Does the manual of your gnuplot for windows describe about that?

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

···

From: nagai@ai.kyutech.ac.jp
Subject: Re: tkgnuplot problem: fork() on win (popen3 for windows?)
Date: Wed, 5 Feb 2003 00:42:54 +0900
Message-ID: 20030205004252X.nagai@ai.kyutech.ac.jp

Does the manual of your gnuplot for windows describe about that?

I couldn’t find the way to get stdout and stderr of gnuplot for
windows. If there is no way, TkGnuplot doesn’t work on Windows.
Because TkGnuplot gets drawing commands on canvas from stdout
of gnuplot process. Of course we can get outputs of gnuplot by
files on the disk drive. But it is not a strategy of TkGnuplot.

If you use gnuplot as a drawing command generater for tkgnuplot.rb,
you don’t need to require an avility of drawing graph on the screen
to the gnuplot command. Therefore, if you have a gnuplot command
which cannot draw on the screen but can use stdin/out/err, you may
be able to let tkgnuplot.rb work by the gnuplot command.
Although I don’t know such gnuplot command working on Windows,
cygwin version of gnuplot may be able to be used in this case.

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)