多线程/分叉服务器守护进程的模型

发布于 2024-11-05 01:04:48 字数 824 浏览 2 评论 0原文

我正在用 C++ 开发一个连接数据库的 Linux 守护进程,它将充当某些网络控制器设备的“服务器”。它必须具有某种形式的并发性才能同时为多个设备提供服务。服务器将侦听传入连接并为每个连接的设备分叉/线程/选择()/任何内容。此连接应保持打开状态,直到断开或关闭。客户端不会是多线程的,因此它们一次可能只有一个连接。

该项目要求设备能够使用将要处理的命令(通常包括数据库查询)联系“服务器”,然后将命令发送到另一个联网设备。数据库查询将确定与哪个其他客户端交谈。然后,我必须找到它所连接的进程或线程,并通过与该线程的现有连接与设备进行通信。

想象一个设备上某种形式的“授权”,导致另一台设备接收命令并执行某些操作。我不能只是在设备之间“点对点”发送命令,因为所有内容都必须记录在服务器上,并且可能需要数据库查询。

我对线程/进程之间的通信感到困惑。我预计有一个客户端类(即Device)并在每个单独的进程或线程中创建一个对象。我知道命名管道和使用 fork() 时共享内存,以及与其他线程通信的技术,但我担心同步和潜在的稳定性问题。在我遇到这个问题之前,我对 fork 及其易用性和简单性感到兴奋。稳定性是第一要务。

我这样做完全错了吗?我的描述不太好,所以那些认为自己能够提供帮助的人可能必须先提出问题。也许有人刚刚完成了类似这样的东西......


我对这个问题的长度和相当模糊的性质表示歉意。这更像是一个设计问题,而不是一个直接的、可回答的问题。我不确定它是属于程序员还是这里的SO。如有必要,请随意移动。

感谢所有试图拯救我理智的人。

更新:如果我为此使用Boost.Asio,任何人都可以给我一个简短的解释/示例代码或为我指出正确的方向吗?

I'm developing a database-connected Linux daemon in C++ that will act as a "server" for some networked controller devices. It has to have some form of concurrency to be able to service multiple devices at the same time. The server will listen for incoming connections and fork/thread/select()/whatever for each connected device. This connection should remain open until it drops or is closed. The clients will not be multithreaded, so they may only have one connection at a time.

This project requires a device to be able to contact the "server" with a command that will be processed, often including database queries, and, in turn, send a command to another one of the networked devices. The database query will determine which of the other clients to talk to. I then have to find which process or thread it's attached to and communicate with the device through the existing connection to that thread.

Think of some form of "authorization" at one device causing another device to receive a command and do something. I can't just send the commands "peer-to-peer" between devices because everything has to be logged at the server and database queries may be necessary.

I'm perplexed by the communication between threads/processes. I anticipate having a class for the client (i.e. Device) and creating an object in each separate process or thread. I know of named pipes & shared memory when using fork(), as well as techniques for communicating with other threads, but I worry about synchronization and potential stability concerns. I was excited about fork and its ease of use and simplicity until I hit this issue. Stability is the #1 concern.

Am I doing this totally wrong? My description isn't exactly great, so those of you who think you might be able to help probably will have to ask questions first. Maybe someone just finished building something exactly like this...


I apologize both for the length of this question and the rather vague nature. It's more of a design question rather than a direct, answerable one. I wasn't sure if it belonged at Programmers or here at SO. Feel free to move if necessary.

Thank you to everyone who attempts to save my sanity.

Update: If I'm using Boost.Asio for this, can anyone give me a brief explanation/sample code or point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-11-12 01:04:48

您应该查看 Boost::Asio 和活动对象模式。
在 boost'Con 2010 上有一个关于这个主题的精彩视频/讨论,您可以在 blip.tvB 上找到

You should ahve a look at Boost::Asio and at the active object patterm.
There is a nice video/talk about this very subject at boost'Con 2010 that you can find on blip.tvB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文