2009-07-15 63 views
9

我繼承了一些代碼:進程生成WAITFOR()問題,並打開文件限制

Process p = new ProcessBuilder("/bin/chmod", "777", path).start(); 
p.waitFor(); 

基本上,是一些古老的和高度巫術基於原因在磁盤上存儲鍵/值對的文件。我真的不想進入它。

不過,我留下了一堆IO異常:

Exception :Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files 
Message: Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files 

而且一幫我的意思是在10K的領域 - 數以百萬計

給我的感覺的WAITFOR電話是停止這些從等待進程完成並退出,但我認爲chmod在文件實際關閉之前返回結果。有誰知道這是否會成爲這些例外的原因?

我的另一個傾向是,成千上萬的文件的打開和關閉在java端沒有足夠快地發生,並且還有其他事情正在發生,也許類似於某種形式的文件緩衝區不是當fw.close()被調用時被清除。

我對Java很新,這是一個地獄奇怪的,讓我難住。 (很高興該應用程序仍然以某種方式運行..後吐出一個非常大的日誌文件是)

其他人可以想到一種方法來解決這個問題,清理緩衝區或增加文件打開限制的東西,jvm可以保持自己(假設這是問題)

+0

什麼是您的目標操作系統(和版本)。看到這個:http://unix.derkeiler.com/Newsgroups/comp.unix.solaris/2007-02/msg00873.html – 2009-07-15 06:21:11

+0

debian,它似乎被清除出uname。將是最新的穩定。 – Louis 2009-07-15 21:24:58

回答

14

我認爲你正在循環中運行這些chmod命令 - 否則我不明白爲什麼你會得到這麼多的異常。您可能因爲沒有讀取衍生進程的輸出而導致死鎖。那肯定會在前ProcessBuilder,Runtime.exec()天內咬我。

更改您的代碼段以上模式:

try { 
    ProcessBuilder pb = new ProcessBuilder("/bin/chmod", "777", path);  
    pb.redirectErrorStream(true); // merge stdout, stderr of process 

    Process p = pb.start(); 
    InputStreamReader isr = new InputStreamReader(p.getInputStream()); 
    BufferedReader br = new BufferedReader(isr); 

    String lineRead; 
    while ((lineRead = br.readLine()) != null) { 
     // swallow the line, or print it out - System.out.println(lineRead); 
    } 

    int rc = p.waitFor(); 
    // TODO error handling for non-zero rc 
} 
catch (IOException e) { 
    e.printStackTrace(); // or log it, or otherwise handle it 
} 
catch (InterruptedException ie) { 
    ie.printStackTrace(); // or log it, or otherwise handle it 
} 

(來源:this site),看看有沒有什麼幫助的情況。

+0

試過這個,發生了同樣的例外 – Louis 2009-07-15 23:04:46

0

這似乎不太可能,該進程將實際上完成沒有關閉文件。這可能發生在非常大的線程中嗎?或者也許其中一些實際上並沒有完成(即它在某些情況下掛在waitFor)?

否則,我認爲你會被卡在增加打開的文件限制。假設這是一個類Unix系統,那麼「ulimit」命令可能就是你正在尋找的。

+0

設置爲無限:\ – Louis 2009-07-15 21:26:56

0

如果您使用的是JAVA 6,您還可以嘗試File對象上的新setter(用於讀取,寫入,執行)。可能會變慢,但它應該起作用。

6

感謝你們的幫助,這應該能夠解決因爲它而在別處發生的一系列奇怪現象。

使用您的(維奈)例如和流倒閉:

try{ 
    fw.close(); 

    ProcessBuilder pb = new ProcessBuilder("/bin/chmod", "777", path); 

    pb.redirectErrorStream(true); // merge stdout, stderr of process 
    p = pb.start(); 

    InputStreamReader isr = new InputStreamReader(p.getInputStream()); 
    BufferedReader br = new BufferedReader(isr); 

    String lineRead; 
    while ((lineRead = br.readLine()) != null) { 
    // swallow the line, or print it out - System.out.println(lineRead); 
    } 

} catch (Exception ioe) { 
    Logger.logException(Logger.WARN, ioe.getMessage(), ioe); 
} finally { 
    try { 
    p.waitFor();//here as there is some snipped code that was causing a different 
       // exception which stopped it from getting processed 

    //missing these was causing the mass amounts of open 'files' 
    p.getInputStream().close(); 
    p.getOutputStream().close(); 
    p.getErrorStream().close(); 

    } catch (Exception ioe) { 
    Logger.logException(Logger.WARN, ioe.getMessage(), ioe); 
    } 
} 

上心從約翰·B·馬修斯post