ok I have 3 questions:
Q1) so that means, the shell script will have to issue the kill <pid> command to kill my server process?
Q2) and my server would have to catch the SIGTERM signal and perform any cleanups or complete any outstanding transactions before exiting?
Q3) How do I check within my program if another instance of it is already running?
As for third question, a variety of ways. You can create a "lock" file in /var somewhere (as a temporary file). Existence of the file means the server is already running.
You don't say which operating system this is for. The files in init.d are all pretty similar. This article gives a good overview of initscripts on Linux. Towards the bottom of the article you will find a tutorial for writing an init script.
The init script functions library, which provides a number of useful init script functions, is mentioned briefly in that tutorial and documented in more detail here:
and when the signal handler receives and sets the exitFlag to true, accept is in the blocking state, how do I make accept return and exit out of the loop?
You will want your main program to register a signal handler for SIGTERM and then you'll want all of your threads to block SIGTERM. This will cause the signal to be received by the main program, which can then proceed to do pthread_cancel()s on all the threads and perform any other cleanup necessary.
Using OS provided signals works but an alternative solution is for you to build your own customized signal interrupt concept. Could be a TCP socket waiting for some client request (most likely coming from an Administrator client program) that send a SHUTDOWN, RESTART, START etc etc whatever commands. Of cuz since signals is OS level, you also need to build your own interrupt routines so most developers I have seen go straight to the OS signal approach. Effective, less time consuming and it works.
PS I also read elsewhere for some Unixes, the signal timing is not very reliable but I would presume those problems exist only for old Unix version. Newer Unix versions like Linux should not suffer from this syndrome.