C++ fstream vs.iostream和mkfifo

C++ fstream vs.iostream和mkfifo,c++,fstream,fifo,C++,Fstream,Fifo,我有一个客户端和一个服务器,它们通过stdin/stdout进行通信。这很好: mkfifo-fifo0 客户端fifo0 客户端和服务器基本上都是这样的: int main() { run(cin, cout); return 0; } 现在我想调试服务器。因此,在两个不同的壳中,我想这样做: gdb—args服务器fifo1 fifo0 这是: 客户端fifo0 fifo1 我更改main以获取文件名参数。不幸的是,服务器暂停。使用gdb,我看到它在ifstream构造

我有一个客户端和一个服务器,它们通过stdin/stdout进行通信。这很好:

mkfifo-fifo0
客户端fifo0
客户端和服务器基本上都是这样的:

int main() {
  run(cin, cout);
  return 0;
}
现在我想调试服务器。因此,在两个不同的壳中,我想这样做:

gdb—args服务器fifo1 fifo0
这是:

客户端fifo0 fifo1
我更改
main
以获取文件名参数。不幸的是,服务器暂停。使用gdb,我看到它在
ifstream
构造函数中暂停,如下所示:

int main(int argc, char** argv) {
  char const fin[] = "fifo1";
  cerr << "in: '" << fin << "'\n";
  ifstream sin(fin);
  cerr << "opened\n";
  char const fout[] = "fifo0";
  cerr << "out: '" << fout << "'\n";
  ofstream sout(fout, ofstream::out | ofstream::app);
  cerr << "opened\n";
  run(sin, sout);
  return 0;
}

有人能解释一下为什么这样解决了问题吗?

@Duck:你说得对。打开FIFO块,直到每侧都有东西。在使用重定向(
|
)时,shell通常会处理该细节。

因为您可能正在使用Linux,这来自fifo手册页:
在Linux下,在阻塞模式和非阻塞模式下打开FIFO进行读写都会成功。POSIX未定义此行为。这可用于在没有读卡器的情况下打开FIFO进行写操作

您是在一起运行它们吗?我并不是完全按照您的操作,但我怀疑您只是遇到了正常的FIFO语义,即打开将阻塞,直到另一侧有读写器为止。由于shell为您重定向了它们,所以在您自己完成之前,它不会引起注意。我已将问题简化为我确定的问题。这个简单的问题更清楚吗@是的,同时在两个不同的终端。
int main(int argc, char** argv) {
  char const fout[] = "fifo0";
  cerr << "out: '" << fout << "'\n";
  ofstream sout(fout, ofstream::out | ofstream::app);
  cerr << "opened\n";

  char const fin[] = "fifo1";
  cerr << "in: '" << fin << "'\n";
  ifstream sin(fin);
  cerr << "opened\n";
  run(sin, sout);
  return 0;
}