NDK下IPC问题
由于AllJoyn的join session timeout问题一直无法解决,我们怀疑AllJoyn有些内部变量没有清理干净,因此考虑将AllJoyn相关功能放到一个单独的进程中,一旦join session timeout,就重启该进程。
这就涉及到IPC问题。
因为我们写的是通用模块,需要在DTV(arm平台linux)和Android(NDK)上运行,因此就必须考虑各种IPC手段的可用性了。
一开始我考虑直接使用socket进行IPC(肯定可用),但有人说消息队列、共享内存、管道之类的也可以用。于是俺就mmap+信号量实现了一个简单的IPC类:
/*
* File: ipc.h
* Author: raozf
*
* Created on 2013年9月17日, 上午11:04
*/ #ifndef IPC_H
#define IPC_H #include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h> //semaphore name
const std::string IPC_SEM = "alljoyn_sem";
//mmap length
const size_t IPC_MMAP_LEN = * * ; /*
* helper class for IPC between ****forked**** processes.
* implemented by semaphore and mmap
*/
class IPC
{
public:
IPC():_mutex(NULL), _memory(NULL)
{
//unnamed semaphore should use sem_init() //open named semaphore, shared between processes
_mutex = sem_open(IPC_SEM.c_str(), O_CREAT, O_RDWR, );
if(_mutex == SEM_FAILED || _mutex == NULL)
{
LOGV("[ContextSharing]IPC::IPC() sem_open() failed. semaphore name:%s, %s", IPC_SEM.c_str(), strerror(errno));
return;
} /*
* http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html
*/
//create a anonymous mmap
_memory = (char*)mmap(NULL, IPC_MMAP_LEN, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, , );
if(_memory == MAP_FAILED || _memory == NULL)
{
LOGV("[ContextSharing]IPC::IPC() mmap() failed. %s", strerror(errno));
deinit();
return;
}
} ~IPC()
{
deinit();
} //send msg to other process
//header+message
// int char*
void send(const std::string& msg)
{
*((int*)_memory) = msg.length();
memcpy(_memory + sizeof(int), msg.c_str(), msg.length());
sem_post(_mutex);
} //receive from other process
std::string recv()
{
sem_wait(_mutex);//will block
return std::string(_memory + sizeof(int), (int)(_memory));
} private:
void deinit()
{
if(_mutex != NULL)
{
/* destory an unnamed semaphore, initialized by sem_init()
//sem_destroy(_mutex);
*/ // closes the named semaphore, but still existed in system kernel
sem_close(_mutex);
_mutex = NULL; //remove semaphore from system kernel(if it's reference is 0--which decremented by sem_close())
//removes the named semaphore referred to by name. The semaphore name is removed immediately.
//The semaphore is destroyed once all other processes that have the semaphore open close it.
sem_unlink(IPC_SEM.c_str());
} if(_memory != NULL)
{
munmap(_memory, IPC_MMAP_LEN);
_memory = NULL;
}
} private:
sem_t* _mutex;
char* _memory;
}; #endif /* IPC_H */
辅助类,封装一个线程专门等待信号量、接收数据:
/*
* File: ThreadedIPC.h
* Author: raozf
*
* Created on 2013年9月17日, 下午3:21
*/ #ifndef THREADEDIPC_H
#define THREADEDIPC_H #include "Common/ThreadManager.h"
#include "delegate.h"
#include "ipc.h" static const OID OID_ThreadedIPC = { 0x8f52a31f, 0x51d5, 0x462b, { 0xa9, 0x8d, 0x40, 0x70, 0xa3, 0xf6, 0xb5, 0x7f } };
class ThreadedIPC
:public CObjectRoot<CObjectMultiThreadModel>
,public IThreadClient
{
public:
PSFRESULT FinalConstruct()
{
PSFRESULT res = PSF_E_FAIL; CHK_PSFRESULT(_listen_thread.Initialize());
_listen_thread.AddTask(this, NULL); CLEANUP:
return res;
} void FinalRelease()
{
_listen_thread.Terminate();
} void Send(const std::string& msg)
{
_sharer.send(msg);
} private:
void OnExecute(void* pArg)
{
while(true)
{
_delegate_recv(_sharer.recv());
}
} void OnTerminate(void* pArg)
{
} public:
delegate::Delegate<void(std::string)> _delegate_recv; private:
CTaskWorker _listen_thread;
IPC _sharer; BEGIN_OBJECT_INTERFACE_MAP()
OBJECT_INTERFACE_ENTRY(OID_ThreadedIPC, this);
END_OBJECT_INTERFACE_MAP()
}; #endif /* THREADEDIPC_H */
这里面用到了我们自己写的delegate和thread类(先忽略之)。
但在android上运行时却报错:
IPC::IPC() sem_open() failed. semaphore name:alljoyn_sem, Function not implemented
看来信号量在NDK下是用不了的。
而且,不仅仅信号量,共享内存、消息队列在NDK下都不能用。
参见讨论:https://groups.google.com/forum/#!topic/android-ndk/FzJIsJIxCX4
http://stackoverflow.com/questions/18603267/cross-process-locking-with-android-ndk
(但管道可用?http://stackoverflow.com/questions/8471552/porting-c-code-which-uses-fork-to-android)
Android源代码中的文档说明:http://www.netmite.com/android/mydroid/1.6/bionic/libc/docs/SYSV-IPC.TXT
(该文件在Android4。3中似乎已经移出该文档)
Android does not support System V IPCs, i.e. the facilities provided by the
following standard Posix headers: <sys/sem.h> /* SysV semaphores */
<sys/shm.h> /* SysV shared memory segments */
<sys/msg.h> /* SysV message queues */
<sys/ipc.h> /* General IPC definitions */ The reason for this is due to the fact that, by design, they lead to global
kernel resource leakage.
原因就是防止内核资源泄露。
更重要的问题在于:fork()也尽量不要用。
道理很简单:我们不应该控制android的底层,这些api会造成系统的不稳定。
https://groups.google.com/forum/#!msg/android-platform/80jr-_A-9bU/nkzslcgVrfYJ
“Bear in mind that the Dalvik VM doesn't like fork() much, and goes into
conniptions if you try to do *any* work between the fork() and the
exec(). ”
https://groups.google.com/forum/#!topic/android-ndk/FzJIsJIxCX4
悲催!
----------------------------
ps:学到了一个命令“ipcs”,查看系统中共享内存、信号量状态:
$ ipcs
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
------ Message Queues --------
key msqid owner perms used-bytes messages
NDK下IPC问题的更多相关文章
- C++成员变量内存对齐问题,ndk下非对齐的内存访问导致BUS_ADRALN
同样的代码,在vs下运行正常,在android ndk下却崩溃: signal 7(SIGBUS),code 1 (BUS_ADRALN),fault addr 0xe6b82793 Func(sho ...
- Android NDK 下的宽字符编码转换及icu库的使用(转)
原贴http://topic.csdn.net/u/20101022/16/1b2e0cec-b9d2-42ea-8d9c-4f1bb8320a54.html?r=70149216 ,看过并动手实现, ...
- 20155202 张旭 课下作业: Linux下IPC机制
20155202张旭 Linux下IPC机制 IPC机制定义 在linux下的多个进程间的通信机制叫做IPC(Inter-Process Communication),它是多个进程之间相互沟通的一种方 ...
- 20155239吕宇轩 Linux下IPC机制
20155239吕宇轩 Linux下IPC机制 - 共享内存 原理:把所有需要使用的共享数据都存放在共享内存 区域中,任何想要访问这些共享数据的进程都必须在自己的进程地址空间中新增加一块内存区域,用来 ...
- 2017-2018-1 20155307 《信息安全系统设计基础》第十周课上未完成补充以及课下IPC作业
课上内容2:stat命令的实现-mysate 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k ,grep -r的使用 伪代码 产品代码 mystate.c,提交码云 ...
- Linux下IPC机制
Linux下IPC机制 实践要求 研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 IPC 进程间通信(IPC,Inter ...
- NDK下 将Platinum SDK 编译成so库 (android - upnp)
Platinum UPnP SDK 是一个跨平台的C++库,利用该库,可以很容易就构建出DLNA/UPnP控制点(DLNA/UPnP Control Point)和DLNA/UPnP设备(DLNA/U ...
- NDK下编译JNI
NDK环境下编译JNI 下载demo.tar.gz然后解压 弄个套路 1.编辑build.sh设置好NDK目录 2.把cpp文件放到code下面 运行sh build.sh即可
- 【记录一个问题】没用任何用处的解决了libtask的context.c在32位NDK下的编译问题
32位下用ndk编译libtask出现这样的错误: [armeabi-v7a] Compile thumb : task <= context.c /Users/ahfu/code/androi ...
随机推荐
- 微信小程序-表格形式如何布局?
微信小程序没有现成的table标签,该怎么布局呢? <view class="my-grids"> <block wx:for="{{grids}}&q ...
- **极光推送PHP服务器端推送移动设备消息(Jpush V2 api)
jpush.php 这是推送方法 用到curl发送请求 <?php /** * 极光推送php 服务器端 * @author yalong sun * @Email <syl_ad@1 ...
- App Center编译React Native平台Android应用
做React Native一段时间后,对于React Native的发布有一些了解,原本的方法都是在本地直接生成APK文件的,具体可以参考<react native 生成APK> 因为需要 ...
- 深入解析php中的foreach问题
本篇文章是对php中的foreach问题进行了详细的分析介绍,需要的朋友参考下 前言:php4中引入了foreach结构,这是一种遍历数组的简单方式.相比传统的for循环,foreach能够更加便 ...
- HDU 1561 The more, The Better【树形DP/有依赖的分组背包】
ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...
- Python开发基础-Day9-生成器、三元表达式、列表生成式、生成器表达式
生成器 生成器函数:函数体内包含有yield关键字,该函数执行的结果是生成器,生成器在本质上就是迭代器. def foo(): print('first------>') yield 1 pri ...
- 【BZOJ 1815】【SHOI 2006】color 有色图
http://www.lydsy.com/JudgeOnline/problem.php?id=1815 这道题好难啊,组合数学什么根本不会啊qwq 题解详见08年的Pólya计数论文. 主要思想是只 ...
- Linux-Oracle 安装配置步骤
一.打开 VMware 安装 VMware 解压 ORACLER11.2_redhat.rar 到 D:\...\machine\oracle-linux 打开 VMware, 选择 打开虚拟机 找到 ...
- 【贪心】【堆】AtCoder Grand Contest 018 C - Coins
只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...
- 【深搜+set使用学习】POJ3050-Hopscotch
[题目大意] 给出一个5*5的方格,求出从任意一点出发走6步组成的不同序列数. [思路] dfs的水题,当作set使用方法的初次学习.每次从任意一点出发进行一次dfs,将序列加入set,最后输出set ...