繁体   English   中英

用1个程序用c ++打开2个管道

[英]Opening 2 pipes in c++ from 1 program

我有一个程序,我正在为嵌入式设备编写,我正在尝试使用管道来传递消息。 在我的程序和另一个程序之间传递消息之前,我正在构建测试代码以确保一切正常,并遇到问题。 请注意,嵌入式设备不支持c ++ 11(因此使用pthread)。

有问题的代码:

void* testSender(void *ptr) {
  std::cout << "Beginning testSender" << std::endl;
  int pipe = open("/dev/rtp10", O_WRONLY);
  if (pipe < 0) {
    std::cout << "write pipe failed to open" << std::endl;
  } else {
    std::cout << "write pipe successfully opened" << std::endl;
  }
  std::cout << "Ending testSender" << std::endl;
  return NULL;
}

void* testReceiver(void *ptr) {
  std::cout << "Beginning testReceiver" << std::endl;
  int pipe = open("/dev/rtp10", O_RDONLY);
  if (pipe < 0) {
    std::cout << "read pipe failed to open" << std::endl;
  } else {
    std::cout << "read pipe successfully opened" << std::endl;
  }
  std::cout << "Ending testReceiver" << std::endl;
  return NULL;
}

void testOpenClosePipes() {
  std::cout << "Beginning send/receive test" << std::endl;
  pthread_t sendThread, receiveThread;
  pthread_create(&sendThread, NULL, &testSender, NULL);
  pthread_create(&receiveThread, NULL, &testReceiver, NULL);
  std::cout << "waiting for send and receive test" << std::endl;
  pthread_join(receiveThread, NULL);
  pthread_join(sendThread, NULL);
  std::cout << "Done testing open send/receive" << std::endl;
}

从我的主线程调用函数testOpenClosePipes(),并在调用它后得到以下输出:

Beginning send/receive test
waiting for send and receive test
Beginning testReceiver
Beginning testSender
write pipe failed to open
Ending testSender

然后程序挂起。 我相信这是因为读取管道已经打开,然后等待发送器连接到管道,但我可能在那里错了。 请注意,如果我在启动发送线程之前启动接收线程,则结果如下:

Beginning send/receive test
waiting for send and receive test
Beginning testSender
Beginning testReceiver
read pipe failed to open
Ending testReceiver

从我到目前为止所读到的关于管道的内容来看,似乎正在发生的是两个中的一个(发送或接收)正确打开,然后保持直到管道的另一端被打开。 但是,管道的另一端无法正确打开,最终导致系统挂起,因为打开的管道在成功移动之前正在等待其连接。 我无法弄清楚为什么会发生这种情况,我希望得到帮助。

检查失败的打开呼叫的errno值怎么样?

在回顾了我的问题之后,看来问题实际上并不在于使用有问题的管道,而是/dev/rtp*为嵌入式系统的特殊应用程序打开了一个管道,实际上并不是一个可以去的管道从linux到linux。 这可以通过使用不同的管道来解决,并在尝试打开管道之前首先使用mkfifo命令创建所述管道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM