Friday, June 24, 2011

FreeBSD Processes getting stuck in "pipewr" state

I have one FreeBSD installed server. I installed JRE on it for my java development. I wrote a simple java program that will call some freeBSD scripts on the server. So, my java code was looking as below-

+++++++++++++++++++++++++++++++++++++++++++++++++++

String [] cmd = {"/bin/sh", "-c", "someScript"};
Process proc = Runtime.getRuntime.exec(cmd);
+++++++++++++++++++++++++++++++++++++++++++++++++++

so, when I ran "top" command on the console of my freeBSD server, then I got following output
===========================================================================
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
34200 mahak 1 118 0 37172K 33320K pipewr 5 70.4H 100.00% someScript
82189 jaibhi 32 72 0 957M 11988K CPU2 7 0:00 100.00% java
60975 root 1 -8 0 5176K 3244K CPU1 4 22:44 0.98% find
===========================================================================

as you can see, "pipewr" in italics..is the state of the process. someScript got stuck in "pipewr" state for long time. This is because my Java process is forking another process i.e "someScript". And someScript when executed directly then it prints some output on console. So, when someScript is forked from a java process, then a pipe is created between java and someScript. Since, someScript is trying to write back the output to this pipe, it doesn't find proper stream or redirection for writing its output.

so, there are two solutions to this problem-
1. Modify the "String [] cmd = {"/bin/sh", "-c", "someScript"};" line of code to "String [] cmd = {"/bin/sh", "-c", "someScript > tempFile "};".
By doing this we are redirecting the output of someScript to a tempFile. So, someScript will never ever stuck in "pipewr" state
2. create java IO streams to read the output given by execution of someScript.

No comments:

Post a Comment