PDA

View Full Version : get the process names and id




chidambaram
Sep 18, 2008, 08:15 AM
hi all,

i am working in Carbon c application(Mac OS 10.4.7)

i want to get the process names by using a C program.

i want to know any structures or any C functions which can retrieve the process details which are currently running.

i want to get the handle of that processess.

Actually my requirement is i have to trace one process which is selected by user for when user closes that process.

thanks in advance....



lee1210
Sep 18, 2008, 09:48 AM
man getpid

pid_t getpid(void);

Returns the process ID of the current process.

That will cover the process ID. Otherwise there's not a very good way, in my opinion, to get the image name in a "safe", platform-agnostic manner. The way i would do it would be to store argv[0], if it exists, when the process starts. Then have some method you can call to retrieve that value. There are means by which processes can be started where this will not be available. In those cases, I am not sure what the tact should be. I can tell you how to do it on OS X, Linux, HP-UX, etc. but none of them are the same. To make something that would behave the same on HP-UX and Linux I wrote a wrapper to pstat_getproc from HP-UX that would, based on compile flags, either call pstat_getproc on HP-UX or parse the /proc filesystem on linux. This is not enjoyable, and I would not recommend it.

I guess if you're using Carbon, you're already tied to one platform, so it may not be that big of a deal to do it in a system specific manner. There may also be a carbon function that lets you get this information.

Now that I re-read your post, I'm not sure if you're talking about the current process, or just getting a list of other processes. If you're wanting to look at all of the current processes, i would probably rely on something like ps. The key for process ID is p, the key for program name is c.

I'm not sure what kind of tracing you want to do, exactly. Are you just needing to monitor that a process is running? Do you need to attach to the process like gdb and see it's current stack information, etc? Do you just need to log the system calls it makes? This makes a big difference in how you'd approach this problem.

-Lee

kainjow
Sep 18, 2008, 10:48 AM
There should be events in Carbon you can handle that get called when a process exits. I don't know off the top of my head, but look around, it should be easy to find.

Edit: kEventAppTerminated
See:
http://developer.apple.com/technotes/tn/tn2050.html

ChrisA
Sep 18, 2008, 11:19 AM
any structures or any C functions which can retrieve the process details which are currently running.

Not sure what you want. If you want information on ALL running processes then you can type the "ps" command at the terminal. From inside a C program you can "exec" or "system" the ps command and parse it's output.

If you want to do this without running "ps" then read the source code of any open source version of ps and see how it's done. Likely you can cut and paste. But the implementation of "ps" is platform specific. What works on the Mac will not on Linux so runing ps in the shell is not so bad if you need to be platform independent

chidambaram
Sep 19, 2008, 12:14 AM
thanks for all reply...