Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Getting a Process ID

Getting a Process ID
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 26, 2003, 12:22 AM
 
I'm building a Cocoa app that uses NSTask to launch a shell script; the shell script, in turn, launches a Java Virtual Machine (JVM) process. I need to get the PID for the JVM process into the Cocoa app.

I can certainly get the PID for the shell script via:

taskPID = [task processIdentifier];

But don't know how to get the PID for the JVM. Any ideas on how I can read the JVM PID into my Cocoa app?

Thanks,
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 26, 2003, 01:31 PM
 
Another approach would be to have my Cocoa app monitor the lifecycle of a Unix process that it did not directly start.

For example,

1. Launch My.app and it would watch for Terminal.app launch events.

2. When I launch the Terminal.app (from the dock) My.app would display a message indicating that Terminal.app was launched along with with its Unix PID.

3. When I quit the Terminal.app, then My.app displays a message indicating that Terminal.app has exited.

Any ideas, on how to approach this scenario?
All I found is good facilities for processes that are launched by a Cocoa application but nothing about monitoring existing processes.

I figure that periodically polling the system through an NSTask that invokes "ps -ax" and then have My.app parse the output for PIDs is inefficient and ugly. There's gotta be a better way, right?


(Last edited by DaGuy; May 26, 2003 at 11:26 PM. )
     
Forum Regular
Join Date: Aug 2000
Location: UK
Status: Offline
Reply With Quote
May 26, 2003, 05:23 PM
 
Originally posted by DaGuy:

All I found is good facilities for processes that are launched by a Cocoa application but nothing about monitoring existing processes.
Look in the NSWorkspace docs:

NSWorkspaceWillLaunchApplicationNotification

NSWorkspaceDidTerminateApplicationNotification

It only works for apps that appear in the Dock, but I think that's what you wanted.
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 27, 2003, 01:47 AM
 
Originally posted by calumr:
Look in the NSWorkspace docs:

NSWorkspaceWillLaunchApplicationNotification

NSWorkspaceDidTerminateApplicationNotification

It only works for apps that appear in the Dock, but I think that's what you wanted.
Thanks, I wasn't aware of NSWorkspace. As you mentioned (and unfortunately for me) it only picks up dock launched applications.

It would certainly work well for the scenario that I described above but going back to my first post, It's a Java process that I'm really interested in. Perhaps picking the Terminal.app to illustrate my point wasn't very wise. In any event, I added NSWorkspace to my repertoire -and that's a good thing.

Any ideas on how to get the same sort of info (app name, PID) but not just for doc launched processes but for any running process?

Thanks,
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 27, 2003, 03:04 AM
 
It sounds like what you're looking for is sysctl() (declared in sys/sysctl.h). Apple has an example that ought to put you on the right track.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 27, 2003, 11:36 AM
 
Originally posted by Chuckit:
It sounds like what you're looking for is sysctl() (declared in sys/sysctl.h). Apple has an example that ought to put you on the right track.
Yes, this is exactly what I've been looking for!
Thanks a whole bunch. I forgot to check for the regular C stuff -a.k.a. Cocoa obsession.


     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2