How do I get started writing a daemon process in a Unix-like operating system, like Linux?
I am doing a tool in PHP for my personal use. But PHP is very slow and the task I need to do is takes much time, so I'll make a daemon in c++ and keep it in the background (It will run in a VPS).
PHP would connect to the daemon througt a simple tcp socket (I'll try to design/use a simple IPC protocol) in order to submit the task. The daemon will manage the tasks with a simple FIFO queue. PHP would only start the task, but it wouldn't wait until it is end, because it can take several minutes (PHP would monitor the task too - but that's another topic).
The web/PHP interface won't be a problem at all. But I have no experience on unix daemon programming. I have made simple console programs with c+开发者_如何学运维+ on linux, so I'll just need a manual/book/guide and advices to get started on unix daemon developing.
Thanks.
http://www.enderunix.org/docs/eng/daemon.php provides a fairly thorough but short introduction with sample code that seems to cover all the important bits. There's a much more in-depth description in "Advanced Programming in the UNIX Environment (2nd edition)" if you're willing to spend some money on paper (worth it, IMHO).
The link above does not deal with the init scripts, but you can probably figure that out with some searching - all you need is some way of starting the daemon code and some way of sending it signals later - typically, you write the PID of the daemon to a /var/run/XXX file.
精彩评论