linux popen线程安全,c – popen上的死锁

我正在

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"); //

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内核?

任何指针都非常感谢!

问候,

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值