2

我目前正在学习命名管道,并且正在尝试围绕这个概念开设一个课程。这是我的主要测试:

NamedPipe first("firstpipe"); // Stores the pipe name
NamedPipe second("secondpipe");
first.createPipe(); // Creates the pipe: mkfifo(mPipeName.c_str(), S_IRUSR | S_IWUSR)

second.createPipe();
pid = fork();
if (pid == 0)
{
    first.openPipeOut(); // my class open with O_WRONLY 
    first << "first : test of send first " << "Hello !"; // write to the file descriptor
    // My problem is here 
    std::string stringtest;
    second.openPipeIn(); // this is opening with O_RDONLY   
    second >> stringtest; // overload to read
    std::cout << stringtest << std::endl; // showing result
}
else
{
    std::string stringtest;
    first.openPipeIn(); //opening with O_RDONLY
    first >> stringtest; // overload to read
    std::cout << stringtest << std::endl; // showing result

    // my problem is here :
    second.openPipeOut(); // opening with O_WRONLY
    second << "Second test" << " Hi !"; // overload to write
}

当我不添加此测试时,测试效果很好:

std::string stringtest;
second.openPipeIn();   //this is opening with O_RDONLY   
second >> stringtest;   // overload to read
std::cout << stringtest << std::endl; // showing result

和这个

second.openPipeOut(); // opening with O_WRONLY
second << "Second test" << " Hi !"; // overload to write

如果这样做,我的程序将被阻止,甚至不会显示第一个链结果。有什么我错过的吗?

4

0 回答 0