[CentOS] program calls system() gettind PID - slightly off topic

William L. Maltby CentOS4Bill at triad.rr.com
Sun Nov 4 14:05:53 UTC 2007

On Thu, 2007-10-18 at 17:34 -0700, Garrick Staples wrote:
> On Thu, Oct 18, 2007 at 08:07:46PM -0400, Jerry Geis alleged:
> > I have a C program on centos. When I call system("some_program &");
> > how do I get the PID of the some_program?
> > 
> > I cant just list processes as there might be more than one some_program 
> > active.
> > I need to know the one that I just started.

Is each instance of "some_program" spawned from the same parent? If not,
the PPID of the child can be used to differentiate between the children.

> You don't.  At least not by using system().
> Use fork() and exec() and you will have the child pid.

Unfortunately (maybe), the environment that is provided by the system
call (the command interpreter is invoked - /etc/profile, /etc/bashrc et
al may come into play) is not established with fork and the exec* family
of commands. Programming effort may be needed if this is important.

Changing the invocation to some variation of

    "/bin/bash some_params some_program &"

might do the trick when using the fork/exec combo.

> <snip sig stuff>


More information about the CentOS mailing list