使用Android NDK进行跨进程锁定? [英] Cross-process locking with Android NDK?

查看:429
本文介绍了使用Android NDK进行跨进程锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以锁定例如跨进程在Android NDK上使用互斥锁或文件锁?

Is there a way to lock e.g. using mutexes or file locks on Android NDK, across processes?

我想锁定一个套接字,以便一次只能向其发送一个进程.这些过程不是分叉的,它们是独立的调用.

I want to lock a socket so only one process can send to it at once. The processes are not forked, they are independent invocations.

在编写共享库时,我不能依赖于任何需要写入权限的共享位置,也不能使用任何进程本地存储,因为根据定义,这将无法与其他进程共享.这也适用于应用程序名称.我本质上是在寻找带有AF_UNIX套接字的抽象名称空间之类的东西,但要寻找互斥锁/信号灯.

As I am writing a shared library, I cannot rely on any shared location which requires permissions to write to it, neither can I use any process local storage, because this would be unable to share with other processes by definition. This goes for app names too. I'm essentially looking for something like abstract namespaces with AF_UNIX sockets, but for mutex/semaphores.

推荐答案

假定进程具有相同的用户ID,则可以使用flock(2)锁定文件(两个进程都可以访问任何文件)或POSIX信号灯操作(sem_open(3))来使用信号量.

Assuming the processes have the same user ID, you can use flock(2) to lock a file (could be anything accessible to both processes), or the POSIX semaphore operations (sem_open(3)) to use a semaphore.

如果用户ID不同,则该机制仍将起作用,但是您必须将文件许可权设置为更开放"-引入了某些恶意应用通过抓取来执行拒绝服务攻击的风险锁.

If the user IDs are different, the mechanisms will still work, but you have to set the file permissions to be more "open" -- which introduces the risk of some malicious app performing a denial-of-service attack by grabbing the lock.

这篇关于使用Android NDK进行跨进程锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