 |
 |
List of processes with names?
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Santa Clara, CA 95050
Status:
Offline
|
|
I was wondering if there is any way to get the PIDs of all the processes from the user-written code. I know that we can use the getpid() and getppid() to get the code for the current process and its parent. However, is there a way to get the PIDs of all the processes on the system? (source code for 'top'?
------------------
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
"man ps"

|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
You could get the source code for top and try to manipulate the kvm functions... however, this is cumbersome.
Try a pipe to "ps" with arguments "wwwaux" (All processes, full name, &c.) Then scan the string to your liking.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Look at sysctl() ("man 3 sysctl"). More specifically, the CTL_KERN top-level name, and the KERN_PROC section under that.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
[Double post]
[This message has been edited by lindberg (edited 04-24-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Santa Clara, CA 95050
Status:
Offline
|
|
Thank you for all your suggestions. I am looking at syscntl(). This seems to be useful. It has some really low level (neat) stuff which we can poke into. I am not sure, however, what permissions will my process have to have in order to read some of these variables. I will try and see how far will I get.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Santa Clara, CA 95050
Status:
Offline
|
|
Is there a way to use the unix shell command from a C source? If yes, then I guess, piping the ps to a string will suffice. If not, then I probably have to us syscntl().
Thank you.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
here's some code I wrote a while ago just for learning how to use popen(). It's messy and probably lame, but hey, I think it kinda works
It uses ls but you can easily change this to your liking.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
FILE *f;
int c;
char s[2500];
f = popen("ls","r");
if (f != NULL)
{
char mychar;
for (i=0; i<2500; i++)
if (!(feof(f)))
s[i] = fgetc(f);
printf("%s",s);
} else {
printf("Something went wrong somehwere.");
}
/* i = system("ls");*/
i = pclose(f);
return 0;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Santa Clara, CA 95050
Status:
Offline
|
|
Thanks Angus. I tried the code. It works. Thank you all for the help.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|