Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > C - printing linux process table
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 08-03-2005   #1 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Bangalore
Posts: 10
C - printing linux process table

Hello,
I need to write a C program, which can print all the processes currently running with their process ids. I dont know where to start. Is there any system call available to get the list of processes and its ids? Can any one give me some ideas, so that I can proceed from there.

Thanks
Seran
seranmca is offline  

Old 08-03-2005   #2 (permalink)
/etc/init.d/moderator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 5,825
Code:
ps aux
will give you details of system processes.
Code:
top
runs an interactive CLI tool for monitoring, if you can extract anything there either.
__________________
Registered Linux user #378740
New members read here / Forum Rules
#linuxforums on irc.freenode.net
bigtomrodney is offline  
Old 08-03-2005   #3 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Bangalore
Posts: 10
I should use not system() - system call in my C program. I have to read all the entries in the linux process table and I have to print process names with its ids.

This will be a pure C programming work, without using shell commands.
seranmca is offline  
Old 08-03-2005   #4 (permalink)
Linux Enthusiast
 
Join Date: Jan 2005
Posts: 575
The /proc directory has that info.I believe it has a listing of
the ids of all processes.
Santa's little helper is offline  
Old 08-03-2005   #5 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Bangalore
Posts: 10
Thanks. got it.

I have to search thru the numbered directories (process ids) and those exe links (process names) .
seranmca is offline  
Old 08-03-2005   #6 (permalink)
Linux Guru
 
Join Date: Oct 2001
Location: Täby, Sweden
Posts: 7,578
You may want to consider using libproc, which is the library used by ps, top and related programs. I don't know where to get hold of the documentation for it, though.
Dolda2000 is offline  
Old 08-03-2005   #7 (permalink)
Linux User
 
Join Date: Oct 2004
Location: Serbia&Montenegro
Posts: 281
Yes, I also heard that libproc is good for these things, cause I'm thinking on doing something similar too.
__________________
Linux registered user #358842
Human knowledge belongs to the world.
Fallen Angel is offline  
Old 08-09-2005   #8 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Bangalore
Posts: 10
Obtaining process-id of a process from a C program

I had written a small program to return the process-id of a process. I thought this will be usefull, and the code is here....


Code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

unsigned int getProcessID( char *process )
{
  int target_link;
  DIR *dir_p;
  DIR *dir_proc;
  struct dirent *dir_entry_p;
  struct dirent *dir_proc_p;
  char dir_name[35];
  char target_name[235];
  char exe_link[235];
  char process_id[20];
  int i;

  dir_p = opendir( "/proc/" ); /* Open /proc/ directory */

  while( NULL != (dir_entry_p = readdir(dir_p))) /* Reading /proc/ entries */
    {
      if ( strspn( dir_entry_p->d_name, "0123456789" ) == strlen( dir_entry_p->d_name ) ) /* Checking for numbered directories */
	{
	  strcpy( process_id, dir_entry_p->d_name ); /* Saving the numbered directory i.e process-id */

	  strcpy( dir_name, "/proc/" );
	  strcat( dir_name, dir_entry_p->d_name );
	  strcat( dir_name, "/" ); /* Obtaining the full-path eg: /proc/24657/ */

	  if ( ( dir_proc = opendir( dir_name ) ) ) /* Open that numbered directory */
	    {
	      while ( ( dir_proc_p = readdir( dir_proc ) ) ) /* Reading its entries */
		{
		  if ( strcmp( dir_proc_p->d_name, "exe" ) == 0 ) /* Check for that exe link file */
		    {
		      strcat( exe_link, dir_name );
		      strcat( exe_link, dir_proc_p->d_name ); /* Getting the full-path of that exe link */

		      /* Getting the target of the exe ie to which binary it points to */
		      target_link = readlink( exe_link, target_name, sizeof( target_name ) ); 
		      target_name[ target_link ] = '\0';

		      if ( strstr( target_name, process ) != NULL ) /* Searching for process name in the target name */
			{
			  printf( "\n\n%s FOUND --- PROCESS_ID %s \n\n\n", process, process_id );
			  return atoi(process_id); /* GOT IT: return the process-id */
			}

		      strcpy( exe_link ,"" );
		    }
		}
	    }
	  else
	    printf( "Dir %s opening failed \n", dir_name );
	}
    }

  closedir(dir_p);
  return 0;
}


int main()
{
  system( "emacs &" );
  printf( " %u ", getProcessID( "emacs" ) );
  return 0;
}
getProcessID( ) takes the name of the process, searches inside the /proc directory and returns the process-id. Let me know if there are some other ways.

Mod edit - Added code tags.
seranmca is offline  
Old 09-14-2006   #9 (permalink)
Just Joined!
 
Join Date: Sep 2006
Posts: 1
Thanks

Thanks for the code, really useful!
Bedan is offline  
Old 03-11-2008   #10 (permalink)
Just Joined!
 
jonowoodhouse's Avatar
 
Join Date: Mar 2008
Posts: 1
Hi Seranmca

Thanks for this code. It is very useful indeed.

I've made a couple little tweaks and optimisations to it:

Code:
unsigned int getProcessID(char *p_processname) {
	DIR *dir_p;
	struct dirent *dir_entry_p;
	char dir_name[40];										// ??? buffer overrun potential
	char target_name[252];									// ??? buffer overrun potential
	int target_result;
	char exe_link[252];
	int errorcount;
	int result;

	errorcount=0;
	result=0;
	dir_p = opendir("/proc/"); 																// Open /proc/ directory
	while(NULL != (dir_entry_p = readdir(dir_p))) {											// Reading /proc/ entries
		if (strspn(dir_entry_p->d_name, "0123456789") == strlen(dir_entry_p->d_name)) {		// Checking for numbered directories 
			strcpy(dir_name, "/proc/");
			strcat(dir_name, dir_entry_p->d_name);
			strcat(dir_name, "/"); 															// Obtaining the full-path eg: /proc/24657/ 
			exe_link[0] = 0;
			strcat(exe_link, dir_name);
			strcat(exe_link, "exe");													 	// Getting the full-path of that exe link
			target_result = readlink(exe_link, target_name, sizeof(target_name)-1);			// Getting the target of the exe ie to which binary it points to
			if (target_result > 0) {
				target_name[target_result] = 0;
				if (strstr(target_name, p_processname) != NULL) {							// Searching for process name in the target name -- ??? could be a better search !!!
					result = atoi(dir_entry_p->d_name);
					printf("getProcessID(%s) :Found. id = %d\n", p_processname, result);
					closedir(dir_p);
					return result;
				}
			}
		}
	}
	closedir(dir_p);
	printf("getProcessID(%s) : id = 0 (could not find process)\n", p_processname);
	return result;
}

Cheers
Jono
jonowoodhouse is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Free Network Mapping Tool for Microsoft® Office Visio® Professional 2007 Users
Don't map your network by hand – let LANsurveyor Express for Microsoft Visio Professional 2007 automatically create network diagrams for you.
subscribe
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say.
subscribe

Safe, Secure Backup


All times are GMT. The time now is 12:37 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2