Files
OS/lab6/pipe_tester.cpp
2025-04-24 23:32:10 +08:00

42 lines
1.2 KiB
C++

#include <iostream>
#include <thread>
#include <fstream>
#include <chrono>
#include <syncstream>
void writer() {
std::osyncstream(std::cout) << "Writer started" << std::endl;
std::ofstream pipe_write("/dev/wendy_in");
if (!pipe_write.is_open()) {
std::cerr << "Cannot open pipe in" << std::endl;
}
std::ifstream txt("./make.txt");
if (!txt.is_open()) {
std::cerr << "Canno open make.txt" << std::endl;
}
// pipe_write << "Hello from the other side\n";
pipe_write << txt.rdbuf();
std::osyncstream(std::cout) << "Writer ended" << std::endl;
}
void reader() {
std::osyncstream(std::cout) << "Reader started" << std::endl;
std::ifstream pipe_read("/dev/wendy_out");
std::osyncstream(std::cout) << "Reader read content: " << pipe_read.rdbuf();
std::osyncstream(std::cout) << "Reader ended" << std::endl;
}
int main() {
std::thread writer_thread(writer);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::thread reader_thread(reader);
if (reader_thread.joinable()) {
reader_thread.join();
}
if (writer_thread.joinable()) {
writer_thread.join();
}
return 0;
}