Linux 服务和 cron 作业源
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这类问题的经典参考是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.
取决于您的应用程序的用途。
就我个人而言,我会保留一个线程仅用于处理信号,并在其他线程中调用 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 :)