Results 1 to 10 of 10
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 ...
- 08-03-2005 #1Just 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
- 08-03-2005 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
will give you details of system processes.Code:ps aux
runs an interactive CLI tool for monitoring, if you can extract anything there either.Code:top
- 08-03-2005 #3Just 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.
- 08-03-2005 #4Linux 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.
- 08-03-2005 #5Just 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) .
- 08-03-2005 #6Linux 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.
- 08-03-2005 #7Linux 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.
- 08-09-2005 #8Just 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....
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.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; }
Mod edit - Added code tags.
- 09-14-2006 #9Just Joined!
- Join Date
- Sep 2006
- Posts
- 1
Thanks
Thanks for the code, really useful!
- 03-11-2008 #10
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



