26 January, 2011

pgrep command for listing PID's of currently running processes

pgrep is a bash command that looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.  All the criteria given as parameters to pgrep have to matched.
$ pgrep fi 
         -- List the PID of process names match with "fi".   

$ pgrep -l fi 
         -- Same as above and process name also get 
             listed.  
 
$ pgrep -vl fi 
         -- List all processes, which name not match    
             with "fi".   

$ pgrep -xl fi 
         -- List all processes, which name exactly match  
             with "fi".  
 
$ pgrep -f sbin 
         --List all processes, which are running from  some 
            sbin folder. It check the command line also.  
 
$pgrep -c fi
         --Show the count of matching processes.  
 
$ pgrep -d, fi 
         -- List the matching process id in CSV format.  
 
$ pgrep -t tty1 
         -- List the process controlling the term tty1. 
 
$ pgrep -u root, jo 
         -- List all processes owned by root and jo. 
 
$ pgrep -u jo gnome
         -- List the process called sshd and owned  by jo.  
 
$ pgrep -n fi 
          -- Show only the newest process.  
 
$ pgrep -o fi 
          -- Show only the oldest process.  
Read rest of entry

22 September, 2010

C program to fork a new process

Here is a C program to create suboprocess from the current process using fork() system call in Unix. It also identifies and checks both the parent and child process using the process ids...

#include <unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>

int main ()
{
  int flag,flag2;
  int pid,ppid;
  printf("\nHello Worldn");
  flag = fork();
  printf("\nflag :%d",flag);
 
if(flag > 0)
  {         
    printf("\nI'm the Father" );
    printf("\n%dpid ",getpid());
    printf("\n%dppid ",getppid());
   
  }
  else if(flag == 0)
  {     
    printf("\n\n\nI'm the Sonn");
    pid=getpid();
    ppid=getppid();
    printf("\n%dpid ",pid);
    printf("\n%dppid ",ppid);
   
    flag2=fork();
    if(flag2 > 0)
    {         
        printf("\nI'm the sonn" );
        printf("\n%dpid ",getpid());
        printf("\n%dppid ",getppid());
      }
      else if(flag2 == 0)
      {     
        printf("\n\n\nI'm the grandSonn");
        pid=getpid();
        ppid=getppid();
        printf("\n%dpid ",pid);
        printf("\n%dppid ",ppid);
       }
      else
        printf("failure");
   }
  else
    printf("failure");
  printf("\nGoodbye\n\n");
}
Read rest of entry
 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger