From: Jonathan de Boyne Pollard on
>
>
> On Windows I would call OpenPorocess to get the process handle and
> then use WaitForSingleObject to wait on the handle. I wonder why UNIX
> does not have something similar?
>
Because the Win32 API took advantage of the second system effect. The
world gradually learned over the years that handle-based APIs are good
things, especially because they eliminate all sorts of race conditions
that open up security holes and cause other problems. But the POSIX API
has evolved towards this from non-handle-based paradigms in some areas,
at varying rates in various parts of the API. It still hasn't fully
arrived. So whilst there are various non-portable ways to control
processes through handle-based APIs, that vary across systems such as
Linux, Solaris, the BSDs, and Plan 9, the portable, POSIX, API still
only supports waiting on (and otherwise controlling) processes by their
IDs, and even then only waiting on process IDs of direct child processes.

There are more reasons for such restrictions today than there were
decades ago, in fact. Notice that in recent (2.6.24 and later) versions
of Linux with certain features enabled, one cannot even necessarily name
all processes on a system by PID from within an arbitrary non-parent
process, because they might be in a different, non-contained, PID namespace.

From: Barry Margolin on
In article <82755qFp8jU8(a)mid.individual.net>,
Ian Collins <ian-news(a)hotmail.com> wrote:

> On 04/ 9/10 10:21 AM, Ersek, Laszlo wrote:
> > On Fri, 9 Apr 2010, Ian Collins wrote:
> >
> >> On 04/ 9/10 03:02 AM, Rainer Weikusat wrote:
> >>> Sasha<agalkin(a)audible.com> writes:
> >>>
> >>> [...]
> >>>
> >>>> I need to wait for already running process to track when it exits. I
> >>>> know how to do this for the child process spawned by the watcher
> >>>> process, I need now to do the same for the process already running.
> >>>
> >>> "man ptrace"
> >>
> >> Do some platforms extend ptrace to control non-child processes?
> >
> > Absolutely; you can attach gdb to an already running, unrelated process.
> >
> OK, thanks. The online Linux man page
> (http://linux.die.net/man/2/ptrace) was a little ambiguous:
>
> "The ptrace() system call provides a means by which a parent process may
> observe and control the execution of another process"
>
> Note the use of parent and another rather parent and child.

Read down to PTRACE_ATTACH:

PTRACE_ATTACH
Attaches to the process specified in pid, making it a traced "child" of
the current process; the behavior of the child is as if it had done a
PTRACE_TRACEME. The current process actually becomes the parent of the
child process for most purposes (e.g., it will receive notification of
child events and appears in ps(1) output as the child's parent), but a
getppid(2) by the child will still return the PID of the original parent.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***