lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Sean Conner once stated:
> It was thus said that the Great Jorge once stated:
> > With vanilla Lua I can do
> > 
> > 	local f = io.popen('cat /proc/cpuinfo') -- runs command
> > 	local l = f:read("*a") -- read output of command
> > 	f:close()
> > 
> > How do I get the output of a external program under nixio? It will be a
> > long running process, so i'll have to feed the filehandle to nixio.poll,
> > create an iterator, etc.
> > 
> > It will be under Linux, if that helps. I suppose I could do a
> > nixio.fork() followed by a nixio.execp(), but that gives me only the
> > pid... Where is the stdout for it?
> 
>   stdin, stdout and stderr (in fact, all open files) are inhereted by the
> child process, so what ever is currently stdin and stdout will be the same
> with the child process.  You'll also need to check nixio.pipe() and
> nixio.dup().  Something like (untested, since I don't use nixio, but it
> should be along these lines; you'll need to add appropriate error checking,
> etc):
> 
> 	read,write = nixio.pipe()
> 	child = nixio.fork()
> 	
> 	if child == 0 then	-- child process
> 	  nixio.dup(0,read)	-- set stdin of child to read end of pipe
> 	  nixio.dup(1,write)	-- set stdout of child to write end of pipe
> 	  nixio.exece('/bin/cat',{ '/proc/cpuinfo'} , nil )
> 	  -- if we get here, there's an error
> 	else	-- parent process
> 	  -- here we can read and write to the vars read,write and
> 	  -- which sends and receives data to/from the child process
> 	end

  Oops.  Forgot---you can close read and write in the child process after
the call do nixio.dup().  This is safe under Unix (due to how file
descriptors and dup()/dup2() (the actual system calls nixio.dup() uses)
work).

  -spc