Are there any drawbacks to using C ++ for network daemons?

In recent years, I have written several network daemons in different languages, and now I am going to launch a new project that requires a new user implementation of the proper network protocol.

The specified protocol is quite simple - some basic messages in JSON format, which are transmitted in some basic Frey packaging, so that clients know that the message is received completely and is ready for analysis.

The daemon will have to process several connections (about 200 at the same time), as well as manage them and send messages together, as in a chat room.

In the past, I used mostly C ++ to write my demons. Often with a Qt4 card (parts of the network, not parts of the GUI!), Because this is what I also used for other projects, and it was easy to do and very portable. This is usually normal, and I had no particular problems.

As a Linux administrator now, I noticed that most network daemons in the wild are written in simple C (of course, some of them are written in other languages, but I feel that> 80% of daemons are written in simple C).

Now I wonder why this is so. Is this due to a clean UNIX historical background (e.g. KISS) or simple portability or less bloat? What are the reasons not to use C ++ or any higher level languages ​​for things like daemons?

Thanks in advance!


Update 1:

For me, using C ++ is usually more convenient due to the fact that I have objects that have getter and setter methods, etc. Objects in a regular context "context" can be a real pain at some point, especially when you are used to object-oriented programming.

Yes i know that C ++ is a superset of C, and this C code is basically C ++ You can compile any C code using the C ++ compiler. But this is not the main thing .;)


Update 2:

I know that at present it might make sense to use a high-level language (scripting) such as Python, node.js or similar. I have done this in the past, and I am aware of the benefits of this (at least I hope so do I); but this question only applies to C and C ++.

+4
source share
8 answers

I am not thinking of any technical reason to choose C over C ++. Not the one that I can't immediately think of counterpoint, anyway.

Edit in response to editing: I would seriously discourage you from considering: "... C code is mostly C ++." Although you can technically compile any C program using the C ++ compiler (since you are not using any function in C that was newer than what was adopted in C ++), I'm really trying to prevent that so that someone writes C as C ++ code or treats C ++ as "C with objects."

In response to the fact that C is standard on Linux, only if the C developers keep talking about it: p C ++ is as much a part of any standard on Linux as C, and there are a huge number of C + programs + created in Linux. If you are writing a Linux driver, you need to do it in C. Besides this ... I know that RMS likes to say that you are more likely to find a C compiler than C ++, but in fact it wasn’t really pretty long time. You will find both or not at almost all installations.

In response to maintainability - I, of course, do not agree.

As I said, I cannot come up with one that cannot be immediately refuted. Visa is the opposite too.

+6
source

C ++ resistance for developing daemon code is based on several sources:

  • C ++ has a reputation for being hard to avoid memory leaks. And memory leaks are not any long-term software. This is somewhat wrong - the problem is that developers with a C-background tend to use C-idioms in C ++, and this is very impenetrable. Using the available C ++ functions, such as vectors and smart pointers, can lead to code creation without leaks.

  • Conversely, smart pointer template classes, while they hide resource allocation and release from the programmer, do a lot of it under the covers. In fact, C ++ usually has a lot of implicit distribution as a result of creating copy constructors, etc. As a result, a C ++ heap can become fragmented over time, and daemon processes will ultimately fail with an error from memory, even if there is enough RAM. This can be improved by using modern heap managers who are more resistant to fragmentation, but they do this by consuming more resources up.

  • while this does not apply to the usermode daemon code, kernel-mode developers avoid C ++, again, because of the implicit code, C ++ generates, and C ++ libraries use exceptions to handle errors. Most C ++ compilers implement C ++ exceptions in terms of hardware exceptions, and a lot of kernel mode code runs in environments where exceptions are not allowed. In addition, all implicit code generated by C ++, being implicit, cannot be wrapped in #pragma directives to guarantee its placement in a page accessible or insecure memory.

As a result, C ++ is not possible for kernel development on any platform in general, and as a rule, it also avoids daemon developers. Even if one code is written using the appropriate classes of intellectual memory management and does not leak, storing over potential problems of memory fragmentation makes languages ​​where memory allocation is an explicit preferred choice.

+6
source

I would recommend that you be better. If it’s more convenient for you to use C ++, your code will be cleaner and work more efficiently, because you will get more used to it if you know what I mean.

The same applies on a larger scale to something like a discussion of Python vs Perl. Whatever you are more comfortable is likely to create better code, because you will have experience.

+2
source

I think the reason is that ANSI C is the standard Linux programming language. It is important to follow this standard when people want to share their code with other people, etc. But this is not a requirement if you just want to write something for yourself.

You personally can use C or C ++, and the result will be identical. I think you should choose C ++ if you know this well and can use some special object-oriented functions in your code. Don't look too much at other people here if you are good in C ++, just go in and write your daemon in C ++. I will personally write it in C ++ too.

+1
source

You're right. The reason for not using C ++ is KISS, especially if you are ever planning on someone else supporting your code in the future. Most of the people I know have learned to write demons from existing sources or read Stevens books. To a large extent, this means that your examples will be in C. C ++, that’s fine, I wrote demons myself, but I think that if you expect it to be saved and you don’t know who its supporter can be, shows the best foresight for writing in C.

+1
source

Boost makes it incredibly easy to create single-threaded or multi-threaded and highly scalable network daemons with the asio library.

+1
source

I would recommend using C ++ with the caveat of using exception handling and dynamic RTTI. These features may have temporary performance implications and may not be supported on all platforms.

C ++ is more modular and supported, so if you can avoid these features, continue to use it for your project.

0
source

Both C and C ++ are great for defining a daemon.

In addition, you should also consider scripting languages ​​like Perl or Python at this time. Performance is usually good enough, and you can write applications more reliable and in less time.

BTW, see ACE , a framework for writing portable network applications in C ++.

0
source

Source: https://habr.com/ru/post/1335716/


All Articles