Need to write a daemon in linux, not sure what to use C++ or C
I have a little problem picking the right language to write my daemon, I am confused between C and C++, I want to use C++ because it is more expanded than C, but I want to use C because it is the starting point of everythi开发者_高级运维ng in linux,
I want to go for C++ as I have many resources about it, so, does it make any difference if I pick C++ instead of C?
and what I will have good if I learn C more? I feel like if I go into C++ I will cover C within C++...
Regards
Use whichever language you know best right now.
Use neither C nor C++, they are not really required for general programming.
Use a high-level language that makes thing easy, such as Python
Of course it depends what kind of "daemon" you're writing, but in all likelihood, you want to be focusing your development effort on the task in hand, not fixing things like memory leaks, string handling or other distractions. Using neither C or (to a lesser extent) C++ will allow you to do this.
I can answer this one entirely in code. Writing a daemon roughly consists of doing this:
/*
* Daemon Initialisation:
* 1. Fork()
* 2. setsid()
* 3. Fork() do we need to do this twice?
* 4. Chdir /
* 5. Umask(0)
* 6. Close STDIN/OUT/ERR
* 7. Optionally re-open stuff.
*
* Refs:
* 1. http://www.faqs.org/faqs/unix-faq/programmer/faq/
* 2. http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
* 3. http://www.enderunix.org/docs/eng/daemon.php
*/
/* Variables */
/* Our process ID and Session ID */
pid_t pid, sid;
int fd = 0;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
* we can exit the parent process.
*/
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Fork off the parent process, again */
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the file mode mask */
umask(0);
/* Close all file descriptors */
for (fd = getdtablesize(); fd >= 0; --fd)
{
close(fd);
}
/* Open standard file descriptors from
* elsewhere.
* e.g. /dev/null -> stdin.
* /dev/console -> stderr?
* logfile as stdout?
*/
fd = open("/dev/null", O_RDWR); /* open stdin */
dup(fd); /* stdout */
dup(fd); /* stderr */
These are all C function calls, but of course, you can call them from C++.
The reason I have this code sat around is because it's actually a function I pass function pointers to which executes my "daemon body". Why do I do it this way? To work out where my errors are running the code directly as a process (if necessary with root privs) then I "daemonise". It's quite hard to debug a daemon process otherwise...
Edit: of course, using function pointers is a C way of thinking, but there's no reason you can't implement some form of class-based mechanism.
So, honestly, it really doesn't matter. Pick whichever you prefer.
as far as I'm concerned, C++ is C with optional extras. So go with that, and use whatever bits and pieces you feel comfortable with.
There is no reason to choose C instead of C++ if you are more comfortable with C++, and vice versa. They are both equally capable languages for this task.
Unless you are looking to become more comfortable with C, just use what you know.
Unless you are planning on doing kernel development or embedded programming, learning C++ is strictly better than learning C. The only thing you need to be careful of is that C++ "mangles" its function names, so that your function void foo()
in C++ will not be accessible directly by a C program. The trick is to declare it with C linkage, as in extern "C" void foo()
.
That said, C++ is a much bigger language than C, and it will definitely take more time to learn.
You should definitely use C++.
You should definitely use C, period.
精彩评论