From: Shoichi OZAWA Date: 1997-09-18T12:07:17+09:00 Subject: [ruby-list:4394] Re: socket 小澤@日立 です。 なんとなく動くようになりました。 ただ、io.c にパッチが必要な... ipc0.rb ---------------------------------------- #!/usr/local/bin/ruby channel = ARGV.shift print "ipc0.rb: channel = ", channel, "\n" ioport = IO.new(channel, "w+") i = ioport.read(4) data = i.unpack("i").shift printf "ipc0.rb: int data = %x\n", data #ioport.write("OK") s = ioport.read(4) #ioport.write("OK") print "ipc0.rb: str = ", s, "\n" ---------------------------------------- ipc1.rb ---------------------------------------- #!/usr/local/bin/ruby require "socket" channel = Socket.socketpair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) pid = fork() if pid == nil then # child process cmd = "./ipc0.rb" arg0 = "./ipc0.rb" arg1 = channel[0].fileno exec("#{cmd} #{arg1}") fail "exec faild" end # parent process channel[0].close i = [0x12345678].pack("i") channel[1].write(i) #buf = channel[1].read(2) #print "buf = ", buf, "\n" str = "ruby" channel[1].write(str) #buf = channel[1].read(2) #print "buf = ", buf, "\n" ---------------------------------------- これで、親が ipc1.rb で、子が ipc0.rb で。 $ ./ipc1.rb ipc0.rb: channel = 3 ./ipc0.rb:8:in `read': Errno::EBADF: Bad file number from ./ipc0.rb:8 となります。(;_;) (sparc-sunos4.1.4 の場合) i386-linux の時は IO.new のところで怒られます。(T_T) そこで、IO.new に目を付けて io.c を覗いてみました。 関数 rd_fdopen の中の789行目で fcntl(fd,F_SETFD,fd >= 3); なにやら、fd が 3 以上のときにどうかするらしい。 # man fcntl 見てもよく分かりませんでした。(^^; この fcntl をコメントアウトして ruby を make したら 上のスクリプトが動くようになりました。 この fcntl を無くしてしまって良いものかどうかは 分かりません。ただ、上のスクリプトは ruby の お作法に合っていない と言うことなのでしょう。(^_^) "OK" のやりとりをするところは、うまく行かないので コメントにしてあります。(^_^;; ではでは。 -- 小澤 彰一 Keep it simple, stupid. mailto:ozawa@omika.hitachi.co.jp