Listing threads

Since the introduction of the Native POSIX Threads Library (NPTL) threads have become rather elusive. They don't show up in the default process listing using the ps command and if you are using the ps applet from Busybox they don't show up ever. In this note I will show how to display thread information using the full ps program and how to do the same thing in a shell script if you don't have it.

First, if you do have the luxury of the full ps command (from the procps package) on your target system then all you need to do is add the “-L” option. For example to list all processes (-A) and all their threads (-L)

	ps -AL

Adding -f (full) gives more information:

	ps -ALf

More interesting, you can create custom output formats using the -o switch followed by a list of keywords. There are over 40 possibilities - read the ps man page for a complete list - among them


  • pid - the process ID
  • tid - the thread ID
  • class - the scheduling class, one of TS (SCHED_OTHER), FF ( SCHED_FIFO) or RR (SCHED_RR)
  • rtprio - priority of real-time tasks (1..99)
  • stat - state, one of R (running) S (sleeping) D (disk wait) T (stopped) Z (zombie)
  • comm - the command name
  • wchan - name of the kernel function in which the process is sleeping

For example

# ps -Leo pid,tid,class,rtprio,stat,comm,wchan
  PID   TID CLS RTPRIO STAT COMMAND         WCHAN
    1     1 TS       - Ss   init            wait
   ...
  277   277 TS       - Ss   sh              wait
 1093  1093 TS       - Sl   thread-test     futex_wait
 1093  1094 TS       - Sl   thread-test     hrtimer_nanosleep
 1093  1095 TS       - Sl   thread-test     hrtimer_nanosleep
 1093  1096 TS       - Sl   thread-test     hrtimer_nanosleep
 1149  1149 TS       - R+   ps              -

Note the four threads in process thread-test each have PID 1093 and have TIDs from 1093 to 1096.

Now, what if you only have the Busybox ps applet, or no ps at all? Of course, you could go and get procps and cross-compile the ps program or maybe find one already cross compiled. For the case where that is not possible or not worth the effort, here is a shell script I use that does almost the same as ps using just basic ash or bash syntax. It gathers information from the /proc file system (as the ps program does) and formats it in a fairly crude way. It is fairly easy to edit to change the information being displayed, as explained in the comments.

#!/bin/sh

# A (b)ash script replacement for ps showing threads and other helpful stuff.
# Especially useful for Busybox based systems which has a very simple ps applet
#
# License: GPLv2
# Copyright 2009 Chris Simmonds, 2net Ltd
# chris@2net.co.uk

show_details ()
{
	# Read the stat entry for this process/thread. See man 5 proc for description
	read _PID _COMM _STATE _PPID _PGRP _SESSION _TTY_NR _TPGID _FLAGS _MINFLT _CMINFLT _MAJFLT \
	     _CMAJFLT _UTIME _STIME _CUTIME _CSTIME _PRIORITY _NICE _NUM_THREADS _IRETVALUE \
	     _STARTTIME _VSIZE _RSS _RSSLIM _STARTCODE _ENDCODE _STARTSTACK _KSTKESP _KSTKEIP \
	     _SIGNAL _BLOCKED _SIGIGNORE _SIGCATCH _WCHAN _NSWAP _CNSWAP _EXIT_SIGNAL _PROCESSOR \
	     _RT_PRIORITY _POLICY _JUNK < /proc/$PID_TO_SHOW/stat

	# Decode policy and priority
	case $_POLICY in
	"0")
		POLICY="TS"
		RTPRIO="- "
		;;
	"1")
		POLICY="FF"
		RTPRIO=$_RT_PRIORITY
		;;
	"2")
		POLICY="RR"
		RTPRIO=$_RT_PRIORITY
		;;
	*)
		POLICY="??"
		RTPRIO="- "
	esac
	# _WCHAN is the address of the kernel function the task is blocked
	# in: not much use, so read /proc/NN/wchan to get the name of the function
	read _WCHAN < /proc/$PID_TO_SHOW/wchan

	# The output is more or less equivalent to "ps -Leo pid,tid,class,rtprio,stat,comm,wchan"
	echo -e "$PID\t$TID\t$POLICY\t$RTPRIO\t$_STATE\t$_COMM\t$_WCHAN"

	# Of course, you can easily change the line above to output more or less information
	# Here is an example which adds nice, vsize and rss
	# echo -e "$PID\t$TID\t$POLICY\t$RTPRIO\t$_NICE\t$_STATE\t$_VSIZE\t$_RSS\t$_COMM\t$_WCHAN"

}

