Linux 服务和 cron 作业源

发布于 2024-08-30 10:31:12 字数 182 浏览 2 评论 0原文

我是 Linux 新手,正在用 C++ 编写一个生成多个线程的服务,并且我通过从 init.d 调用它来启动该服务,但是我应该如何从脚本向我的应用程序发送终止信号,以便我的服务终止所有线程线程并退出。

还有哪里可以找到 Linux 服务的源代码。例如 /etc.init.d/rc5.d/S14cron 。这将有助于理解如何实施服务。

I am new to linux and writing a service in C++ which spawns multiple threads and I am starting the service by calling it from init.d, but how should I send the terminate signal to my application from the script , so that my service terminates all the threads and exits.

And also where can I find the source code for any linux services. e.g. /etc.init.d/rc5.d/S14cron . It will be helpful in understanding how to implement a service.

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

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

发布评论

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

评论(2

乖乖兔^ω^ 2024-09-06 10:31:12

这类问题的经典参考是Steven的《UNIX环境下的高级编程》。您可以在此处找到本书的源代码。

The classic reference for this kind of question is Steven's "Advanced Programming in the UNIX Environment". You can find the source code to this text book here.

往事随风而去 2024-09-06 10:31:12

取决于您的应用程序的用途。

就我个人而言,我会保留一个线程仅用于处理信号,并在其他线程中调用 sigprocmask 以停止向它们传递信号。

然后,主线程/信号处理线程(将其设为主线程通常是一个好主意)可以向其线程发送消息,告诉它们完成正在做的事情并退出。

或者,如果您喜欢仅崩溃的原则,您可以只调用 exit_group 并完成它:)

Depends what your application does.

Personally I'd keep a thread just for handling signals and call sigprocmask in the other threads to stop signals being delivered to them.

The main thread / signal handling thread (it is usually a good idea to make this the main thread) can then send a message to its threads to tell them to finish what they're doing and quit.

Alternatively, if you like the principle of crash-only, you could just call exit_group and be done with it :)

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