|
Home > Archive > Unix Programming > March 2004 > Killing previously started processes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Killing previously started processes
|
|
|
| Xref: intern1.nntp.aus1.giganews.com comp.unix.programmer:144461
How can I kill previously started process reliably?
One idea I have is to save the process id when creating a new process. When
killing the process, the saved pid could be used. Only problem here is, that
if the process has already died and new process has been created into the
same pid, I would kill a wrong process.
What would be a sophisticated way to do this?
-S-
| |
| Loic Domaigne 2004-03-15, 8:34 am |
| Hi,
> How can I kill previously started process reliably?
> One idea I have is to save the process id when creating a new process. When
> killing the process, the saved pid could be used. Only problem here is, that
> if the process has already died and new process has been created into the
> same pid, I would kill a wrong process.
> What would be a sophisticated way to do this?
There are several solutions, both with pro and cons. At application level,
one way would be to implement an IPC (socket for instance), and send an
explicit "shutdown message". That's the *look-like-pro* solution.
.... But, there might be easier solutions, depending on your requirements.
For instance, in some circumstances, a launcher script does it!
(This launcher script does nothing but start the process and indicate that
the process is indeed running.)
Regards,
Loic.
--
Article posté via l'accès Usenet http://www.mes-news.com
Accès par Nnrp ou Web
| |
| Nils O. Selåsdal 2004-03-15, 9:37 am |
| In article <40558c0c$1@news.vip.fi>, S wrote:
> How can I kill previously started process reliably?
>
> One idea I have is to save the process id when creating a new process. When
> killing the process, the saved pid could be used. Only problem here is, that
> if the process has already died and new process has been created into the
> same pid, I would kill a wrong process.
>
> What would be a sophisticated way to do this?
open a pid file.
ret = lockf(pidfd,F_TLOCK,0);
if you didn't get the lock, another process(yours)
IS running and holding the lock.
read the lockfile , get the
pid, kill it (very small race here.)
or, you got the lock,(no other process is running)
write pdi to the file. Don't close the filedescriptor.
--
Vennlig hilsen/Best Regards
Nils Olav Selåsdal
System Engineer
w w w . u t e l s y s t e m s . c o m
| |
| Casper H.S. Dik 2004-03-15, 9:37 am |
| "S" <kysy.jos@kiinnostaa.com> writes:
>How can I kill previously started process reliably?
>One idea I have is to save the process id when creating a new process. When
>killing the process, the saved pid could be used. Only problem here is, that
>if the process has already died and new process has been created into the
>same pid, I would kill a wrong process.
>What would be a sophisticated way to do this?
If the process is a child of the process which needs to do the
checkign this is fairly simple: when the child dies you will be notified
and the child becomes a zombie. Until the zombie is reaped using wait(2)
the pid cannot be recycled.
Casper
| |
| Brian Raiter 2004-03-15, 6:35 pm |
| >> How can I kill previously started process reliably?
> open a pid file.
> ret = lockf(pidfd,F_TLOCK,0);
Or, use fcntl(2) with F_GETLK and F_SETLK. The advantage here (may not
be available on some Unices) is that if the file is locked, F_GETLK
will report the pid of the process holding the lock. This obviates the
necessity of having the program store its own pid in the file.
b
|
|
|
|
|