Shutting down a server

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Shutting down a server

Post 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?
Only Human
erikgreenwald

Re:Shutting down a server

Post 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
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Shutting down a server

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Shutting down a server

Post by Neo »

I am going through the link you gave.
I have a question though.
How do you write a daemon in Java?
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Shutting down a server

Post by Neo »

Guess its not that easy huh.
http://jakarta.apache.org/commons/daemon/
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Shutting down a server

Post 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.
Only Human
Post Reply