我正在寻找一种在内核空间和用户空间之间共享普通 Linux 或 POSIX 信号量的方法,以便我可以在非实时上下文中安全地等待/发布它.如果您提供有关此主题的任何信息,我们将不胜感激.如果这是不可能的,您有什么其他想法可以完成这项任务吗?11 一种方法是添加系统调用,将信号量放在内核空间中,让用户空间进程调用该系统调用,信号量将全部在内核空间中管理.如果我不必仅仅因为这个而修补内核,我会更高兴. 解决方案 我能想到的一个解决方案是拥有一个 /proc(或 /sys 或其他)主内核模块上的文件,其中写入 0/1(或读取/写入)将导致它发出 up/down 在 semaphore 上.导出该信号量允许其他内核模块直接访问它,而用户应用程序将通过 /proc 文件系统.我还是等着看原问题是否有答案.Short versionIs it possible to share a semaphore (or any other synchronization lock) between user space and kernel space? Named POSIX semaphores have kernel persistence, that's why I was wondering if it is possible to also create, and/or access them from kernel context.Searching the internet didn't help much due to the sea of information on normal usage of POSIX semaphores.Long versionI am developing a unified interface to real-time systems in which I have some added book keeping to take care of, protected by a semaphore. These book keepings are done on resource allocation and deallocation, which is done in non-real-time context.With RTAI, the thread waiting and posting a semaphore however needs to be in real-time context. This means that using RTAI's named semaphore means switching between real-time and non-real-time context on every wait/post in user space, and worse, creating a short real-time thread for every sem/wait in kernel space.What I am looking for is a way to share a normal Linux or POSIX semaphore between kernel and user spaces so that I can safely wait/post it in non-real-time context.Any information on this subject would be greatly appreciated. If this is not possible, do you have any other ideas how this task could be accomplished?11 One way would be to add a system call, have the semaphore in kernel space, and have user space processes invoke that system call and the semaphore would be all managed in kernel space. I would be happier if I didn't have to patch the kernel just because of this though. 解决方案 One solution I can think of is to have a /proc (or /sys or whatever) file on a main kernel module where writing 0/1 to it (or read from/write to it) would cause it to issue an up/down on a semaphore. Exporting that semaphore allows other kernel modules to directly access it while user applications would go through the /proc file system.I'd still wait to see if the original question has an answer. 这篇关于用户空间和内核空间之间的共享信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!