# Print banner. If you change show_details() to output more (or different)
# information, change this as well so you know what is what
echo -e "PID\tTID\tCLS\tRTPRIO\tSTAT\tCOMMAND\tWCHAN"
# echo -e "PID\tTID\tCLS\tRTPRIO\tNICE\tSTAT\tVSIZE\tRSS\tCOMMAND\tWCHAN"

# Get a list of processes from /proc

# Doing it like this gives a numerical listing. The simpler "for p in /proc/[0-9]*"
# would give an alphabetic list in which 2 comes after 10 rather than before.
# N.B. Assumes PIDs are 5 digits or fewer (check /proc/sys/kernel/pid_max)
for p in /proc/[0-9] /proc/[0-9][0-9] /proc/[0-9][0-9][0-9] /proc/[0-9][0-9][0-9][0-9] /proc/[0-9][0-9][0-9][0-9][0-9]; do
	PID=$(basename $p)
	if [ -f /proc/$PID/stat ]; then
		TID=$PID
		PID_TO_SHOW=$PID
		show_details

		# Look in /proc/NNNN/task for a list of threads
		for t in $p/task/*; do
			TID=$(basename $t)
			if [ $TID != $PID ]; then
				PID_TO_SHOW=$TID
				show_details
			fi
		done
	fi
done

Example:

# list_threads.sh 
PID     TID     CLS     RTPRIO  STAT    COMMAND WCHAN
1       1       TS      -       S       (init)  do_wait
...
277     277     TS      -       S       (sh)    do_wait
1093    1093    TS      -       S       (thread-test)   futex_wait
1093    1094    TS      -       S       (thread-test)   hrtimer_nanosleep
1093    1095    TS      -       S       (thread-test)   hrtimer_nanosleep
1093    1096    TS      -       S       (thread-test)   hrtimer_nanosleep
1098    1098    TS      -       R       (list_threads.sh)       0

Finally a note about 'C' libraries: NPTL was incorporated into the GNU C library 2.3 and requires a 2.6 kernel. Many embedded systems use the smaller uClibc library, which doesn't have NPTL, using the older Linux Threads library for POSIX threading. There is an on-going project to integrate NPTL into uClibc but it is not in general use yet. The main difference between NPTL and Linux Threads, so far as this discussion goes, is that in the former all threads have the PID of the process that contains them, whereas with Linux Threads each thread has its own PID. Thus threads show up in process listings by default if you are using Linux Threads but not with NPTL. However, my list_threads script is still useful (I believe) because it can show more information than Busybox.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Helo http://foxfox.ifxworld.com/ you forex

Helo!!! Forex - Outwork a cup of tea get satisfaction from working have free money, just register forex forex

website hosting free

free host
Exceptional Hosting Guidance For Anybody To Use

You could have issues locating a service that is dependable while offering the characteristics you need. Choosing the right host is hard as a result of a variety of possibilities. The information incorporated in this article provides you with the advantage of the skills of your own friends who definitely have been throughout the most severe and come forth with a web-based host that proved helpful in the long run.

Upon having picking your online hosting company, you ought to prefer to make monthly payments every month, rather than investing in an extended period of time. You can't forecast what your small business will be or what your variety is going to do inside the forthcoming a few months. When your company will grow too large for your number or even your company shuts, you might get rid of the amount of money you paid, unless of course the variety says usually.

Website hosting suppliers importance their reputations, since you can discover a lot from what users report. Study online hosts to determine what use a honest reputation plus a long history of excellent service. This will likely also help you organize out those who don't have a good history.

Specific web hosts will enable you to receive a reimburse proportional to the amount of downtime your web site endures. This is definitely not much of a refund in comparison with a damage in revenue throughout the down time. You ought to select a web hosting remedy that gives dependable up-time as opposed to refunds to be offline.

Should your website hosting service gives online talks, forums or putting up boards, then use them for immediate access to information about them. When you can find out about your problems, you may rule out professional services that won't work for you. Choosing the right host company will likely be easier whenever you affirmed by great critiques. Provided you can talk with a current client of a company, they will likely be the very best man or woman to discover information about a business you are considering.

When you are a newbie in web site design, take a web host which offers excellent customer support as opposed to a lot of features. When you first begin there are a lot of inquiries that turn up in your head about internet hosting, so you're gonna want a number that has fantastic customer satisfaction accessible and able to answer your questions. The tech support that you will receive coming from a organization with wonderful customer service will be far more good for you than a bundle of features you might never use.

Are you presently thinking of internet hosting your internet site having a totally free hosting provider? You will want to keep the individual back ups of all the your crucial info, given that free of charge web hosting service solutions often times have small file backup professional services, if any. As a result, if anything fades away, you're at a complete loss.

Opt for your variety based on a range of criteria as opposed to creating your final decision based on cost by itself. Keep your choices open so you can find what works for you. Have a look at all variables prior to selecting your variety and after that choose a prepare which is within your budget. Make sure that the number can supply almost everything essential.

When picking an online hosting firm, it is a good idea to decide on one which has brought quite a few honors. When a organization has numerous hosting honors, as an example, this can present you with a good feeling of the assistance and repair they give. Owning an award is a good sign the hosting company is dependable and has numerous happy customers. Try to find hosts which have won awards that were awarded based upon client votes these are your best option.

Be sure you know any straight down periods your web web hosting service services could have timetabled. It is the best for the maintenance to become timetabled no more than monthly. If it's more often than that, it may result in an excessive amount of lower time for your web site.

If relocating your internet site to another number is one thing you are interested in, make sure to research into no matter if you will certainly be capable to exchange your website address. Some hosts could keep your web site label whenever you depart. Then you will have to alter your brand, perplexing your very long time website visitors.

Plenty of internet hosts will offer you several add-ons which are as to what their support, but these capabilities vary from variety to hold. Stick with the businesses that supply the support you want. For instance, some capabilities may be around on better-costed plans, so search for relevant stipulations.

When looking for a web-based number, do a little more investigation on the internet besides simply looking at the host's site and promotional supplies. Go to sites which can be unbiased and get no link to your supplier, then go through user reviews there. These critiques will assist you to understand a quality support from other hosts.

Some website hosts use a more substantial host to provide their solutions for you. These companies invest in a obstruct of space about the hosting server at a discount, and convert a profit by leasing place to more compact websites. You can examine out diverse web hosts beneath the very same firm, as you may get a much better deal by doing this!

Study your commitment meticulously, and get the internet hosting provider to describe any not clear conditions for you. Charges and conditions will not be easily obvious from the principal textual content of your agreement. No sales pitch will almost certainly stage them in the market to you. Consider the whole price of the assistance, which includes put in place service fees and penalties for early cancellation, before you sign around the dotted line.

An internet variety ought to have numerous back links just in case their principal one particular goes off the internet. Ensure that the website hosts you are considering have this. Should your web host has only a single interconnection to the net, this can be risky simply because that when it goes traditional, your web site may also go offline. Ensure that the firm has redundant relationships and that each one of those links is capable of doing helping your site.

To conclude, it might be difficult for the greatest hosting company. Because of the large number of aspects that must be considered, it can be hard to determine which hosting company will likely be best. Here are some ideas when choosing a web number to suit your enterprise.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.