Jerry Geis
2007-Oct-19 00:07 UTC
[CentOS] program calls system() gettind PID - slightly off topic
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. Thanks, Jerry
Garrick Staples
2007-Oct-19 00:34 UTC
[CentOS] program calls system() gettind PID - slightly off topic
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.You don't. At least not by using system(). Use fork() and exec() and you will have the child pid. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: <http://lists.centos.org/pipermail/centos/attachments/20071018/05ff4236/attachment-0004.sig>
William L. Maltby
2007-Nov-04 14:05 UTC
[CentOS] program calls system() gettind PID - slightly off topic
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>-- Bill