Board index » kylix » Getting a kylix daemon to wait for termination.....

Getting a kylix daemon to wait for termination.....


2004-09-04 09:42:49 PM
kylix0
I'm using TIdTCPServer within a Kylix daemon.
When I terminate the server, it takes a few seconds for all the threads to
terminate. I want to wait for them to finish before the terminal is released
back to the user.
I'm sure that what I need is the WaitFor method, but I can't find any useful
resources as to how to utilise it.
Can anyone help?
In essence, a user will call "./MyApplication -stop" - here's the "-stop"
part of the daemon:-
if SigNum <>0 then
begin
if FileExists( '/var/run/myapp.pid' ) then
begin
AssignFile(F, '/var/run/myapp.pid' );
Reset( F );
Read( F, pid );
CloseFile( F );
kill( pid, SigNum );
<------ I need to wait here for all threads to terminate ------->
Halt( 0 );
Where do I put the WaitFor, and what is the syntax?
Thanks,
Dean
 
 

Re:Getting a kylix daemon to wait for termination.....

The waitpid() function should be enough:
uses Libc;
if waitpid(pid, nil, 0) <>pid then
WriteLn('Error terminating the daemon.');
--
Regards,
Andreas Hausladen
(www.kylix-patch.de.vu - unofficial Kylix 3 patches)
 

Re:Getting a kylix daemon to wait for termination.....

Hi Andreas,
Thanks but that doesn't work:-
kill(pid, Signum);
if waitpid(pid, nil, 0) <>pid then
WriteLn('Error terminating the daemon.');
Just always outputs "Error terminating.....".
The result of waitpid(..) always outputs -1.
I need to loop something I think. So check to see if any PID threads are
running, and wait until they have all terminated.
Dean
"Andreas Hausladen" < XXXX@XXXXX.COM >wrote in message
Quote
The waitpid() function should be enough:

uses Libc;

if waitpid(pid, nil, 0) <>pid then
WriteLn('Error terminating the daemon.');


--
Regards,

Andreas Hausladen
(www.kylix-patch.de.vu - unofficial Kylix 3 patches)
 

{smallsort}

Re:Getting a kylix daemon to wait for termination.....

OK, after a lot of searching and reading it seems it will never work, as
each thread has it's own PID.
Is there a way to get a thread's PID by process name?
eg:-
GetPID('myapp');
Or something similar? So I can wait for a -1 or nil return or something,
then I know that my threads are all gone?
Thanks,
Dean
 

Re:Getting a kylix daemon to wait for termination.....

public.borland.com wrote:
Quote
Or something similar? So I can wait for a -1 or nil return or something,
then I know that my threads are all gone?
Why not simply delete the file with the pid in the server? The killing
application can then test for file existance.
--
Regards,
Andreas Hausladen
(www.kylix-patch.de.vu - unofficial Kylix 3 patches)
 

Re:Getting a kylix daemon to wait for termination.....

Quote
Why not simply delete the file with the pid in the server? The killing
application can then test for file existance.
That only contains the PID for the main process.
Thanks for your help - but it's ok, I answered my own question in the end -
the answer is : it isn't possible (well you could GREP a ps ax output, but
that's too ugly ;-)).
Dean
 

Re:Getting a kylix daemon to wait for termination.....

It will work,
as each connection occurs put the handle of each thread in a tstringlist or
whatever, then in the on disconnect remove the handle from the list.
then on daemon shut down loop through the remaining handles in the list and
shut down each socket via the handle.
"public.borland.com" < XXXX@XXXXX.COM >wrote in message
Quote
OK, after a lot of searching and reading it seems it will never work, as
each thread has it's own PID.

Is there a way to get a thread's PID by process name?

eg:-

GetPID('myapp');

Or something similar? So I can wait for a -1 or nil return or something,
then I know that my threads are all gone?

Thanks,

Dean


 

Re:Getting a kylix daemon to wait for termination.....

public.borland.com wrote:
Quote
OK, after a lot of searching and reading it seems it will never work, as
each thread has it's own PID.

Beware ! That is different with Kernel 2.6.x. Here Kylix automatically
uses the new thread method, that AFAIK uses the projects PID for all
it's threads.
-Michael
 

Re:Getting a kylix daemon to wait for termination.....

Quote
then on daemon shut down loop through the remaining handles in the list
and
shut down each socket via the handle.
I don't want to terminate them again (they've already been told to
terminate) - I just want to know when they've terminated. Indy is blocking
so this can sometimes take a while.
D