gpt4 book ai didi

c++ - 当同一进程使用 FIFO 的两端时无法重新打开 FIFO

转载 作者:行者123 更新时间:2023-11-28 05:22:29 25 4
gpt4 key购买 nike

当在单个进程上使用 FIFO 时,看起来两端都打开然后关闭,不可能重用 FIFO。任何重新打开封闭端的尝试都会失败,或者返回的文件描述符无用。

是否有可能解决此问题,还是我们必须保持 FIFO 的两端打开,直到我们完全确定我们不再需要它为止?

下面是一些测试代码,显示并尝试重新打开 FIFO 的关闭写入端:

#include <iostream>
#include <cstdio>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

using namespace std;

int main(int argc, const char **argv)
{
cout << "Creating an instance of a named pipe..." << endl;
mode_t prevMask = umask(0);
if (mknod("my_pipe", S_IFIFO | 0666, 0))
return -1;
umask(prevMask);

cout << "Opening Read end..." << endl;
int fdM = open("my_pipe", O_RDONLY | O_NONBLOCK);
if (fdM == -1)
return -1;

cout << "Opening Write end..." << endl;
int fdS = open("my_pipe", O_WRONLY | O_NONBLOCK);
if (fdS == -1)
return -1;

cout << "Sending data to pipe..." << endl;
const char *data = "Hello my friend!";
ssize_t NbOfBytesWritten = write(fdS, data, strlen(data));
if (NbOfBytesWritten < 0)
return -1;
cout << "Number of bytes sent: " << NbOfBytesWritten << endl;

cout << "Closing Write end..." << endl;
if (close(fdS))
return -1;

cout << "Reopening Write end..." << endl;
fdS = open("my_pipe", O_WRONLY | O_NONBLOCK);
if (fdS == -1)
{
cout << "open() - failed("<< errno << "): " << strerror(errno) << '.';
remove("my_pipe");
return -1;
}

cout << "Sending some more data to pipe..." << endl;
data = "What's up?";
NbOfBytesWritten = write(fdS, data, strlen(data));
if (NbOfBytesWritten < 0)
return -1;
cout << "Number of bytes sent: " << NbOfBytesWritten << endl;

cout << "Reading data from pipe..." << endl;
char buff[128];
ssize_t numBytesRead = read(fdM, buff, 127);
if (NbOfBytesWritten < 0)
return -1;
buff[numBytesRead] = '\0'; // null terminate the string
cout << "Number of bytes read: " << numBytesRead << endl;
cout << "Message: " << buff << endl;

cout << "Closing Write end..." << endl;
if (close(fdS))
return -1;

cout << "Closing Read end..." << endl;
if (close(fdM))
return -1;

cout << "Deleting pipe..." << endl;
if (remove("my_pipe"))
return -1;

return 0;
}

这是输出:

Creating an instance of a named pipe...
Opening Read end...
Opening Write end...
Sending data to pipe...
Number of bytes sent: 16
Closing Write end...
Reopening Write end...
open() - failed(6): No such device or address.

我还测试了类似的代码,试图重新打开一个关闭的读端(而写端保持打开状态)。在这种情况下,open() 函数成功,但使用 open() 返回的文件描述符的 read() 函数失败并显示:发送时出现通信错误。 (70)

编辑:

我正在使用 CYGWIN。

最佳答案

您的代码在 Linux 上运行良好。我认为您遇到的问题是 CYGWIN 上的命名管道不能很好地工作并且无法遵循 POSIX 语义。参见 FIFO (named pipe) is broken on Cygwin .可能与您有同样的问题。

关于c++ - 当同一进程使用 FIFO 的两端时无法重新打开 FIFO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41194407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com