Is it possible to implement Linux executable concurrency primitives that provide better isolation than threads but comparable performance?

Consider the following application: a web search server, which at startup creates a large index in the memory of web pages based on data read from disk. After initialization, the in-memory index cannot be changed and several threads are launched to serve user requests. Suppose the server is compiled into native code and uses OS threads.

The threading model now does not contain isolation between threads. An erroneous thread, or any secure code that is not associated with a thread, can ruin an index or corrupted memory that has been allocated and logically belongs to another thread. Such problems are difficult to detect and debug.

Theoretically, Linux allows for better isolation. After the index is initialized, the memory it occupies can be read-only. Themes can be replaced by processes sharing an index (shared memory), but different from those that have separate heaps and cannot damage each other. Illegal operation is automatically detected by the hardware and operating system. No mutexes or other synchronization primitives are needed. Memory jumps are completely eliminated.

Is such a model possible in practice? Are you aware of any real application that does such things? Or maybe there are some fundamental difficulties that make such a model impractical? Do you think that this approach can lead to an increase in performance compared to traditional threads? Theoretically, the memory used is the same, but are there some implementation problems that slow down the work?

+6
source share
3 answers

The obvious solution is not to use threads at all. Use separate processes. Since each process has a lot in common with code structures and readonly, making data sharing only trivial: format it as necessary for use inside the memory in the file and map the file to memory.

Using this scheme, only the variables for each process will be independent. The code will be shared, and the statically initialized data will be transmitted until it is written. If the process is overwhelmed, there will be zero on other processes. No concurrency issues.

+4
source

You can use mprotect() to make your index read-only. On a 64-bit system, you can map the local memory for each thread at a random address (see this Wikipedia article on address space randomization ), which makes it possible to damage memory from one thread touching another astronomically small (and, of course, any damage, which skips the mapped memory as a whole, will call segfault). Obviously, you need to have different heaps for each thread.

+1
source

I think you could find memcached interesting. In addition, you can create shared memory and open it as read-only, and then create your own threads. This should not result in poor performance.

0
source

Source: https://habr.com/ru/post/924672/


All Articles