Saturday, March 24, 2012

Processes and Threads

While browsing through the trace output produced by a prebuilt strace binary I found from this blog, I discovered that a process on Android (and in Linux) could use the clone syscall to create threads. This syscall creates new processes, but by specifying the new process to share the address space with its parent, a thread is created instead. Turns out that the pthread (POSIX thread) library is typically implemented using the clone syscall.

Sometimes when you see a pid that you don't normally see in the output of ps, that pid might in fact belong to a thread.


To see all the threads in the system, do a  ps -t, and you would get something like the following. The -t flag seems to work only in Android, but not in Linux generally.


Note that the following is an incomplete listing of a ps -t output. In particular, it shows the web browser process (pid 320) and all its threads (ppid 320).



user      pid   ppid  
app_20    320   33    136684 57988 ffffffff afd0c51c S com.android.browser
app_20    321   320   136684 57988 c0059e24 afd0c738 S HeapWorker
app_20    322   320   136684 57988 c0059e24 afd0c738 S GC
app_20    323   320   136684 57988 c0047be8 afd0bfec S Signal Catcher
app_20    324   320   136684 57988 c02181f4 afd0c22c S JDWP
app_20    325   320   136684 57988 c0059e24 afd0c738 S Compiler
app_20    326   320   136684 57988 c01a94a4 afd0b6fc S Binder Thread #
app_20    327   320   136684 57988 c01a94a4 afd0b6fc S Binder Thread #
app_20    330   320   136684 57988 c00b8fec afd0c51c S CookieSyncManag
app_20    332   320   136684 57988 c0059e24 afd0c738 S pool-1-thread-1
app_20    333   320   136684 57988 c0059e24 afd0c738 S AsyncTask #1
app_20    335   320   136684 57988 c00b8fec afd0c51c S WebViewCoreThre
app_20    336   320   136684 57988 c00b8fec afd0c51c S WebViewWorkerTh
app_20    338   320   136684 57988 c0059e24 afd0c738 S AsyncTask #2
app_20    339   320   136684 57988 c0059e24 afd0c738 S AsyncTask #3
app_20    340   320   136684 57988 c0059e24 afd0c738 S WebViewCoreThre
app_20    341   320   136684 57988 c0059e24 afd0c738 S http0
app_20    342   320   136684 57988 c0059e24 afd0c738 S http1
app_20    343   320   136684 57988 c0059e24 afd0c738 S http2
app_20    344   320   136684 57988 c0059e24 afd0c738 S http3
app_20    345   320   136684 57988 c0059e24 afd0c738 S AsyncTask #4
app_20    347   320   136684 57988 c0059e24 afd0c738 S AsyncTask #5
app_20    348   320   136684 57988 c0059e24 afd0c738 S WebViewCoreThre
app_20    359   320   136684 57988 c01a94a4 afd0b6fc S Binder Thread #
app_20    360   320   136684 57988 c01a94a4 afd0b6fc S Binder Thread #
app_20    361   320   136684 57988 c01a94a4 afd0b6fc S Binder Thread #

No comments: