简体   繁体   English

使用System V的共享内存IPC的Posix线程

[英]Posix Threads for shared memory IPC using System V

I am writing a client and a server program to demonstrate IPC between the client and server process. 我正在编写一个客户端和服务器程序,以演示客户端和服务器进程之间的IPC。 For example, the client thread may send data to the server (which is done in this case using System V shared memory segments). 例如,客户端线程可以将数据发送到服务器(在这种情况下,使用System V共享内存段完成)。 The choices for creating shared memory segments were: system v and posix shared memory commands). 用于创建共享内存段的选择是:System v和posix共享内存命令)。

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. 谈到主要问题,快速搜索如何在linux环境链接中使用线程(Ubuntu 16.04内核:4.13.0-36-generic)表明我们可以将pthreads用于相同的线程。 Will using pthreads (which stands for POSIX threads) affect the usage of the memory segments in any way? 使用pthreads(代表POSIX线程)是否会以任何方式影响内存段的使用? 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. 两段的描述是为了给这个问题一些背景。

You have to be careful not to confuse Intra-Process-Communication (IPC) with Inter-Process-Communication here. 您必须注意不要在此处将进程内通信(IPC)与进程间通信混淆。 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). SysV和Posix共享内存指的是前者(进程之间的通信),Posix线程处理后者(进程中使用多个线程的通信/同步)。

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. 也就是说,假设您的客户端和服务器是单独的进程 (而不是进程中的线程),则为服务器使用posix线程能够一次处理多个请求是合理的,但是使用IPC来传递客户端和服务器之间来回请求和响应。

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. 这种方法可以很好地将IPC工作与多线程处理分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM