Reputation: 2362
I am writing a client and a server program to demonstrate IPC between the client and server process. For example, the client thread may send data to the server (which is done in this case using System V shared memory segments). The choices for creating shared memory segments were: system v and posix shared memory commands).
In the setup that we have created so far, the client sends only one request to the server to which the server returns the computed value to the client. However, I would like to simulate the creation of multiple requests from the client side and hence wanted to spawn multiple threads each of which create a shared memory segment which the server accesses and stores the value.
Coming to the main question, a quick search on how to use threads in a linux environment link (Ubuntu 16.04 Kernel: 4.13.0-36-generic) shows that we can use pthreads for the same. Will using pthreads (which stands for POSIX threads) affect the usage of the memory segments in any way? Are there any incompatibilities that I should be aware of?
EDIT: The question is not about how to design such a system but to know more about the thread-safeness of initiating memory segments. The two paragraph description was to give some context to the question.
Upvotes: 1
Views: 755
Reputation: 1715
You have to be careful not to confuse Intra-Process-Communication (IPC) with Inter-Process-Communication here. SysV and Posix Shared Memory refer to the former (communication between processes), Posix Threads deals with the latter (communication/synchronization within a process using multiple threads).
That said, and assuming your client(s) and server are separate processes (not threads within a process), it is reasonable to use posix-threads for your server to be able to process multiple requests at once, but use IPC for communicating the request and response back and forth between the client(s) and server.
Without knowing more details about your problem, a first order way to handle this would be to create a thread-pool where each thread is responsible for processing from a single client. A single server thread could be responsible for servicing all client requests, offloading them to individual worker threads for processing and then later retrieving the answer to send back to the client. This approach would nicely separate the IPC work from the multi-threaded processing.
Upvotes: 1