Find the answer to your Linux question:
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 ...
  1. #1
    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

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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) .

  6. #6
    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.

  7. #7
    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.

  8. #8
    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&#40; char *process &#41;
    &#123;
      int target_link;
      DIR *dir_p;
      DIR *dir_proc;
      struct dirent *dir_entry_p;
      struct dirent *dir_proc_p;
      char dir_name&#91;35&#93;;
      char target_name&#91;235&#93;;
      char exe_link&#91;235&#93;;
      char process_id&#91;20&#93;;
      int i;
    
      dir_p = opendir&#40; "/proc/" &#41;; /* Open /proc/ directory */
    
      while&#40; NULL != &#40;dir_entry_p = readdir&#40;dir_p&#41;&#41;&#41; /* Reading /proc/ entries */
        &#123;
          if &#40; strspn&#40; dir_entry_p->d_name, "0123456789" &#41; == strlen&#40; dir_entry_p->d_name &#41; &#41; /* Checking for numbered directories */
    	&#123;
    	  strcpy&#40; process_id, dir_entry_p->d_name &#41;; /* Saving the numbered directory i.e process-id */
    
    	  strcpy&#40; dir_name, "/proc/" &#41;;
    	  strcat&#40; dir_name, dir_entry_p->d_name &#41;;
    	  strcat&#40; dir_name, "/" &#41;; /* Obtaining the full-path eg&#58; /proc/24657/ */
    
    	  if &#40; &#40; dir_proc = opendir&#40; dir_name &#41; &#41; &#41; /* Open that numbered directory */
    	    &#123;
    	      while &#40; &#40; dir_proc_p = readdir&#40; dir_proc &#41; &#41; &#41; /* Reading its entries */
    		&#123;
    		  if &#40; strcmp&#40; dir_proc_p->d_name, "exe" &#41; == 0 &#41; /* Check for that exe link file */
    		    &#123;
    		      strcat&#40; exe_link, dir_name &#41;;
    		      strcat&#40; exe_link, dir_proc_p->d_name &#41;; /* Getting the full-path of that exe link */
    
    		      /* Getting the target of the exe ie to which binary it points to */
    		      target_link = readlink&#40; exe_link, target_name, sizeof&#40; target_name &#41; &#41;; 
    		      target_name&#91; target_link &#93; = '\0';
    
    		      if &#40; strstr&#40; target_name, process &#41; != NULL &#41; /* Searching for process name in the target name */
    			&#123;
    			  printf&#40; "\n\n%s FOUND --- PROCESS_ID %s \n\n\n", process, process_id &#41;;
    			  return atoi&#40;process_id&#41;; /* GOT IT&#58; return the process-id */
    			&#125;
    
    		      strcpy&#40; exe_link ,"" &#41;;
    		    &#125;
    		&#125;
    	    &#125;
    	  else
    	    printf&#40; "Dir %s opening failed \n", dir_name &#41;;
    	&#125;
        &#125;
    
      closedir&#40;dir_p&#41;;
      return 0;
    &#125;
    
    
    int main&#40;&#41;
    &#123;
      system&#40; "emacs &" &#41;;
      printf&#40; " %u ", getProcessID&#40; "emacs" &#41; &#41;;
      return 0;
    &#125;
    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.

  9. #9
    Just Joined!
    Join Date
    Sep 2006
    Posts
    1

    Thanks

    Thanks for the code, really useful!

  10. #10
    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(&#37;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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...