<p>While implementing an applicative server and its client-side libraries in C++, I am having trouble finding a clean and reliable way to stop client processes on server shutdown on Windows.</p>

<p>Assuming the server and its clients run under the same user, the requirements are:</p>

<ul>
<li>the solution should work in the following cases:
  <ul>
  <li>clients may each feature either a console or a gui.</li>
  <li>user may be unprivileged.<li>
  <li>clients may be or become unresponsive (infinite loop, deadlock).</li>
  <li>clients may or may not be children of the server (direct or indirect).</li>
  </ul>
<li>unless prevented by a client-side defect, clients shall be allowed the opportunity to exit cleanly (free their ressources, sync some data to disk...) and some reasonable time to do so.</li>
<li>all client return codes shall be made available (if possible) to the server during the shutdown procedure.</li>
<li>server shall wait until all clients are gone.</li>
</ul>

<hr>

<p>As of this edit, the majority of the answers below advocate the use of a shared memory (or another IPC mechanism) between the server and its clients to convey shutdown orders and client status. These solutions would work, but require that clients successfully initialize the library.</p>

<p>What I did not say, is that the server is also used to start the clients and in some cases other programs/scripts which don't use the client library at all. A solution that did not rely on a graceful communication between server and clients would be nicer (if possible).</p>

<p>Some time ago, I stumbled upon a C snippet (in the MSDN I believe) that did the following:</p>

<ol>
<li>start a thread via CreateRemoteThread in the process to shutdown.</li>
<li>had that thread directly call ExitProcess.</li>
</ol>

<p>Unfortunately now that I'm looking for it, I'm unable to find it and the search results seem to imply that this trick does not work anymore on Vista. Any expert input on this ?</p>

[{'answer_id': 81812, 'author': 'Asaf Bartov', 'author_id': 7483, 'author_profile': 'https://Stackoverflow.com/users/7483', 'pm_score': 1, 'selected': False, 'text': "<p>You want some sort of IPC between clients and servers.  If all clients were children, I think pipes would have been easiest; since they're not, I guess a server-operated shared-memory segment can be used to register clients, issue the shutdown command, and collect return codes posted there by clients successfully shutting down.</p>\n\n<p>In this shared-memory area, clients put their process IDs, so that the server can forcefully kill any unresponsive clients (modulo server privileges), using TerminateProcess().</p>\n"}, {'answer_id': 81826, 'author': 'MSalters', 'author_id': 15416, 'author_profile': 'https://Stackoverflow.com/users/15416', 'pm_score': 0, 'selected': False, 'text': "<p>That's a very general question, and there are some inconsistencies.</p>\n\n<p>While it is a not 100% rule, most console applications run to completion, whereas GUI applications run until the user terminates them (And services run until stopped via the SCM). Hence, it's easier to request a GUI to close. You send them the equivalent of Alt-F4. But for a console program, you have to send them the equivalent of Ctrl-C and hope they handle it. In both cases, you simply wait. If the process sticks around, you then shoot it down (TerminateProcess) and pray that the damage is limited. But your HDD can fill up with temporary files.</p>\n\n<p>GUI application in general do not have exit codes - where would they go? And a console process that is forcefully terminated by definition does not exit, so it has no exit code. So, in a server shutdown scenario, don't expect exit codes.</p>\n\n<p>If you've got a debugger attached, you generally can't shutdown the process from another application. That would make it impossible for debuggers to debug exit code!</p>\n"}, {'answer_id': 82770, 'author': 'titanae', 'author_id': 2387, 'author_profile': 'https://Stackoverflow.com/users/2387', 'pm_score': 2, 'selected': False, 'text': '<p>If you use thread, a simple solution is to use a named system event, the thread sleeps on the event waiting for it to be signaled, the control application can signal the event when it wants the client applications to quit.</p>\n\n<p>For the UI application it (the thread) can post a message to the main window, WM_ CLOSE or QUIT I forget which, in the console application it can issue a CTRL-C or if the main console code loops it can check some exit condition set by the thread. </p>\n\n<p>Either way rather than finding the client applications an telling them to quit, use the OS to signal they should quit. The sleeping thread will use virtually no CPU footprint provided it uses WaitForSingleObject to sleep on.</p>\n'}, {'answer_id': 82794, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>If you are willing to go the IPC route, make the normal communication between client and server bi-directional to let the server ask the clients to shut down. Or, failing that, have the clients poll. Or as the last resort, the clients should be instructed to exit when the make a request to server. You can let the library user register an exit callback, but the best way I know of is to simply call "exit" in the client library when the client is told to shut down. If the client gets stuck in shutdown code, the server needs to be able to work around it by ignoring that client\'s data structures and connection.</p>\n'}, {'answer_id': 390464, 'author': 'Jason S', 'author_id': 44330, 'author_profile': 'https://Stackoverflow.com/users/44330', 'pm_score': 1, 'selected': False, 'text': '<p>Use PostMessage or a named event.</p>\n\n<p>Re: PostMessage -- applications other than GUIs, as well as threads other than the GUI thread, can have message loops and it\'s very useful for stuff like this. (In fact COM uses message loops under the hood.) I\'ve done it before with ATL but am a little rusty with that.</p>\n\n<p>If you want to be robust to malicious attacks from "bad" processes, include a private key shared by client/server as one of the parameters in the message.</p>\n\n<p>The named event approach is probably simpler; use CreateEvent with a name that is a secret shared by the client/server, and have the appropriate app check the status of the event (e.g. WaitForSingleObject with a timeout of 0) within its main loop to determine whether to shut down.</p>\n'}]
['https://Stackoverflow.com/questions/81727', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11892/']