Tuesday, February 1, 2011

~/proc Directory~

Hello Everyone,

In this article we would discuss about the /proc directory. GNU/Linux maintains the detailed information about each process in directory /proc.
I would update the basics information/layout of each process shortly.
However i have written shell script which take PID of a process as input
and display detailed information in one go........Here is the pstate.sh script.


#########################################################


#!/bin/sh

# Copyright (C) 2011 Free Software Foundation, Inc.
# Author : Mantosh Kumar, mantosh4u@gmail.com

# The GNU Script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# The GNU Script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See
# the GNU Lesser General Public License for more details.


################Brief Description##################
# The script name would be "pstate.sh". The script would take the PID as
# argument and check whether the process exists or not. If process is there,
# then script should give the whole set of information about the process in
# organized manner, so that user would be having whole set of information
# at one go. Just Type " ps aux " on terminal and check about the number of
# processes running on your machine. Now you can select any PID to enter
# while running with this script.This script works fine most of time, but
# in case if user wants to get info about processess like pid:1(init),
# he/she needs to run this as root user.This scripts does not work sometime
# for the processes which has opened file and file number are not in
# sequence.


echo -n "The detailed status information of a particular process\n";
echo -n "=================================";
echo -n "=======================\n";

# The most important input argument would be passed from the user, which
# can be accessed from $1 value.Lets save into Global variable.Since we
# have defined this variable which is not inside any scope.
PID=$1;

# Now declare other variable which should be dedicated directory for a
# process mantained by the kernel. Before processing further we should
# check whether directory exists or not. If process is still there, there
# would be directory in /proc If directory exists go on or else immediately
# return, by giving user message like process does not exists.This is also
# Global variable.
proc_direct=/proc/$PID;



# Shell function for fetching system info and current time as well as date.

sys_time() {

local sys_name=`uname`;
local sys_month=`date +%B`;
local sys_date=`date +%d`;
local sys_year=`date +%Y`;
local sys_minute=`date +%M`;
local sys_second=`date +%S`;

echo -n "System:$sys_name, $sys_month $sys_date, $sys_year "
echo -n "$sys_minute:$sys_second\n";
}



# Shell function for fetching process control info of a given PID.

pid_process() {


# status.log file would contain the all information releated to
# process Now /proc/[pid]/status file contains all stuff, just
# redirecting to temp.log
cat $proc_direct/status > $PWD/status.log;

#echo -n "pwd: $PWD\n";
#Processing for pid, Ppid,Process Name, State, Thread Counter value
pid_proc=`awk '$1 == "Pid:" { print $2 }' status.log`;
ppid_proc=`awk '$1 == "PPid:" { print $2 }' status.log`;
p_name=`awk '$1 == "Name:" { print $2 }' status.log`;
p_state=`awk '$1 == "State:" { print $2 }' status.log`;
thread_c=`awk '$1 == "Threads:" { print $2 }' status.log`;
echo -n "Process Name\t:$p_name\n";
echo -n "State\t\t:$p_state\n";
echo -n "Pid\t\t:$pid_proc\n";
echo -n "Parent Pid\t:$ppid_proc\n";
echo -n "Threads\t\t:$thread_c\n";

#Priority of process
cat $proc_direct/sched > $PWD/sched.log;
p_priority=`awk '$1 == "prio" { print $3 }' sched.log`;
echo -n "Priority\t:$p_priority\n";

#Uid & Gid Information of User
p_uid=`awk '$1 == "Uid:" { print $2 }' status.log`;
p_guid=`awk '$1 == "Gid:" { print $2 }' status.log`;
echo -n "Uid\t\t:$p_uid\n";
echo -n "Gid\t\t:$p_guid\n\n";


#Memory Management Information
VmPeak=`awk '$1 == "VmPeak:" { print $2 }' status.log`;
VmSize=`awk '$1 == "VmSize:" { print $2 }' status.log`;
VmLck=`awk '$1 == "VmLck:" { print $2 }' status.log`;
VmHWM=`awk '$1 == "VmHWM:" { print $2 }' status.log`;
VmRSS=`awk '$1 == "VmRSS:" { print $2 }' status.log`;
VmData=`awk '$1 == "VmData:" { print $2 }' status.log`;
VmStk=`awk '$1 == "VmStk:" { print $2 }' status.log`;
VmExe=`awk '$1 == "VmExe:" { print $2 }' status.log`;
VmLib=`awk '$1 == "VmLib:" { print $2 }' status.log`;
VmPTE=`awk '$1 == "VmLib:" { print $2 }' status.log`;

echo -n "Peak Virtual Memory Size\t:$VmPeak\n";
echo -n "Current Virtual Memory Size\t:$VmSize\n";
echo -n "Locked Memory Size\t\t:$VmLck\n";
echo -n "Peak resident Set Size\t\t:$VmHWM\n";
echo -n "Current Resident Set Size\t:$VmRSS\n";
echo -n "Data Segment Size\t\t:$VmData\n";
echo -n "Stack Size\t\t\t:$VmStk\n";
echo -n "Text(Instruction) Size\t\t:$VmExe\n";
echo -n "Shared library Code Size\t:$VmLib\n";
echo -n "Page Table Size\t\t\t:$VmPTE\n";

#Now purge the two log files which we created inside this function
# status.log, sched.log
rm sched.log;
rm status.log;

}


