简体   繁体   中英

writing a C/C++ daemon (Linux)

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 (eg 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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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