Cleanest way to exit processes?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Cleanest way to exit processes?

Post by stegemma »

I'm writing a new chess engine graphical interface, that create separated processes to control external engines, redirecting console I/O. At present it seems to works but I'm not sure about the way that I exit from the interface. I don't want that the interface hangs if an external process is somehow bugged, so I drastically terminate that processes this way:

Code: Select all

clsWxBoard::~clsWxBoard()
{
	if(pTimer)
	{
		pTimer->Stop();
		SmartDelete(pTimer);
	}
	for &#40;int i = 0; i < cllEngines.Count&#40;); i++)
	&#123;
		cllEngines&#91;i&#93;->Push&#40;"quit");
		cllEngines&#91;i&#93;->Terminate&#40;1000&#41;;
		cllEngines&#91;i&#93;->Join&#40;);
	&#125;
	cllEngines.Clear&#40;);
	objSatana.PushCommand&#40;"quit");
	objSatana.Join&#40;);
	delete bmpBoard;
	delete bmpSprites;
	return;
&#125;
clsWxBoard is derived from a wxWidgets component, so that I could port the code on Windows/Mac/Linux (actually only the Windows version works). What I'm asking is if it is ok to presume that in one second the external process has quit cleanly; my Terminate function does this:

Code: Select all

bool clsRedirectedChild&#58;&#58;Terminate&#40;int wait_ms&#41;
&#123;
	Sleep_ms&#40;wait_ms&#41;;
	return TerminateProcess&#40;pi.hProcess, 0&#41;==TRUE;
&#125;
Waiting for the process to terminates after the quit command sent maybe could lock my interface (that would not be good) but terminating a process with TerminateProcess could cause some corruption in its ini files or something similar.

Maybe I could try with some bugged engine, just to see what happens in any condition... if someone could suggest one ;)
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: Cleanest way to exit processes?

Post by xmas79 »

stegemma wrote:Maybe I could try with some bugged engine, just to see what happens in any condition... if someone could suggest one ;)
I don't think you'll ever find an engine bugged more than

Code: Select all

int main&#40;) &#123; while&#40;1&#41;; &#125;;
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Cleanest way to exit processes?

Post by stegemma »

xmas79 wrote:
stegemma wrote:Maybe I could try with some bugged engine, just to see what happens in any condition... if someone could suggest one ;)
I don't think you'll ever find an engine bugged more than

Code: Select all

int main&#40;) &#123; while&#40;1&#41;; &#125;;
Wow... you're a genius! It was that simple! ;)

Ok, with this mini-max bugged "engine" it works.

Thanks a lot!
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: Cleanest way to exit processes?

Post by xmas79 »

stegemma wrote:Wow... you're a genius!
I agree :)

Wait! I have one more buggy engine: take yours and in main search write:

Code: Select all

int search&#40;int alpha, int beta, ......)
&#123;
    ...
    while&#40;1&#41;;
    ...
&#125;
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: Cleanest way to exit processes?

Post by xmas79 »

stegemma wrote:
xmas79 wrote:
stegemma wrote:Maybe I could try with some bugged engine, just to see what happens in any condition... if someone could suggest one ;)
I don't think you'll ever find an engine bugged more than

Code: Select all

int main&#40;) &#123; while&#40;1&#41;; &#125;;
Wow... you're a genius! It was that simple! ;)

Ok, with this mini-max bugged "engine" it works.

Thanks a lot!
Oh and another one more:

take yours and in the protocol interfacing code write randomly:

Code: Select all

...
while&#40;1&#41;;
...
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Cleanest way to exit processes?

Post by stegemma »

xmas79 wrote:
stegemma wrote:Wow... you're a genius!
I agree :)

Wait! I have one more buggy engine: take yours and in main search write:

Code: Select all

int search&#40;int alpha, int beta, ......)
&#123;
    ...
    while&#40;1&#41;;
    ...
&#125;
You must cheat the compiler... so I've try this one:

Code: Select all

int search&#40;int alpha, int beta, ......)
&#123;
    ...
    while&#40;++nodescount&#41;;
    ...
&#125;
Still it works right but I'm sure that the first user ever could find a way to make it hangs...
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Cleanest way to exit processes?

Post by Sven »

An "engine" that permanently does calculations (while (++nodecount)) is probably easy to kill. But what if that "engine" itself is hanging while waiting for an event (e.g. input) that does not come? Or one that performs a huge amount of disk I/O? Or one that allocates lots of resources (e.g. memory) which need to be freed?
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Cleanest way to exit processes?

Post by stegemma »

Sven Schüle wrote:An "engine" that permanently does calculations (while (++nodecount)) is probably easy to kill. But what if that "engine" itself is hanging while waiting for an event (e.g. input) that does not come? Or one that performs a huge amount of disk I/O? Or one that allocates lots of resources (e.g. memory) which need to be freed?
In fact I expect that this kind of engine can give some problem. The test that I've done is related to Satana, that use separate threads for console I/O, for the search and for the main loop that handle commands.

The memory allocated by a process I hope that would be released if you kill the process (not very sure for this and maybe it is OS related). Not the same for other resources, for sample GDI resources on Windows and other system resources. Of course we talk about chess engines and we can expect that they would open sygyzy files, ini files, pgn files and so on; I don't expect that a standard engine can needs windows and other graphical resources... ops... my old program Raffaela just do it! ;)

And what about a software that spoke the moves with a sexy voice? It could lock the audio API forever (even if the voice isn't sexy, of course).

PS: tested on Raffaela and it works; almost the memory seems to be released correctly but maybe I should run it some thousands times, to notice some lost resource
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Cleanest way to exit processes?

Post by Sven »

The memory, or other resources, may be released correctly but my point was that this may delay process termination in some cases.
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Cleanest way to exit processes?

Post by stegemma »

Sven Schüle wrote:The memory, or other resources, may be released correctly but my point was that this may delay process termination in some cases.
Yes, maybe a variable timeout parameter is needed, for some engine, to terminate the process as later as possible, without hanging too long the interface itself.
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com