![](/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.