我正在Linux系统下编写一个小应用程序(嵌入在ARM设备中),此程序运行两个线程。我在一个函数中使用了“popen”命令,导致第二个线程进入该函数时发生死锁。然而,第一个进入该函数的线程仍然可以正确运行。
以下是一些代码示例:
我真的感觉自己错过了很重要的东西。使用popen时,死锁是如何发生的?这个问题是来自标准libc还是Linux内核?任何提示都将不胜感激!
问候,
以下是一些代码示例:
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int sendCommand(
const std::string& command, std::string& answer)
{
FILE* fd; // File descriptor to command output
char answer_c[COMMAND_BUFFER_SIZE]; // The answer as char[]
int answerLength = 0; // The length of the answer
pthread_mutex_lock( &mutex1 );
// A probe
cout << "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" << endl;
fd = popen(command.c_str(), "r"); // <- Second thread entering is stuck here ...
if(fd <= 0)
{
cout << "couldn't popoen !" << endl;
return -1;
}
// A probe, never showed by second thread entering the function
cout << "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZzz" << endl;
// ... Omitted code ...
// Close the file descriptor
pclose(fd);
pthread_mutex_unlock( &mutex1 );
}
我真的感觉自己错过了很重要的东西。使用popen时,死锁是如何发生的?这个问题是来自标准libc还是Linux内核?任何提示都将不胜感激!
问候,