需要在linux下写一个守护进程,不知道用什么C++或C

发布于 2024-10-11 04:37:52 字数 228 浏览 5 评论 0原文

我在选择正确的语言来编写我的守护程序时遇到了一些问题, 我对 C 和 C++ 感到困惑,我想使用 C++,因为它比 C 更具扩展性, 但我想使用 C,因为它是 linux 中一切的起点,

我想选择 C++,因为我有很多关于它的资源,所以,如果我选择 C++ 而不是 C,会有什么区别吗?

如果我多学习 C 语言,我会有什么好处? 我觉得如果我进入 C++,我会在 C++ 中涵盖 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 everything 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

热鲨 2024-10-18 04:37:52

使用您现在最熟悉的语言。

Use whichever language you know best right now.

稚气少女 2024-10-18 04:37:52

既不要使用 C 也不要使用 C++,一般编程并不真正需要它们。

使用使事情变得简单的高级语言,例如 Python

当然,这取决于你是哪种“守护进程”写作,但很可能,您希望将开发工作集中在手头的任务上,而不是修复内存泄漏、字符串处理或其他干扰等问题。既不使用 C 也不使用 C++(在较小程度上)将允许您执行此操作。

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.

镜花水月 2024-10-18 04:37:52

我可以完全用代码来回答这个问题。编写守护程序大致包括以下操作:

/*
 * 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 */

这些都是 C 函数调用,但当然,您可以从 C++ 调用它们。

我之所以保留这段代码,是因为它实际上是一个函数,我将函数指针传递给它来执行我的“守护程序主体”。我为什么要这样做?为了找出我的错误直接作为进程运行代码的位置(如果需要使用root权限),然后我“守护进程”。否则调试守护进程是相当困难的...

编辑:当然,使用函数指针是一种 C 思维方式,但没有理由不能实现某种形式的基于类的机制。

所以说实话,这真的不重要。选择您喜欢的。

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.

゛时过境迁 2024-10-18 04:37:52

就我而言,C++ 是具有可选附加功能的 C。因此,就这样吧,使用任何你觉得舒服的东西。

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.

≈。彩虹 2024-10-18 04:37:52

如果您更喜欢 C++,则没有理由选择 C ​​而不是 C++,反之亦然。它们都是同样有能力完成这项任务的语言。

除非您希望更熟悉 C,否则只需使用您所知道的即可。

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.

謌踐踏愛綪 2024-10-18 04:37:52

除非您打算进行内核开发或嵌入式编程,否则学习 C++ 绝对比学习 C 好。您唯一需要注意的是 C++“破坏”了其函数名称,因此您的函数 void foo( ) C++ 中的 C 程序无法直接访问。诀窍是使用 C 链接来声明它,如 extern "C" void foo() 中。

也就是说,C++ 是一种比 C 更大的语言,并且肯定需要更多的时间来学习。

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.

怕倦 2024-10-18 04:37:52

你绝对应该使用 C++。

You should definitely use C++.

吐个泡泡 2024-10-18 04:37:52

你绝对应该使用C,就这样。

You should definitely use C, period.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文