最好的方式写一个linux守护进程 [英] best way to write a linux daemon

查看:130
本文介绍了最好的方式写一个linux守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于工作,我需要编写一个tcp守护程序来响应我们的客户端软件,并想知道是否有任何提示,以最好的方式来解决这个问题。

For work i need to write a tcp daemon to respond to our client software and was wondering if any one had any tips on the best way to go about this.

推荐答案

这取决于你的应用程序。线程和分叉都可以是完全有效的方法,以及单线程事件驱动模型的第三个选项。

It depends on your application. Threads and forking can both be perfectly valid approaches, as well as the third option of a single-threaded event-driven model. If you can explain a bit more about exactly what you're writing, it would help when giving advice.

对于有价值的东西,这里有一些一般的指导原则:

For what it's worth, here are a few general guidelines:


  • 如果您没有共享状态,请使用forking。

  • 如果您有共享状态,

  • 如果您在大量连接下需要高性能,请避免分支,因为它具有更高的开销(特别是内存使用)。而是使用线程,事件循环或几个事件循环线程(通常每个CPU一个)。

实现,因为你可以基本上忽略所有其他连接一旦你叉;由于额外的同步要求,下一个最难处理的线程;事件循环更加困难,因为需要将你的处理转化为状态机;和多线程运行事件循环的最困难的(由于结合其他因素)。

Generally forking will be the easiest to implement, as you can essentially ignore all other connections once you fork; threads the next hardest due to the additional synchronization requirements; the event loop more difficult due to the need to turn your processing into a state machine; and multiple threads running event loops the most difficult of them all (due to combining other factors).

这篇关于最好的方式写一个linux守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