# Shell function for displaying detailed map section of virtual memory
# of process.

pid_maps() {

#cat /proc/$PID/maps
echo -n "\n";
echo -n "Detailed Information about the memory map section\n";
echo -n "================================";
echo -n "=================\n";
cat $proc_direct/maps;

}



#Shell function for displaying basic info about files opend by PID.

pid_fileinformation() {

echo -n "\n";
echo -n "The detailed information about the files opened"
echo -n "by process\n";
echo -n "================================";
echo -n "==========================\n";
echo -n "\n";

fcounter=`ls $proc_direct/fd | wc -w`;
echo -n "Number of files opened by the process\t\t:$fcounter\n\n";

#change the cwd to the location $proc_direct/fdinfo, so that we can
#directoly use the stat utility for each of file number
cd $proc_direct/fd;

#Now Use Loop so that we get basics information about each of
# file number.

count=0;
while [ $count -lt $fcounter ] ; do
echo -n "Information about filenumber: $count\n";
echo -n "-------------------------------\n";

#stat $count;
filename=`stat -c %N $count`;
filetype=`stat -c %F $count`;
access=`stat -c %A $count`;
owner=`stat -c %U $count`;
size=`stat -c %s $count`;

echo -n "FileName\t:$filename\n";
echo -n "FileType\t:$filetype\n";
echo -n "Size\t\t:$size\n";
echo -n "Owner\t\t:$owner\n";
echo -n "Access\t\t:$access\n";
echo -n "\n";

count=`expr $count + 1`
done
}



# Main Body of Script, Each of above function has been called from this
# area. We can assume this as main function of C program.

if [ -d $proc_direct ]
then

sys_time
echo -n "\n";
pid_process
pid_maps
pid_fileinformation

else
echo -n "\n";
echo -n "Process(PID:$PID) does not exist on the system\n";

fi




#################################################################



Output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mantosh@ubuntu:~$ ./pstate.sh 8417
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



The detailed status information of a particular process
========================================================
System:Linux, February 02, 2011 33:37

Process Name :ocat
State :T
Pid :8417
Parent Pid :8416
Threads :1
Priority :120
Uid :1000
Gid :1000

Peak Virtual Memory Size :1748
Current Virtual Memory Size :1652
Locked Memory Size :0
Peak resident Set Size :288
Current Resident Set Size :280
Data Segment Size :28
Stack Size :84
Text(Instruction) Size :4
Shared library Code Size :1504
Page Table Size :1504

Detailed Information about the memory map section
=================================================
08048000-08049000 r-xp 00000000 07:00 248189 /home/mantosh/Desktop/LinuxPI/ocat
08049000-0804a000 r--p 00000000 07:00 248189 /home/mantosh/Desktop/LinuxPI/ocat
0804a000-0804b000 rw-p 00001000 07:00 248189 /home/mantosh/Desktop/LinuxPI/ocat
b7e38000-b7e39000 rw-p b7e38000 00:00 0
b7e39000-b7f95000 r-xp 00000000 07:00 33170 /lib/tls/i686/cmov/libc-2.9.so
b7f95000-b7f96000 ---p 0015c000 07:00 33170 /lib/tls/i686/cmov/libc-2.9.so
b7f96000-b7f98000 r--p 0015c000 07:00 33170 /lib/tls/i686/cmov/libc-2.9.so
b7f98000-b7f99000 rw-p 0015e000 07:00 33170 /lib/tls/i686/cmov/libc-2.9.so
b7f99000-b7f9c000 rw-p b7f99000 00:00 0
b7fb4000-b7fb6000 rw-p b7fb4000 00:00 0
b7fb6000-b7fb7000 r-xp b7fb6000 00:00 0 [vdso]
b7fb7000-b7fd3000 r-xp 00000000 07:00 577782 /lib/ld-2.9.so
b7fd3000-b7fd4000 r--p 0001b000 07:00 577782 /lib/ld-2.9.so
b7fd4000-b7fd5000 rw-p 0001c000 07:00 577782 /lib/ld-2.9.so
bfbca000-bfbdf000 rw-p bffeb000 00:00 0 [stack]

The detailed information about the files opened by process
==========================================================

Number of files opened by the process :6

Information about filenumber: 0
-------------------------------
FileName :`0' -> `/dev/pts/0'
FileType :symbolic link
Size :64
Owner :mantosh
Access :lrwx------

Information about filenumber: 1
-------------------------------
FileName :`1' -> `/dev/pts/0'
FileType :symbolic link
Size :64
Owner :mantosh
Access :lrwx------

Information about filenumber: 2
-------------------------------
FileName :`2' -> `/dev/pts/0'
FileType :symbolic link
Size :64
Owner :mantosh
Access :lrwx------

Information about filenumber: 3
-------------------------------
FileName :`3' -> `pipe:[44415]'
FileType :symbolic link
Size :64
Owner :mantosh
Access :lr-x------

Information about filenumber: 4
-------------------------------
FileName :`4' -> `pipe:[44415]'
FileType :symbolic link
Size :64
Owner :mantosh
Access :l-wx------

Information about filenumber: 5
-------------------------------
FileName :`5' -> `/home/mantosh/Desktop/LinuxPI/ocat'
FileType :symbolic link
Size :64
Owner :mantosh
Access :lr-x------




Hope that this article might be of some use for the reader. Feel free to put yours comment.

No comments:

Post a Comment