开发者

writing a C/C++ daemon (Linux)

开发者 https://www.devze.com 2023-01-08 19:10 出处:网络
I want to write a generic (C/C++) library that I will use to develop daemons in the Linux environment. Rather than reinventing the wheel, I thought I\'d come in here to find out if there are any well

I want to write a generic (C/C++) library that I will use to develop daemons in the Linux environment. Rather than reinventing the wheel, I thought I'd come in here to find out if there are any well known libraries in use.

The library could be either C or C++ - although I would prefer C++ (maybe something that was part of, or based on the excellent BOOST library?).

As an aside, in terms of library selection criteria, since daemons are pretty 'mission critical' components, it would be much better if the library that you propose is actively maintained by a group of developers (e.g. the BOOST library [again]), has an active community (or at least a mailing list to resort to when faced with tricky situations), rather than a lone individual somewhere out there ...

I saw this document, which is a good starting point, but it is slightly dated, so I'm wondering if there is anything better and more well known/used out there ... ?

BTW, I will be developing on Ubuntu (10.0.4开发者_C百科)


An alternative solution is to use a process monitor such as supervisord, which manages multiple services, restarts them when they crash, provides a minimalistic web page to view and control the status of processes, can manage groups of services, supports a general-purpose status-change event forwarding mechanism and other goodies. Such tools give you far more value than a daemon library would.


#include <unistd.h>

has

int daemon(int nochdir, int noclose);

Which forks, detatches from the controlling terminal, reopens all of {stdin, stdout, stderr} to /dev/null, and changes the working directory to the root directory. (based on flags, of course)


If your daemon uses tcp/ip sockets, you can use the inet daemon (or xinetd). Your process is started on demand as a new incoming connection comes in. There may be scalability issues in the event of large scale deployment, however.

0

精彩评论

暂无评论...
验证码 换一张
取 消