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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-20-2004, 01:59 AM
|
#1
|
LQ Newbie
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5
Rep:
|
Listing Processes
Is there any way of listing all processes on linux in C, as i am trying to emulate the EnumWindows function in windows.
|
|
|
08-20-2004, 02:20 AM
|
#2
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
You can get them from the /proc. check the man page for more info.
|
|
|
08-20-2004, 02:48 AM
|
#3
|
LQ Newbie
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5
Original Poster
Rep:
|
how do you found out the active ones
|
|
|
08-20-2004, 02:56 AM
|
#4
|
LQ Newbie
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5
Original Poster
Rep:
|
are the ones on the /proc directory all active processes. If a process finished is the pid cleared from the /proc diectory
|
|
|
08-20-2004, 03:01 AM
|
#5
|
Member
Registered: Apr 2004
Posts: 101
Rep:
|
yes all the processes in the proc file system are either running , stopped .
if by active you mean running.. processes.. then refer to the man page
to know how to know if the process is running or suspended.
|
|
|
08-20-2004, 03:29 AM
|
#6
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
Check the file /proc/<pid>/status. It has everything you need.
|
|
|
08-20-2004, 05:40 AM
|
#7
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Does this help:
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);
}
/* This function shows how to get information from the /proc/<pid>
* directory. In this case it reads and filters on the owner
* of the file (this is also the owner of the process)
*
* If you want to list ALL PID's, just remove the part
* that check for the user.
*/
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;
}
Last edited by Hko; 08-20-2004 at 05:42 AM.
|
|
|
All times are GMT -5. The time now is 01:53 PM.
|
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
|
|