PHP Web サイトと C++ プログラムの間の非常に単純な通信を実現しようとしています。選択された解決策は、Linux fifo を利用することでした。
これは最初のコマンドではうまく機能しますが、ファイルを再度開いてみるとエラーが返されます。エラー メッセージの詳細については、次のコードを参照してください。
C++ アプリケーション:
string path = "/tmp/cmd.fifo";
__mode_t priv = 0666;
int fifo;
mkfifo(path.c_str(), priv); // returns 0, but privileges are not applied
chmod(path.c_str(), priv); // returns 0 as well, and now privileges are working
fifo = open(path, O_RDONLY); // blocks the thread
PHP:
$fifoPath = "/tmp/cmd.fifo";
$fifo = fopen($fifoPath, 'w');
fwrite($fifo, "COMMAND");
fclose($fifo);
C++ アプリケーション:
// thread is unblocked. fifo==16
char in[20];
ssize_t r = read(fifo, in, sizeof(in)); // r == 7, in == "COMMAND"
// process the command. And read again:
ssize_t r = read(fifo, in, sizeof(in)); // r = 0, so EOF
// Let's reopen it so we can wait for the next command.
// tried using close() here, no success though.
open(path.c_str(), O_RDONLY); // returns -1! strerror(errno) == "No such file or directory"
mkfifo(path.c_str(), priv); // returns -1! strerror(errno) == "File exists"
上記のコードの問題は何でしょうか? 「そのようなファイルまたはディレクトリはありません」および「ファイルが存在します」というメッセージは、非常に対立しています。
ところで: 私は、このコミュニケーションのための別の種類のソリューション、たとえば C++ ソリューションやブースト ライブラリを使用したものに対してオープンです。