![](/img/trans.png)
[英]Calling jpegoptim and pngquant from java Process, passing image in stdin and reading result in stdout: broken pipe or hang
[英]Broken Pipe Java program writing in an external C programs stdin and reading from stdout
I am using the follwing java code to write in the stdin of a C program which basically reads from stdin adds A_A_A to the end of the line and writes it back in the stdout which the java program reaads from and throws the stdout
BufferedWriter b_stdout = new BufferedWriter(new OutputStreamWriter(fuse.process.getOutputStream()));
String strLine;
BufferedReader br = new BufferedReader(new FileReader("/home/Desktop/data1024.dat"));
while ((strLine=br.readLine())!=null)
{
b_stdout.newLine();
b_stdout.write(strLine);
b_stdout.flush();
}
data1024 文件是一個大小為 1 MB 的文本文件。 以上是在 C 程序中寫入的線程...以下是 C 程序
main() {
int rc;
int df;
int i;
char buf[32768];
rc = fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
FILE *fp;
for (i=0;i<=10000000;i++)
{
int rc=-1;
memset(buf,'\0',32768);
//rc = fread(buf,5, 1, stdin);
rc = read(fileno(stdin),buf,32768);
if (rc > 0)
{
strcat(buf,"B_B_B_B_B_B_B_B_B");
write(fileno(stdout),buf,strlen(buf));
/*fp=fopen("wroteExeB","a");
fprintf(fp,"%s",buf);
fclose(fp);*/
//printf("%s",buf);
}
}
另一個 java 線程從以下 C 程序的標准輸出中讀取
public void run(){
try{
String strLine;
int c;
BufferedReader bill_stdout = new BufferedReader(new InputStreamReader(fuse.process.getInputStream()));
BufferedWriter new_write = new BufferedWriter(new FileWriter("/homeDesktop/data89.dat"));
while ((strLine = bill_stdout.readLine()) != null) {
new_write.newLine();
new_write.write(strLine);
System.out.println("output thread "+strLine);
new_write.flush();
}
現在當我運行程序時
我經常面對 Broken:Pipe 錯誤
我可以在 C 程序中寫入一些行,因為我可以在輸入程序的標准輸出中看到它們。
有些行是來自 output 線程的 output 的一部分,但是在 C 程序中寫入的輸入線程在 17 或 18 等幾行之后給出了以下錯誤
java.io.IOException: Broken pipe
有人可以幫忙嗎...我嘗試使用FileWriter一次寫入一個字符,而不是使用BufferedWriter在這種情況下它可以工作並繼續寫入字符。
我試圖減少寫入的輸入行的大小...我嘗試停止輸入線程,以便管道可能會在它們已滿並且尚未讀取數據時中斷。 但沒有任何效果
請幫忙。 謝謝
我沒有詳細查看代碼,但是 C 代碼是否將標准輸入設置為非阻塞? 這不會讓 C 程序立即退出,導致 pipe 在您嘗試寫入時損壞嗎?
'Broken pipe' 表示您已寫入另一端已關閉的 pipe。 換句話說,你違反了你和接收者之間的應用協議。 這不僅僅是緩沖、阻塞/非阻塞模式等問題,這是一個設計錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.