Show HN: Threadprocs – executables sharing one address space (0-copy pointers)

Introduction to Threadprocs

As developers, we're constantly looking for ways to improve performance and efficiency in our applications. One area that's often overlooked is the way we handle memory sharing between processes. Traditional approaches, such as using POSIX processes or dlopen-based plugin systems, can be cumbersome and restrictive. That's why I'm excited to explore a new project called Threadprocs, which promises to change the way we think about memory sharing.

What are Threadprocs?

Threadprocs is a project that allows multiple independent programs to share a single virtual address space, while still behaving like separate processes. This means that each threadproc is a standalone, semi-isolated process with its own independent binaries, globals, and lifetimes. But here's the key benefit: pointers are valid across threadprocs, with no code changes required for well-behaved Linux binaries.

How does it work?

The implementation of Threadprocs relies on careful manipulation of virtual address layout, file descriptors, signals, and other low-level details. It's implemented entirely in unprivileged user space code, which makes it a fascinating example of what can be achieved without relying on kernel modifications. The project uses a custom loader to direct ASLR (Address Space Layout Randomization) and load the executables into a shared address space.

Benefits and Use Cases

So, what are the benefits of using Threadprocs? For one, it allows for zero-copy pointer-based data structures to be passed between threadprocs, which can be a huge performance boost. Imagine being able to share complex data structures like std::string or std::unordered_map between processes without having to worry about serialization or copying. This makes Threadprocs particularly relevant for systems that rely on shared memory, such as ring buffers or flat tables.

Some potential use cases for Threadprocs include:

  • Real-time systems: Threadprocs could be used to improve performance and reduce latency in real-time systems that require shared memory.
  • High-performance computing: By allowing for zero-copy data sharing, Threadprocs could be used to accelerate certain types of computations that rely on shared memory.
  • Embedded systems: Threadprocs could be used to reduce memory usage and improve performance in embedded systems that require multiple processes to share memory.

Features and Limitations

Some key features of Threadprocs include:

  • Independent binaries: Each threadproc can have its own independent binary, globals, and lifetimes.
  • Zero-copy pointers: Pointers are valid across threadprocs, with no code changes required for well-behaved Linux binaries.
  • Semi-isolated processes: Each threadproc is a standalone, semi-isolated process with its own file descriptors, signals, and other resources.

However, there are also some significant limitations and edge cases to consider:

  • Data race considerations: When sharing data structures between threadprocs, you'll need to consider the usual data race issues that arise when accessing shared memory.
  • Limited scalability: Threadprocs may not be suitable for very large-scale systems, due to the limitations of shared memory and the potential for data corruption.

Example Code and Demo

The Threadprocs project includes a simple demo that demonstrates "cross-threadproc" memory dereferencing. You can find the demo code on GitHub, along with a high-level diagram that illustrates how the system works.

// Example code snippet showing how to share a pointer between threadprocs
#include <stdio.h>
#include <stdint.h>

int main() {
    // Create a shared pointer to a data structure
    int* shared_ptr = malloc(sizeof(int));
    *shared_ptr = 42;

    // Launch a new threadproc and pass the shared pointer
    // ...

    // Access the shared pointer from the new threadproc
    printf("Shared value: %d\n", *shared_ptr);

    return 0;
}

Who is this for?

Threadprocs is likely to be of interest to developers who work on systems that require shared memory, such as real-time systems, high-performance computing, or embedded systems. It's also a fascinating example of what can be achieved with creative use of user space code and careful manipulation of low-level details. If you're looking for a way to improve performance and efficiency in your applications, Threadprocs is definitely worth exploring.

What do you think about Threadprocs? Do you have any experience with shared memory or zero-copy data structures? Share your thoughts and experiences in the comments below!

🚀 Global, automated cloud infrastructure

Oracle Cloud is hard to get. I recommend Vultr for instant setup.

Get $100 in free server credit on Vultr →