与 C++ 中的守护进程交互带插座

发布于 2024-10-13 15:18:01 字数 258 浏览 5 评论 0原文

我正在编写一个守护进程,它需要在后台运行并处理任务,还需要直接从前端接收输入。我一直在尝试使用套接字来处理此任务,但是,由于套接字在等待连接时暂停程序,因此我无法使其正常工作。有办法解决这个问题吗?

我正在使用 http://linuxgazette.net/issue74/tougher.html

感谢您提供的所有帮助

I'm writing a daemon that needs to both run in the background and take care of tasks and also receive input directly from a frontend. I've been attempting to use sockets to take care of this task, however, I can't get it to work properly since sockets pause the program while waiting for a connection. Is there anyway to get around this?

I'm using the socket wrappers provided at http://linuxgazette.net/issue74/tougher.html

Thank you for any and all help

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

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

发布评论

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

评论(5

云雾 2024-10-20 15:18:01

您将需要使用线程来使套接字操作异步。或者使用一些已经实现它的库,最重要的库之一是 增强Asio

You will need to use threads to make the socket operations asynchronous. Or use some library that has already implemented it, one of the top ones is Boost Asio.

迟月 2024-10-20 15:18:01

有几种方法可以处理这个问题。最常见的是使用事件循环和类似 libevent 的东西。然后使用非阻塞套接字。

以事件驱动的方式执行此操作可能需要对程序逻辑进行重大转变。但使用线程来实现它有其自身的复杂性,并且显然不是更好的选择。

There are a few ways to handle this problem. This most common is using an event loop and something like libevent. Then you use non-blocking sockets.

Doing this in an event driven fashion can require a big shift in your program logic. But doing it with threads has its own complexities and isn't clearly a better choice.

空心↖ 2024-10-20 15:18:01

通常守护进程使用事件循环来避免等待事件的问题。

这是解决您提出的问题的最明智的解决方案(不要等待异步事件)。尽管如此

,通常整个守护进程是在事件循环及其回调架构上构建的,并且可能导致部分重写,因此通常快速而肮脏的解决方案是创建一个单独的线程来处理这些事件,这通常会产生比解决的错误更多的错误。因此,使用事件循环:

  • libevent。
  • 灵活的事件循环。
  • 利布夫。
  • 升压::asio
  • ...

Usually the daemons use event loops to avoid the problem of waiting for events.

It's the smartest solution to the problem that you present (do not wait to an asynchronous event). ç

Althought, usually the entire daemon is build over the event loop and it's callback architecture, and can cause a partial rewritting, so usually the quick and dirty solution is creating a separate thread to handle those events wich usually creates more bugs than it solves. So, use an event loop:

  • libevent.
  • glib event loop.
  • libev.
  • boost::asio
  • ...
无声静候 2024-10-20 15:18:01

根据您的描述,您已经将应用程序分为前端(接收输入)和后端(套接字处理和任务)。如果来自前端的输入是通过套接字发送的(通过后端)而不是从套接字接收输入,那么您似乎正在描述客户端而不是服务器。客户端程序通常不作为守护进程实现。

您已经创建了一个阻塞套接字,并且需要在单独的线程执行中监视线程甚至单独的进程),或者创建一个非阻塞套接字并频繁轮询以获取更新。

LinuxGazette 的链接是网络编程的基本介绍。如果您想更深入一点,请查看 Beej 的网络指南编程中对您可用的各种 API 调用进行了一些详细的解释..并且也许会让您欣赏更多的包装器库,例如 Boost::ASIO。

From your description, you have already divided your application into a frontend (receiving input) and backend (socket handling and tasks). If the input from the frontend is sent over the socket (via the backend) rather receiving input from the socket then it seems like you are describing a client and not a server. Client programs are typically not implemented as daemons.

You have created a blocking socket and need to either monitor in a separate thread execution a thread or even separate process) or make a non-blocking socket and poll frequently for updates.

The link to the LinuxGazette is a basic intro to network programming. If you would like a little more depth then take a look at Beej's Guide to Network Programming where the various API calls available to you are explained in a little detail.. and will, perhaps, make you appreciate more wrapper libraries such as Boost::ASIO.

找个人就嫁了吧 2024-10-20 15:18:01

值得您自己保留对事件循环的控制——它并不复杂,并且提供了灵活性。

事件循环的“C++ 伪代码”。

while (!done)
    {
        bool workDone = false;
        // Loop over each event source or internal worker
        for each module
        {
            // If it has work to do, do some.
            if (module.hasWorkDoTo())
            {
                // Generally, do as little work as possible; e.g. process a single event for this module.
                // But tinker with this to manage priorities if need be.
                // E.g. Maybe allow the GUI to flush its queue.
                module.doSomeWork();
                workDone = true;
            }
        }
        if (!workDone)
        {
            // System idle. No Sleep for a bit so we have benign idle baheviour.
            nanosleep(...);
        }
    }

Can be worth retaining control of the event loop yourself - its no complicated and provides flexibility down the track.

"C++ pseudo-code" for an event loop.

while (!done)
    {
        bool workDone = false;
        // Loop over each event source or internal worker
        for each module
        {
            // If it has work to do, do some.
            if (module.hasWorkDoTo())
            {
                // Generally, do as little work as possible; e.g. process a single event for this module.
                // But tinker with this to manage priorities if need be.
                // E.g. Maybe allow the GUI to flush its queue.
                module.doSomeWork();
                workDone = true;
            }
        }
        if (!workDone)
        {
            // System idle. No Sleep for a bit so we have benign idle baheviour.
            nanosleep(...);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文