命名管道阻塞

时间:2012-04-10 15:31:06

标签: c++ named-pipes

我目前正在学习命名管道,我正试图围绕这个概念上课。这是我的测试主要内容:

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

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

0 个答案:

没有答案