What are primitives for the fastest user-space IPC?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
kotovalexarian
Member
Member
Posts: 38
Joined: Tue Nov 24, 2020 10:17 am
Contact:

What are primitives for the fastest user-space IPC?

Post by kotovalexarian »

Hello. Let's say I want to implement the fastest user-space IPC. The obvious solution is to use shared memory. However, we need more primitives because sometimes one process can wait for data from other one. What we want is to:
  • Notify OS scheduler that current process have nothing to do.
  • Notify OS scheduler that current process waits for data from specific other process.
The first requirement can be satisfied with sleep, pthread mutex or POSIX semaphore. The second requirement can't be satisfied with those primitives, I believe. For example, let's inspect how the POSIX semaphore works. It is implemented mostly in user space (in Linux using futex). When waiting for the locked semaphore, syscall is made to notify OS scheduler that current process have nothing to do. However, how can OS scheduler know which process it wait's data from? I suppose that this can magically happen because OS knows which processes share the region of memory where semaphore is located in, but documentation doesn't say anything about this. How can I get such guarantee? Native OS IPC primitives such as pipes provide such guarantee, and I want to have an advantage over them.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: What are primitives for the fastest user-space IPC?

Post by bzt »

The Minix kernel solves the second requirement by simply removing the waiting process from the active tasks list, and puts it on the sender process' needs-to-be-awaken list. So when the sender process is finished with the task and wants to send a response message, the waiting process is automatically put back to the active tasks list, but not sooner. No additional API required.

Cheers,
bzt
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: What are primitives for the fastest user-space IPC?

Post by Korona »

A priority inheriting pthread mutex can fulfill both requirements. Note that the second requirement is only really important in real-time contexts.

At the kernel level, all current mainstream OSes use futexes to implement this functionality (but Windows has some other options, too).

Note that in general, there is no "fastest" IPC. It depends a lot on the use case. Also note that using shared memory becomes a lot harder if processes do not trust each other and that it might be useful to have another IPC mechanism to establish the shared memory in the first place.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: What are primitives for the fastest user-space IPC?

Post by Schol-R-LEA »

Is there a specific goal for this, or are you just trying to optimize your IPC time as tightly as possible? As Korona said, IPC timings can depend a lot on the specific use case. 'As fast as possible' isn't going to have a one-size-fits-all answer.

Also, IPC speed is rarely a limiting factor for most purposes; other aspects unrelated to the speed of the IPC mechanism are likely to be bigger concerns in a given instance. I would serious ask myself whether this is premature optimization.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
User avatar
kotovalexarian
Member
Member
Posts: 38
Joined: Tue Nov 24, 2020 10:17 am
Contact:

Re: What are primitives for the fastest user-space IPC?

Post by kotovalexarian »

bzt wrote:The Minix kernel solves the second requirement by simply removing the waiting process from the active tasks list, and puts it on the sender process' needs-to-be-awaken list. No additional API required.
I develop my own user-space IPC.
Korona wrote:A priority inheriting pthread mutex can fulfill both requirements.
Thank you, I'll take a look.
Korona wrote:Note that the second requirement is only really important in real-time contexts.
I think that it can be important, for example, for high-loaded web servers. However, I don't have benchmarks for now.
Korona wrote:Note that in general, there is no "fastest" IPC. It depends a lot on the use case. Also note that using shared memory becomes a lot harder if processes do not trust each other and that it might be useful to have another IPC mechanism to establish the shared memory in the first place.
Schol-R-LEA wrote:Is there a specific goal for this, or are you just trying to optimize your IPC time as tightly as possible? As Korona said, IPC timings can depend a lot on the specific use case. 'As fast as possible' isn't going to have a one-size-fits-all answer.
Also, IPC speed is rarely a limiting factor for most purposes; other aspects unrelated to the speed of the IPC mechanism are likely to be bigger concerns in a given instance. I would serious ask myself whether this is premature optimization.
I also want to minimize syscalls for better portability.
Post Reply