views:

77

answers:

1

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.

+2  A: 

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.

Joost Diepenmaat
The link above only has a single fork. The canonical method, and the one I believe "Advanced Programming in the UNIX Environment" suggests, is that you should fork twice. http://stackoverflow.com/questions/881388/what-is-the-reason-for-performing-a-double-fork-when-creating-a-daemon
JimB