进程间通信之共享内存


 

共享内存相关的函数shm_open、shm_unlink。Linux系统中可用,Android系统上不支持。

函数shm_open的作用是打开一个特殊的文件,返回file descriptor,这个fd可以与mmap一同使用。

 

http://comments.gmane.org/gmane.comp.gnu.apl.bugs/1377

 

Elias Mårtenson | 2 Jun 10:53 2014

 

Android codecompatibility issues

I've compiled GNU APL for Android, and I'm about to start working on theuser interface. Building it was remarkably simple, but it exposed two smallissues that required some code changes:

 

First of all, SystemVariable.cc includes "langinfo.h". This fileis not available on Android, but simply commenting this one out fixed theissue. Is it even needed anywhere else? Can it be completely removed? If not,can it be placed inside some #ifdef?

 

The second issue is that the SHM calls don't exist on Android. I solved itby creating versions of shm_open() and shm_unlink() that simply returns -1 (andsets errno to ENOSYS). This makes the code compile, but it's pretty ugly. Wouldit make sense to provide a configure option to disable the use of sharedmemory? (and, by extension, the AP's)

 

Regards,

Elias

 

Juergen Sauermann | 2 Jun 16:41 2014

 

Re: Android code compatibility issues

Hi Elias,
 
I have removed langinfo.h (a left-over from internationalization which I 
removed earlier, SVN 307.
 
shm_open() and shm_unlink will go away long-term because I will move the 
shared memory database to a
separate thread. Just didn't have enough time yet to do that.
 
/// Jürgen

 

 

在Linux系统中,sem_open执行后,在maps文件中能看到系统创建了共享内存。

在Android系统中,sem_open不支持。根据ndk的文档,sysv ipc不支持的。

 

http://www.cnblogs.com/crazii/p/3478465.html

 

[]android不支持命名的semaphore

之前sem_open在iOS上, 创建命名的semaphore没有问题 (iOS不支持匿名的semaphore), 但是现在Android平台的sem_open时候报错,返回ENOSYS.

命名的semaphore因为有名字,可以在不同进程间访问,

幸好我这里不需要进程间共享和通信, 直接使用匿名semaphore也可以. (貌似匿名的semaphore也可以通过mmap实现进程共享和通信.但目前没有需求).

使用sem_init 和sem_destroy代替sem_open和sem_close, 问题解决.

 

 



在Linux下,进程间通信的一种方式是通过共享内存来实现的。共享内存允许两个或多个进程共享一定的存储区,这样它们就可以直接访问同一块内存区域,而不需要进行数据的复制。共享内存是一种高效的进程间通信方式,因为数据直接写入内存,不需要多次数据拷贝,所以传输速度很快\[2\]。 在使用共享内存进行进程间通信时,需要给共享内存创建一个唯一的身份ID,以便区分不同的共享内存。当进程需要访问共享内存时,需要在映射时带上这个ID,这样就可以确定访问的是哪一个共享内存\[3\]。 需要注意的是,共享内存并没有提供同步机制,也就是说,在一个进程结束对共享内存的写操作之前,并没有自动机制可以阻止另一个进程开始对它进行读取。为了实现多个进程对共享内存的同步访问,通常会使用信号量来实现对共享内存的同步访问控制\[2\]。 总结起来,Linux下的共享内存是一种高效的进程间通信方式,允许多个进程共享一块存储区。通过给共享内存创建唯一的身份ID,可以区分不同的共享内存。然而,共享内存并没有提供同步机制,需要使用信号量来实现对共享内存的同步访问控制\[2\]\[3\]。 #### 引用[.reference_title] - *1* *3* [Linux多进程间通信——共享内存实现](https://blog.youkuaiyun.com/zhm1949/article/details/124909541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Linux下进程间通信方式——共享内存](https://blog.youkuaiyun.com/xujianjun229/article/details/118584955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值