How to change a C-Node written with ei to be a port program?

Shawn Pearce spearce@REDACTED
Fri Mar 12 02:33:42 CET 2004


erlang@REDACTED wrote:
> > Set the port in binary mode and with a packet format of 3:
> > 
> > 	open_port({spawn, "myprogram"}, [binary, {packet, 3}])
> 
> I had to set {packet, 4}. With a packet length size of 3, I get a badarg in open_port/2.

Doh!  Fat fingers on my part hit the wrong key.  I was on my way out
the door and just fired off a reply without proofreading.  My apologies!

> > When you send/receive through it, use term_to_binary and binary_to_term
> > in Erlang:
> > 
> > 	port_send(Port, term_to_binary(Message)),
> > 
> > 	receive
> > 	{Port, {data, Data}} ->
> > 		Message = binary_to_term(Data)
> > 	end
> 
> I take it you meant port_command/2 to send something to the port.

Yes, I did.  Again, lack of proofreading on my part.

> One question that I wasn't sure of: Should I be choosing the
> port_command/2 and port_close/1 functions over the direct message
> sending?  The manual definitely suggests using the port_...
> functions, so I'm going with that for now.

Yes, in new code.  The BIFs are a little bit clearer than the message
is...  though some may disagree.

> I also have problems with recognising the demise of the port program.
> If I start it using the exit_status option, I get correct notification
> if I kill the program from the outside, but not if I do an exit(1)
> inside it (it's an unstable program ;).  Not a big deal though.

What OS is this on?  I haven't fully tested exit_status, but it does
seem to work most of the time.  You might want to also try eof, it
may report eof when it dies.

-- 
Shawn.

  Talk is cheap because supply always exceeds demand.



More information about the erlang-questions mailing list