views:

1285

answers:

12

Dear stackoverflow,

The topic basically says it all. I want to go more into writing daemons for Linux/UNIX systems and I want things to be fast and stable. Everything I look at seems to be written in C, however, some new projects seem to be using C++ a bit more.

So if I were to start my own project, let's say a modular HTTP daemon, which language would it make more sense for me to use?

I'm quite familiar with both languages but not very specialised. So which language I know better is not really a factor.

Thanks in advance!

A: 

I'd go for C++ but that's a matter of personal preference. Everything you can do in C, you can do it in C++.

Something more important here than the choice of the language is the choice of the libraries. There is many libraries out there to help you in the process of creating a daemon (logging facilities, argument parsing, configuration, and more..).

f4
And a lot of the important things you can do in C++ you can approximate in C http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
Hassan Syed
sure but it's less trivial
f4
+19  A: 

For me it would have to be C++. You can write fast & stable in either language. Equally you can write slow, screwy and buggy in either language.

C is more prevalent because that's the way it's always been done, rather than it being better or worse.

Simply put: write code in the language / paradigm that you are most comfortable in and you will get the best results.

Richard Harrison
+1 Use the language you're most comfortable with.
Chris S
A: 

C has been more popular in the past because it's what was most available and well known. I personally still prefer C, but I know it better than C++, and there's more references to pull examples from (though almost all of that code can be converted to C++ with little effort). C++ is definitely easier if you're keeping track of large complex dataset; but both are capable of building a daemon that's fast and stable.

Chris S
+2  A: 

I'm a C++ fan, but I will give you the following caution:

C is MUCH simpler to master compared to C++. C++ allows you tackle problems much larger in scope. Now if you don't want to take the time to master C++ start with C, it will more likely allow you to write programs that others might accept as a well engineered solution. If you start with C++ and do things the wrong-way you .... well it can be painfull.

Hassan Syed
That's a really good point. +1
jalf
+4  A: 

which language I know better is not really a factor.

In that case I would recommend C++. This kind of project is what OOP and code reuse is all about.

The basic outline of daemons has been coded hundreds of thousands of times. There are a number of good C++ libraries like POCO and ACE that abstract out almost all of the boilerplate and provided you with many useful facilities and options that come bug free, tested by thousands, and cross-platform. Once you are past the learning curve of the framework itself you are free to concentrate on the unique parts of your application without too much worry about the myriad ways network programming can bite you.

Duck
Always bothers me when people talk as if OOP has a monopoly on code reuse... Not least because it's actually a horrible paradigm for producing reusable code...
jalf
A: 

A Reason to Use C

The main reason I would see for using C is that the C libraries will already be in memory, mapped into the other daemons.

If you write a C++ daemon you will probably be the only one and you will be mapping libraries into your RAM for the first time.

Of course, modern machines have lots of RAM so maybe this doesn't matter.

DigitalRoss
Can't you use the C libraries in C++ too?
Brendan Long
Why would there be different C and C++ libraries? Usually, there'd be one set of libraries, written in whatever (probably C, possibly C++, maybe something else), with the standard C ABI, and everything would use those regardless of what language (C++, Python, Lisp, whatever).
David Thornley
A: 

Definately you can use both languges in your project. BUT, afaik, C++ have MUCH more frameworks(Boost,Qt, etc.) to help you write fast and productively. Modern hardware make differencies in performance of C and C++ code almost invisible. If you will take right framework, it will cover off many painful issues that can grab you with pure C. The only disadvantage of C++ is the problem with static linking.

Stability of your code depends on your skills, but I think with C you have to be much more careful.

ensoreus
A: 

Having the awesomeness of Boost.Asio for network, I'd definitely choose C++.

Nicolás
+1  A: 

Our team has written them in both, they both work fine.

Do realize that C++ isn't merely "C with objects"; it took me a while (a couple of less-than-awesome C++ projects, including daemons) to figure that out. If you aren't super-familiar with C++ and decide to go that route, I'd recommend working with someone who has had some serious OOP experience before embarking on the daemon-writing.

Oh, and grab the Richard Stevens book on Advanced UNIX Programming. Much well-written wisdom lies within that tome and provides a good foundation for how daemons work. Make sure you compare with 'net based sources for the changes that have occuring since his passing.

sheepsimulator
Do you mean "Do realize that C++ isn't merely 'C with objects'"? I got hung up on that sentence.
SaulBack
Yeah... my bad. Fixed. Thanks for pointing it out.
sheepsimulator
+3  A: 

I'm going to break with my colleagues and suggest: neither.

Really, C and C++, in 2010? Unless you're writing for a severely resource-constrained system and have lots of time and money to throw at software (maybe a web server for the microcontroller in your mouse?), that seems like a big waste of time.

I see plenty of daemons written in HLLs these days. They're stable, and plenty fast for anything I've ever seen. Do you really want to be chasing down memory leaks in long-running server processes?

Between Bittorrent and Youtube, most bits on the public internet today are probably going through a Python process. The people that build these systems will tell you that network I/O is still (and will probably always be) the bottleneck, so it doesn't matter if your HLL is a little slower than C.

Finally, as Alan Kay pointed out, there's nothing more inefficient than a program which never ships. The best way I know to get a product shipping soon is to use the highest-level language you can find, so you have to write the least code.

Python and Ruby are popular choices in this area, but there are of course many others.

Ken
Python and Ruby are popular choices in writing daemons? Care to back-up your statement with proofs? IMHO, daemons are compiled programs which are intended to be maintained for a long time to come; while Python and Ruby are scripting languages which are very good at RAD, tool making, etc.
legends2k
You can save pretty much the same amount of development time by using libraries written for C or C++ (or, say, Java). People have this idea that C programming is reinventing the wheel constantly, but outside of University courses - where the /point/ is to learn how the backend stuff is written - you really shouldn't be doing *that* much more work than it would be to write something in perl (or some similar perl-wannabe ;))
dannysauer
legends2k: Of all the daemons I've written or seen written over the years, in academia and corporate settings, the vast majority have been in Python or Ruby. I have no "proof" to offer other than my own experiences. I've never heard any definition of daemon that said anything about compilation.
Ken
dannysauer: I've never used C in an academic setting. I have the idea that C programming is low-level because I've had to use it for real work. Using a low-level language with appropriate libraries only works well if you're doing something sufficiently similar to what others have already done -- if you're doing something altogether new, an HLL will absolutely kill it. If Perl and others didn't save massive amounts of time compared to a C library, they wouldn't exist. For example, I write lambdas all day long, but I've never seen a halfway usable lambda in C, nor an efficient alternative.
Ken
Examples of modern daemons written in HLLs: Lamson (SMTP/Python), CherryPy (HTTP/Python), Mongrel (HTTP/Ruby), Webmin (admin/Perl), Aserve (HTTP/Lisp). I'd much rather run lfingerd (finger/Python) than any similar program in C, because the Python one is under 150 lines, including the license.
Ken
A: 

I'd say do neither - you have not said what your daemons need to do (HTTP is a bad example, there are already a lot of good implementations).

I'd say use a language which makes things easy. Like Python or something.

MarkR
A: 

I think C and C++ would not be very different if both of them compiled and become a binary. You can build some daemon program using both of them. But you have to think about the essence of your program, I think if you want to build the program that have big complexity and will develop it in a long time, I would prefer to use C++. Because C++ have an essence of object oriented programming that makes you feel comfort in tracing the code.

deddihp