在单个进程上使用FIFO时,看起来在两端打开后再关闭一个FIFO时,无法重用FIFO。任何重新打开关闭端的尝试都会失败,或者返回的文件描述符无用。
是否有可能解决此问题,或者我们是否必须保持FIFO的两端都打开,直到我们完全确定我们不再需要它为止?
这是一些测试代码,显示并尝试重新打开FIFO的封闭写端:
private class financeBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) financeBox.getSelectedItem();
if(selection.equals("Present value")){
//financeFinderPanel.removeAll();
presentValuePanel();
add(presentValuePanel, BorderLayout.NORTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Simple interest")){
simpleInterestPanel();
add(simpleInterestPanel, BorderLayout.SOUTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Doubling time")){
doublingTimePanel();
add(doublingTimePanel, BorderLayout.NORTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Compound interest")){
compoundInterestPanel();
add(compoundInterestPanel, BorderLayout.EAST);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Future value"))
{
futureValuePanel();
add(futureValuePanel, BorderLayout.WEST);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Choose a formula")){
setSize(200, 125);
financeFinderPanel();
add(financeFinderPanel, BorderLayout.NORTH);
NONEMessage = new JLabel("PICK A SELECTION");
}
}
}
这是输出:
if(actionCommand.equals("Calc Simple Interest")){
try{
double principal = Double.parseDouble(principalText.getText());
double yearlyRate = Double.parseDouble(yearlyRateText.getText());
double termYears = Double.parseDouble(termYearsText.getText());
double interestRate = financeFormula.simpleInterest(principal, yearlyRate, termYears);
//double interestRate = (principal * yearlyRate * termYears);
String msg = "Simple interest is: $" + dc.format(interestRate);
JOptionPane.showMessageDialog(null, msg);
simpleInterestPanel.removeAll();
remove(simpleInterestPanel);
financeFinderPanel();
add(financeFinderPanel, BorderLayout.NORTH);
pack();
}
我还测试了类似的代码,试图重新打开一个封闭的读端(当写端保持打开状态时)。在这种情况下,open()函数成功,但使用open()返回的文件描述符的read()函数失败: 发送时出现通信错误。 (70)
编辑:
我正在使用CYGWIN。
答案 0 :(得分:1)
您的代码在Linux上运行良好。我认为您遇到的问题是CYGWIN上的命名管道不能很好地工作并且无法遵循POSIX语义。见FIFO (named pipe) is broken on Cygwin。你可能遇到同样的问题。