View Single Post

   
  #3 (permalink)  
Old 01-16-2008, 05:37 PM
Vasantharaj
 
Posts: n/a
Default Re: How to get the pid of another process

Hi

Thank you for the reply.
But, if the "some_prog" is a demon, the system() will return and
continue isn't it ? So, in that case is there any method to get the
pid of "some_prog" other than using IPC?

thanks and regards,
vasanth.

dmorris@cup.hp.com wrote in message news:<4010120a@usenet01.boi.hp.com>...
> Vasantharaj <vasantharajg@nestec.net> wrote:
> > Hi all

>
> > How to get the process id inside the c code. My requirement is like ... I
> > may execute a system call inside a c PROGRRAM, and in the next step I wud
> > like to know what is the process id of the previously executed program ?

>
> > {..
> > ...
> > system("/usr/bin/some_prog");
> > // Next I would like to know the pid of some_prog

>
>
> > }

>
> > How to get it ? Any lib call or system call for this ?

>
> > regards,
> > vasanth.

>
> The way you've written the above? I don't think you can.
> According to the system(3S) man page -- system() does
> the equivalent of a fork()/exec()/wait() for the target
> program. So by the time flow of control returns to your
> code, the child process is already gone. The pid of that
> child may well have been re-used for something else.
>
> What I would do here is to replace system() with a fork(),
> which will return the pid of the child process. The child
> process can then exec() "/usr/bin/sh" with the argument
> "/usr/bin/some_prog" [or just exec some_prog directly
> if this is a binary and not a script] and the parent
> can wait() on it. Just like system() -- but with the
> child pid visible before it exits.
>
> Don

Reply With Quote