Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-07-2004, 08:35 PM
|
#1
|
|
Member
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30
Rep:
|
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
|
|
|
|
03-07-2004, 08:38 PM
|
#2
|
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
you want getpid. Read man getpid for more info.
|
|
|
|
03-07-2004, 08:41 PM
|
#3
|
|
Member
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30
Original Poster
Rep:
|
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
|
|
|
|
03-07-2004, 08:51 PM
|
#4
|
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
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.
|
|
|
|
03-07-2004, 08:53 PM
|
#5
|
|
Member
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30
Original Poster
Rep:
|
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.
|
|
|
|
03-08-2004, 01:31 AM
|
#6
|
|
Member
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 274
Rep:
|
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.
|
|
|
|
03-08-2004, 02:57 AM
|
#7
|
|
Member
Registered: Nov 2003
Location: delhi
Distribution: redhat 8.0 ,fedora
Posts: 32
Rep:
|
why don't u use
execv() function
|
|
|
|
03-09-2004, 01:31 AM
|
#8
|
|
Member
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30
Original Poster
Rep:
|
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.
|
|
|
|
03-09-2004, 10:40 AM
|
#9
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void processdir(const struct dirent *dir)
{
puts(dir->d_name);
}
int filter(const struct dirent *dir)
{
uid_t user;
struct stat dirinfo;
int len = strlen(dir->d_name) + 7;
char path[len];
strcpy(path, "/proc/");
strcat(path, dir->d_name);
user = getuid();
if (stat(path, &dirinfo) < 0) {
perror("processdir() ==> stat()");
exit(EXIT_FAILURE);
}
return !fnmatch("[1-9]*", dir->d_name, 0) && user == dirinfo.st_uid;
}
int main()
{
/* Based on example in "man scandir" */
struct dirent **namelist;
int n;
n = scandir("/proc", &namelist, filter, 0);
if (n < 0)
perror("Not enough memory.");
else {
while(n--) {
processdir(namelist[n]);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
<edit>
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)
{
return !fnmatch("[1-9]*", dir->d_name, 0);
}
</edit>
Last edited by Hko; 03-09-2004 at 03:32 PM.
|
|
|
|
03-15-2004, 03:33 AM
|
#10
|
|
Member
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30
Original Poster
Rep:
|
Thanks HKO,
your code is cool!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting 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
|
|
|
All times are GMT -5. The time now is 04:30 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|