Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Multiple Daemons - same script?

by kryptonite (Initiate)
on Jul 21, 2005 at 14:00 UTC ( [id://476836]=perlquestion: print w/replies, xml ) Need Help??

kryptonite has asked for the wisdom of the Perl Monks concerning the following question:

I've searched high and low, but can't seem to find a concise answer to this question: Is there a way to have two daemons running simultaneously from one perl script? The closest I've gotten is to have them run sequentially, daemon1 followed by daemon2. What I'd like to achieve is to have daemon1 run at the same time as daemon2, while each performs a different activity. Any advice would be greatly appreciated!

Tks!

Replies are listed 'Best First'.
Re: Multiple Daemons - same script?
by rev_1318 (Chaplain) on Jul 21, 2005 at 14:19 UTC
    What have you tried so far? Can you show us some sample code?

    Essentially, it could be reolved with multiple forks, as in following (speudo) code:

    if (fork) { exec "daemon1" } if (fork) { exec "daemon2" }

    Paul

Re: Multiple daemons in one script?
by jdhedden (Deacon) on Jul 21, 2005 at 14:38 UTC
    Your confusion may stem from the notions of how daemons are usually managed using a PID file that is named after the script. If you use Proc::PID::File, you can specify the PID file name, and thus support multiple instances of a daemon from the same source file.

    Remember: There's always one more bug.
Re: Multiple daemons in one script?
by ikegami (Patriarch) on Jul 21, 2005 at 14:32 UTC
    I think you can use select RBITS,WBITS,EBITS,TIMEOUT on the server sockets then call accept on the one that returns. Sorry, I don't have time to elaborate or to try this at the moment.
Re: Multiple Daemons - same script?
by pbeckingham (Parson) on Jul 21, 2005 at 15:56 UTC

    Please consider a routine shutdown/restart of your daemon. As with any daemon, let alone one written in Perl, it is imperative that there be no memory leaks, and given the difficulty in achieving this in anything but trivial cases, I suggest an automatic periodic restart.



    pbeckingham - typist, perishable vertebrate.
Re: Multiple daemons in one script?
by Anonymous Monk on Jul 21, 2005 at 14:16 UTC
    Yes, that's possible. What kind of daemons are they? Do they listen to ports and respond to requests? Do they perform background tasks? You will probably need an event loop of some kind of another.

    Having said that, it might be a lot easier to split the code and run two processes. If only by doing an early fork().

Re: Multiple Daemons - same script?
by samizdat (Vicar) on Jul 21, 2005 at 15:04 UTC
    Daemons, once launched, are always running. Once you have FORKed it, (and gotten the PID as described by the previous poster), you can open socket connections to both and communicate as simultaneously as your OS's scheduler will let you. Both daemons and your original process will remain running until you terminate them; that's the nature of daemons.
Re: Multiple Daemons - same script?
by jdhedden (Deacon) on Jul 21, 2005 at 14:40 UTC
    Your confusion may stem from the notions of how daemons are usually managed using a PID file that is named after the script. If you use Proc::PID::File, you can specify the PID file name, and thus support multiple instances of a daemon from the same source file.

    Remember: There's always one more bug.
Re: Multiple Daemons - same script?
by kryptonite (Initiate) on Jul 21, 2005 at 17:00 UTC
    Thanks, all, for the feedback! As I'm fairly new at this, the notion of how daemons work is still somewhat of a black box, and some approaches described here are a little confusing (so you are right, jdhedden!). The daemons will be handling data of different types sent to a single script. Datatype A will be handled by daemon1, Datatype B will be handled by daemon2 and both types are coming from different sources. I am unable to provide code examples since my code rests on a private network, but I'm going to try to implement some form of the suggestions here. I'm sure I'll be back with more questions. Thanks again!

      I just recommended this book on another thread, Saving big blessed hashes to disk, and it's even more appropriate here: "Network Programming with Perl" ISBN 0201615711. It goes into detail about preventing memory leaks, long term, with daemons on different platforms and nice ways to encapsulate things and do dispatch stuff.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://476836]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found