LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-20-2004, 01:59 AM   #1
oriel_gizmo
LQ Newbie
 
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5

Rep: Reputation: 0
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.
 
Old 08-20-2004, 02:20 AM   #2
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
You can get them from the /proc. check the man page for more info.
 
Old 08-20-2004, 02:48 AM   #3
oriel_gizmo
LQ Newbie
 
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5

Original Poster
Rep: Reputation: 0
how do you found out the active ones
 
Old 08-20-2004, 02:56 AM   #4
oriel_gizmo
LQ Newbie
 
Registered: Aug 2004
Location: UK
Distribution: debian
Posts: 5

Original Poster
Rep: Reputation: 0
are the ones on the /proc directory all active processes. If a process finished is the pid cleared from the /proc diectory
 
Old 08-20-2004, 03:01 AM   #5
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Rep: Reputation: 15
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.
 
Old 08-20-2004, 03:29 AM   #6
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Check the file /proc/<pid>/status. It has everything you need.
 
Old 08-20-2004, 05:40 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get a listing of running daemons / processes greengrocer Linux - Newbie 6 08-28-2005 03:36 PM
Listing Used Ports BorgKiller Linux - Security 2 04-13-2005 11:44 PM
iptables is not listing to me pietsebrie Fedora 4 11-18-2004 01:03 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Listing processes by memory usage? Seventh Linux - Newbie 3 06-17-2004 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration