From: Asterix Gallier Date: 2006-08-09T18:32:55+09:00 Subject: Re: Signaling Ruby from C/C++ Francis Cianfrocca wrote: > Ok, Asterix, this was driving me crazy because I know I've done it > before. > > Here's some code from an early version of EventMachine. Note that this > code is Windows-only. For Unix, you need something a little different > (socketpair). > > /*******************************************/ > > static int Pipe[2]; > > // C code called from a Ruby instance method. > // We create an instance variable of whatever object > // self is. > > int sp = _pipe (Pipe, 4096, _O_BINARY); > if (sp) { > // no pipe, handle the error somehow. > } > > char buf [100]; > snprintf (buf, sizeof(buf), "@rd = IO.new( %d, \"r\")", Pipe[0]); > rb_eval_string (buf); > > /********************************************/ > > After executing this code, the object that called it has an instance > variable named @rd which is an IO object you can select for readability > in Ruby (or use a Ruby global if you want). The Windows program can > write bytes to Pipe[1]. Obviously make sure Pipe isn't allocated on the > stack, and obviously make sure you close both sides of the pipe when > you're done with it. > > Also, be very wary that _pipe produces a half-duplex pipe in Windows! > Unlike some Unix flavors, you may not write Pipe[0] and you may not read > Pipe[1]. If you do, you'll get bizarre errors. Thank you all very much for your help and so much code examples. You helped me a lot. Especially thanks to Francis for searching this code example creating and using a pipe. If it will work it would be the perfect solution. There is just one problem with it: The call of IO.new() complains that the given FileNo is not valid and then exits with EBADF. (It's exact the code example you gave me). Is there some extra propagation of this filehandle to ruby necessary or something else? With regards Asterix -- Posted via http://www.ruby-forum.com/.