Getting PID of running process in C/C++
Hi All,
I would like to obtain the process id of runing processes in the system The shell equivalent of what I'm trying to do is "ps -ef". If there is sample code I would really appreciate it. I am using C, and I wish not to use ps or psof Thanks |
you want getpid. Read man getpid for more info.
|
Hi jtshaw ,
Man getpid, getppid tells me that it is for the current process. However, what I really want is list of running processes not only the current process thanks |
Ah, sorry bout that.
What I believe the ps function does is reads all the information from /proc. You'll notice a bunch of directories that are numbers. Those numbers are your pids. In each directory you have information about the pids. |
Heh,
No problem, well that is what I'm trying to do now but it get so troublesome plus we've to look into each of it to find what argument are passed to the process. I wish there's a smart and fast way of doing it. |
Why don't you get the source code of ps and study this? I believe it's either
in binutils or shutils, google will tell you where to download it. |
why don't u use
execv() function |
Thanks everyone,
I use the method used in ps source. It is a little tedious but it works. execv() is not my option because I'm trying to get the pid of all the process in the system, not only ones started by me. |
Maybe you can use this?
I posted it here before some time ago. It scans /proc for all PID's and prints them. Instead of printing the PID you can put any code to get more info from the /proc/<PID> directory by changing the processdir() function. Code:
#include <stdio.h> This only prints processes owned by the user running this program. If you prefer all processes, you can reduce filter() to just: Code:
int filter(const struct dirent *dir) |
Thanks HKO,
your code is cool! |
Number of all active processes from /proc
program that will create a none zombie child process to display Number of all active processes.can some one help me with this?
|
What is a 'none zombie child process'? What is the definition of 'active process'?
|
man 5 proc
look at the section about /proc/pid/stat more precisely the state variable in /proc/pid/stat(us) |
Quote:
|
Number of active processes
I have put some effort. And i had this program before posting the question here. But i was not getting the expected output. And i want my question to be short. So i didnt post my program.
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> void main(){ int pid1,pid2,pid3; int pfds1[2], pfds2[2]; char foo[4096]; pipe(pfds1); pipe(pfds2); pid1 = fork(); if(pid1==0) { close(1); dup(pfds1[1]); close(pfds1[0]);close(pfds1[1]); close(pfds2[0]);close(pfds2[1]); execlp("/bin/ps","ps","-A",NULL); } else { pid2 = fork(); if(pid2==0){ close(0); dup(pfds1[0]); close(1); dup(pfds2[1]); close(pfds1[0]);close(pfds1[1]); close(pfds2[0]);close(pfds2[1]); execlp("/usr/bin/wc","wc","-l",NULL); } } close(pfds1[0]);close(pfds1[1]); close(pfds2[0]);close(pfds2[1]); //Parent waits for all of its 3 children to finish execution wait(NULL);wait(NULL);wait(NULL); int nbytes = read(pfds1[0], foo, sizeof(foo)); printf("Output: (%.*s)\n", nbytes, foo); } |
All times are GMT -5. The time now is 11:02 AM. |