Best way to write linux daemon

For work, I need to write a tcp daemon to answer our client software, and I was wondering if anyone had any tips on the best way to do this.

Should I fork for each new connection, as usual, would I use threads?

+4
source share
7 answers

It depends on your application. Threads and forks can be absolutely right approaches, as well as a third option for a single-threaded event-driven model. If you can explain a little more about what exactly you are writing, this will help with advice.

What it is worth, here are a few general recommendations:

  • If you do not have a general condition, use forking.
  • If you have a general state, use a threading or an event-driven system.
  • If you need high performance with a very large number of connections, avoid unlocking because it has higher overhead (in particular, memory usage). Instead, use threads, an event loop, or multiple event loop threads (typically one for each processor).

Usually forking will be the easiest to implement, as you can essentially ignore all other connections as soon as you use fork; Streams become the most complex due to additional synchronization requirements; the event loop is more complicated due to the need to turn your processing into a state machine; and several threads executing events cross the most complex of them (due to the combination of other factors).

+9
source

I suggest using forking for streaming connections any day. The problem with threads is the shared memory space, and how easy it is to manipulate the memory of another thread. With branched processes, any communication between processes must be specifically made by you.

Just looked and found this SO answer: What is the purpose of the fork? . You obviously know the answer to this question, but answer # 1 in this thread has good advantages regarding the benefits of fork ().

+3
source

Besides the good @hobodave answer, another advantage of "forking for connection" is that you can implement your server very simply using inetd or tcpserver or the like: then you can use standard input and standard output to communicate with the socket and you don’t need to perform listening control (listening for connections, etc.), etc.

+1
source

Another option, of course, is pre-formatting multiple copies of the daemon, and each of them remains alive and continues to respond to requests. It all depends on your application, expected load and performance requirements, among other things.

The easiest and easiest way is to write the inetd daemon; your software may ignore the fact that it works over a TCP connection and just processes I / O through stdin / stdout. This works well in the vast majority of cases.

+1
source

If you do not plan to score many new connections per second, consider starting from inetd. Otherwise...

Download the OpenSSH source code. They put a lot of work into privilege sharing just right, it is portable, and it has been carefully studied for security more than anything else.

Adapt it to your needs, you can probably throw away most of it. Of course, agree to the license agreement. Follow upcoming patches with a good SCC.

Do not worry about the performance of forking processes and threads until you have evidence that this is a real problem. Apache has visited over the years and years on the busiest sites with a simple simple model for each client.

If you are truly ambitious, you can use some kind of non-blocking asynchronous I / O model. I like Boost.Asio, but I'm heavy in C ++.

Make sure your code handles the signals correctly. HUP to reload the configuration. TIME until the end of grace.

Do not try to write your own log file. Use only syslog or just write to stderr, which can be redirected to syslog. This is a real pain when trying to configure logrotate on home servers, which all log in a bit differently.

+1
source

If you want to avoid streaming / formatting together, I would recommend using all non-blocking I / O with libevent , Libevent is pretty well known as a high-performance event-driven programming solution.

+1
source

Take a look at ACE (C ++ / Java) . It has a range of threaded, event, and TCP branching reactors that meet your communications requirements. You can also watch Boost ASIO , which does something like this.

0
source

All Articles