Page 1 of 1

Shutting down a server

Posted: Thu Apr 28, 2005 6:30 am
by Neo
How exactly do servers shut themselves down?
For e.g. a Web server it just keeps waiting for clients in a infinite loop. So how is it supposed to actually exit itself.
One way I can think of is by using a signal handler. I mean when the server is started initially it "takes over" a certain port after this if we try to start the same server again we will be unable to do so as the port will be occupied. This lets us know that our(?) server is already running.
Now if I need to shutdown this server I send a special arg say -close and this sends a signal to the running server (how do i get the PID now?) which terminates itself.

Is there a cleaner way to do this?
And yeah the main thing. How is this done in JAVA?

Re:Shutting down a server

Posted: Thu Apr 28, 2005 4:53 pm
by erikgreenwald
Apache has a 'master' thread that takes a signal and asks the threads to all terminate themselves (close the listening socket , wait for the current sockets to finish up and close, then exit), followed by the master exiting (so clients still get whole files). The signal approach is pretty common in unix, with the daemon putting it's PID in a pid file somewhere like /var/run.... seems pretty clean to me :)

Another technique I've seen used is to have a direct communication channel with a shutdown command (perhaps password protected 'admin' mode).

(whuddya mean by 'java'? jvm's have loads of interesting facilities that allow things like object spaces (tuple space), rmi, various jvm interfaces .... it's pretty much an emulated os...)

-Erik

Re:Shutting down a server

Posted: Thu Apr 28, 2005 8:16 pm
by Colonel Kernel
This might help you:

http://builder.com.com/5100-6370-5144546.html

I usually just make the background threads test a "cancel" flag in their loop, and exit if the flag is true. Testing and setting the flag must be done atomically.

Re:Shutting down a server

Posted: Thu Apr 28, 2005 9:12 pm
by Neo
I am going through the link you gave.
I have a question though.
How do you write a daemon in Java?

Re:Shutting down a server

Posted: Thu Apr 28, 2005 9:18 pm
by Neo
Guess its not that easy huh.
http://jakarta.apache.org/commons/daemon/

Re:Shutting down a server

Posted: Sat Apr 30, 2005 11:27 pm
by Neo
I guess the Java "write once, run everywhere" thing was just a hype.
There seems to be no way you can write a daemon entirely in Java that would run on both *nix and Windows. It seems it would need separate binary helpers (in C most probably) for each platform to make the Java server run as a daemon/service.