从 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
命令创建所述管道来解决。
我有一个正在为嵌入式设备编写的程序,我正在尝试使用管道来传递消息。在我开始在我的程序和另一个程序之间传递消息之前,我正在构建测试代码以确保一切正常工作,但遇到了一个问题。请注意,嵌入式设备不支持 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
命令创建所述管道来解决。