c – popen上的死锁
发布时间:2020-12-16 07:26:25 所属栏目:百科 来源:网络整理
导读:我正在 Linux(嵌入在ARM上)编写一个运行两个线程的小应用程序.我在函数中执行“popen”,这会为进入函数的第二个线程创建死锁.但是,首先进入该函数的第一个线程仍然正确运行. 这是一些代码示例: pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int se
我正在
Linux(嵌入在ARM上)编写一个运行两个线程的小应用程序.我在函数中执行“popen”,这会为进入函数的第二个线程创建死锁.但是,首先进入该函数的第一个线程仍然正确运行.
这是一些代码示例: 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内核? 任何指针都非常感谢! 问候, 解决方法
由于popen做了一个fork(它不是线程安全的),所以popen也不是线程安全的.
This question and answers可能对你有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |