Process and Thread counts

While using any operating system, at any time, there may be a need to know the number of active processes and threads. In Microsoft Windows operating systems, it is possible through different GUI-based tools like Windows Built-in Task Manager, SysInternals Process Explorer etc. But in Unix, Linux or any of the Unix variant operating systems, we normally look for commands to get things done.
The below commands have been tested in OpenSolaris so far. They are expected to work on Linux and other Unix variant operating systems as well.
For example, to find the number of iperf processes, # ps -ef | grep iperf | grep -v "grep" | wc -l
If you know how this works, you can then skip this paragraph. The first command ps is the key command which gets us all running processes at that time. The output of ps is then piped to grep. The grep command is used to search for lines containing iperf (the process we are interested in). Since grep will also be listed as a process with iperf word in the same line, we need to get rid of that entry. Otherwise, our list will have one extra entry which we are not interested in. So, we have second grep. Here we remove all lines containing word grep in the lines from first grep. The last command wc (word count) is used to get count of lines which is nothing but the number of running iperf processes.
Similarly to find the number of iperf threads,# ps -eLf | grep iperf | grep -v "grep" | wc -l
Here the only change is in the arguments passed to ps command. The argument -L hints ps to look for only light weight processes (also called threads).
There is one more way of finding the number of threads. In this method, we make use of
To find the number of iperf threads using this method, execute# echo ::threadlist | mdb -k | grep iperf | wc -l
Here passing ::threadlist to mdb shows a list of threads running in the system. In that list, grep for iperf and find the count of lines.




