2009-05-11
Runtime.exec()にてprocess.getInputStream().close()だけではだめ?
Java, Linux, Armadillo, Debian
Javaで外部スレッドを起動する時はProcessの標準入力、標準出力とエラー出力のstreamをclose()してあげないと「Too many open files」というIOExceptionが出てしまう。
また、標準出力やエラー出力が多くてstreamのバッファ容量を超えるとProcessがデッドロックしてしまうため出力が多い場合は別スレッドにして読み込んでやる必要がある。
今回Runtime.exec()で動かしているプログラムはArmadillo-9のGPIO出力プログラム。
ArmadilloのソフトウェアマニュアルについているサンプルをもとにCで作成した。
とっても簡単なもので引数で渡された値を特定ポートに出力するだけ。
標準出力にはポートの出力の値をprintfで出しているだけ。
こんだけ少ない値なら読み込まなくてもいいかと思ってただclose()するだけにした。
/** * 出力する * @param high boolean[] :0-7bit目の値 high:true low:false */ public void output(boolean[] high) { int parameter = makeParameter(high);//booleanを数値に変換する String[] parameters = { "/home/hogehoge/gpio_output"*1, Integer.toString(parameter)}; Process process = null; try { process = Runtime.getRuntime().exec(parameters); try { process.waitFor(); } catch (InterruptedException ex) { process.destroy(); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (process != null) { try { if(process.getOutputStream() != null){ process.getOutputStream().close(); } if(process.getInputStream() != null){ process.getInputStream().close(); } if(process.getErrorStream() != null){ process.getErrorStream().close(); } } catch (IOException ex1) { ex1.printStackTrace(); } } } }
ところが、これでしばらく動かしていると1つのプロセスのCPU負荷が突然80%近くになり、最終的にSignal11で落ちてしまう。
試しに100回連続でoutputメソッドを呼び50ms待ってまた100回連続でoutputメソッドを呼ぶというサンプルプログラムを作ったら1時間ほどでCPU負荷が上がりSignal11で止まってしまた。
もしかしてきちんと出力を読み込んでからclose()しないとだめ??
確かにネットで色々なサイトを調べてみたがすべてprocess.getInputStream()とprocess.getErrorStream()のデータを読み込んでからclose()している。
2,3バイト程度の出力データだし、closeすれば問題なさそうなんだけど・・・??
少なくともphoneMEではだめみたいです。
ということで以下のように別スレッドにして読み込むようにしました。
/**
* 出力する
* @param high boolean[]
*/
public void output(boolean[] high) {
ReadInputStreamThread thread = null;
ReadInputStreamThread errorThread = null;
int parameter = makeParameter(high);
String[] parameters = {
"/home/hogehoge/gpio_output", Integer.toString(parameter)};
Process process = null;
try {
process = Runtime.getRuntime().exec(parameters);
//標準出力読込スレッドの生成と起動
thread = new ReadInputStreamThread(process.getInputStream());
thread.start();
//エラー出力読込スレッドの生成と起動
errorThread = new ReadInputStreamThread(process.getErrorStream());
errorThread.start();
try {
process.waitFor();
}
catch (InterruptedException ex) {
process.destroy();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
if (thread != null) {
try {
//標準出力読込スレッドの停止待ち
thread.join();
}
catch (InterruptedException ex2) {}
}
if (errorThread != null) {
try {
//エラー出力読込スレッドの停止待ち
errorThread.join();
}
catch (InterruptedException ex3) {}
}
if (process != null) {
try {
if(process.getOutputStream() != null){
process.getOutputStream().close();
}
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
/**
* InputStreamを読んで破棄するスレッド
*/
private class ReadInputStreamThread
extends Thread {
/**読み込むInputStream*/
private InputStream in;
/**
* コンストラクタ
*/
public ReadInputStreamThread(InputStream in) {
this.in = in;
}
public void run() {
try {
while (in.read() >= 0) {
//ただひたすら読み込むだけ
}
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
if (in != null) {
try {
in.close();//inputStreamのclose
}
catch (IOException ex1) {}
}
}
}
}
改造してから上のサンプルプログラムを再び走らせましたが、3時間たった今でも順調に動いています。
これで解決したかな・・・?
時間がある時にJavaSEでも同じ現象が起きるか試してみたいと思います。
*1:GPIOの出力プログラム
- 99 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGLF_jaJP287JP316&q=getErrorStream().close()
- 80 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:en-US:official&hs=zYo&num=100&ei=kXYKSqmKLqXk6gPx58TdDg&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=Runtime+exec+Process.getInputStream&spell=1
- 40 http://www.google.co.jp/search?hl=ja&client=firefox-a&hs=wWd&rls=org.mozilla:ja:official&q=Process Java スレッド&aq=f&aqi=&aql=&oq=
- 30 http://www.google.co.jp/search?hl=ja&lr=lang_ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&hs=1z6&q=Runtime.getRuntime().exec&revid=563449231&ei=xB4JSuvJDqbu6gOPmIGpCQ&sa=X&oi=revisions_inline&resnum=0&ct=top-revision&cd=1
- 24 http://www.google.co.jp/search?hl=ja&lr=lang_ja&tbs=lr:lang_1ja&q=linux java+"/proc"&btnG=検索&aq=f&aqi=&aql=&oq=&gs_rfai=
- 22 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=io+exception+too+many+open+files+runtime&num=50
- 19 http://www.google.co.jp/search?hl=ja&source=hp&q=getinputstream+java&lr=&aq=0&oq=getInputStream
- 18 http://www.google.co.jp/search?hl=ja&source=hp&q=java+process+getinputstream&btnG=Google+検索&lr=&aq=3&oq=java+process+
- 17 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cts=1331108472122&ved=0CCwQFjAA&url=http://d.hatena.ne.jp/hidepon_mory/20090511/1242043434&ei=dBdXT8DhCcfsmAW5leG1Dw&usg=AFQjCNE9LUu1aBKAuJtCfJNNA8K9srwZsw&sig2=G25X_bHltV6CzdWek
- 16 http://d.hatena.ne.jp/n314/20101118/1290101208
