ps uses an unstable, non futur proof API: sysctlbypid KERN_PROCARGS2. (see print.c file in ps sources)
Le 2 sept. 2009 à 20:12, Rakesh Singhal a écrit : Hi all,
Thanks all. My internet connection was down so I could not reply. Solution suggested by Terry using popen() and read argument is right, but I want to implement without using shell or system() or any other script. As I know, proc is not supported by Mac, it is available only in Linux or Solaris, right?
In my query, I mentioned about using code from http://developer.apple.com/mac/library/qa/qa2001/qa1123.html. As suggested by Stephen, I checked code of ps but I could not find that from where it was getting arguments. Already I am using same method as is given in source of ps. I think, kp[i]->kp_proc->p_comm gives only process name, not arguments also.
Regards, rksinghal
On Wed, Sep 2, 2009 at 1:37 PM, Terry Lambert <email@hidden> wrote:
On Sep 1, 2009, at 9:38 PM, Stephen J. Butler wrote:
On Tue, Sep 1, 2009 at 9:55 AM, Rakesh Singhal<email@hidden> wrote:
I can find pid for any running process using code given here
http://developer.apple.com/mac/library/qa/qa2001/qa1123.html. But I want to
find the arguments passed to this running process, when it was launched. I
checked the kinfo_proc structure but I could not find any thing related to
arguments.
To actually answer the question, look at the source code for "ps":
<http://www.opensource.apple.com/source/adv_cmds/adv_cmds-119/ps.tproj/ps.c>
It calls sysctl with { CTL_KERN, KERN_PROC, what, flags } (look a
little higher up for what gets stuffed in those vars). From the
result, it then looks at "kp[i]->kp_proc->p_comm" (saveuser function).
< http://developer.apple.com/mac/library/qa/qa2001/qa1123.html>
"The UNIX Programming FAQ lists a number of alternative ways to do this.
Of these, the only approach that works on Mac OS X is exec'ing the ps
command line tool. exec'ing ps will require parsing the tool's output
and will not use system resources as efficiently as Listing 1."
...on the other hand, the ps method is more likely to continue to work in future versions of the OS. Also note that qa is 7 or 8 years old, based on how you read the dates.
-- Terry
-- Jean-Daniel
|