linux - Creating named pipes in Java -
i experimenting creating named pipes using java. using linux. however, running problem writing pipe hangs.
file fifo = fifocreator.createfifopipe("fifo"); string[] command = new string[] {"cat", fifo.getabsolutepath()}; process = runtime.getruntime().exec(command); filewriter fw = new filewriter(fifo.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); bw.write(boxstring); //hangs here bw.close(); process.waitfor(); fifocreator.removefifopipe(fifo.tostring());
fifocreator:
@override public file createfifopipe(string fifoname) throws ioexception, interruptedexception { path fifopath = propertiesmanager.gettmpfilepath(fifoname); process process = null; string[] command = new string[] {"mkfifo", fifopath.tostring()}; process = runtime.getruntime().exec(command); process.waitfor(); return new file(fifopath.tostring()); } @override public file getfifopipe(string fifoname) { path fifopath = propertiesmanager.gettmpfilepath(fifoname); return new file(fifopath.tostring()); } @override public void removefifopipe(string fifoname) throws ioexception { files.delete(propertiesmanager.gettmpfilepath(fifoname)); }
i writing string consists of 1000 lines. writing 100 lines work 1000 lines doesn't.
however, if run "cat fifo" on external shell, program proceeds , writes out without hanging. strange how cat subprocess launched program doesn't work.
edit: did ps on subprocess , has status "s".
external processes have input , output need handle. otherwise, may hang, though exact point @ hang varies.
the easiest way solve issue change every occurrence of this:
process = runtime.getruntime().exec(command);
to this:
process = new processbuilder(command).inheritio().start();
runtime.exec obsolete. use processbuilder instead.
update:
inheritio() shorthand redirecting of process's input , output of parent java process. can instead redirect input, , read output yourself:
process = new processbuilder(command).redirectinput( processbuilder.redirect.inherit).start();
then need read process's output process.getinputstream().
Comments
Post a Comment