概述
进程间管道通信,同文件共享通信几乎完全一样(利用文件实现进程间共享数据)
唯一核心不一样的地方:
int ret = mkfifo(kSharedFile.c_str(), 0600);
测试代码
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
std::string kSharedFile = "/tmp/test.fifo";
template<class T>
class Writer {
private:
std::ofstream out;
public:
Writer(std::string& name):
out(name, std::ofstream::binary) {}
void Write(const T& data) {
out.write(reinterpret_cast<const char*>(&data), sizeof(T));
out.flush();
}
};
template<class T>
class Reader {
private:
std::ifstream in;
public:
Reader(std::string& name):
in(name, std::ofstream::binary) {}
T Read() {
T data;
in.read(reinterpret_cast<char*>(&data), sizeof(data));
if (!in) {
throw std::runtime_error("Failed to read a message");
}
return data;
}
};
struct Message {
int x, y;
};
std::ostream& operator<<(std::ostream& o, const Message& m) {
o << "(x=" << m.x << ", y=" << m.y << ")";
}
void DoWrites() {
std::vector<Message> messages {{1, 0}, {0, 1}, {1, 1}, {0, 0}};
Writer<Message> writer(kSharedFile);
for (const auto& m : messages) {
std::cout << "Write " << m << std::endl;
writer.Write(m);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void DoReads() {
Reader<Message> reader(kSharedFile);
try {
while(true) {
std::cout << "Read " << reader.Read() << std::endl;
}
} catch (const std::runtime_error& e) {
std::cout << e.what() << std::endl;
}
}
int main(int argc, char** argv) {
int ret = mkfifo(kSharedFile.c_str(), 0600);
if (!ret) {
throw std::runtime_error("Failed to create named pipe");
}
if (fork()) {
DoWrites();
} else {
DoReads();
}
}
我公司承接各类技术服务,主要聚焦于:stm32、单片机、嵌入式、QT应用开发、Web+Python+Django应用开发。欢迎合作。