C++ 编程中的管道通信与对象基础
一、命名管道
命名管道比匿名管道更复杂,它具有字符名称,在文件系统(FS)中用户可以观察到它。命名管道在进程使用完后不会自动销毁,而是在执行特定的系统调用 unlink() 时才会被删除,因此具有持久性。
以下是通过 CLI 命令演示命名管道的示例:
$ ./test > fifo_example
$ cat fifo_example
$ Child: Sending message to child!
下面是使用 C++ 代码实现命名管道通信的示例:
#include <sys/stat.h>
#include <unistd.h>
#include <array>
#include <iostream>
#include <filesystem>
#include <string_view>
using namespace std;
using namespace std::filesystem;
static string_view fifo_name = "example_fifo"; // {1}
static constexpr size_t buf_size = 64;
void write(int out_fd, string_view message) { // {2}
write(out_fd, messa
超级会员免费看
订阅专栏 解锁全文
2009

被折叠的 条评论
为什么被折叠?



