Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In Linux, I could discover the number of running and blocked processes running

grep "procs_running" /proc/stat

and

grep "procs_blocked" /proc/stat

Is there a way to do it in Mac OS X? Preferably, a way which I could use in a shell script?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Building on Blender's answer, to get the number of running processes the following can be used:

$ ps axo state | grep "R" | wc -l

To get the number of processes in Uninterruptible Sleep you can use(Edit Changed 'D' to 'U', thanks Gilles!):

$ ps axo state | grep "U" | wc -l
share|improve this answer
    
So, a process in Uninterruptible Sleep is equivalent to a Linux' blocked process (I mean, a process with is waiting some operation, such as I/O)? –  brandizzi Dec 13 '10 at 18:31
2  
Uninterruptible wait = uninterruptible sleep = blocked = in a syscall. On OSX, the letter in the ps output is U, not D. –  Gilles Dec 13 '10 at 19:14
2  
A problem with this method is that it's not atomic: it may count some processes more than once and omit others, because these processes have changed in or out of one of the states you're observing. –  Gilles Dec 13 '10 at 19:15
    
I'll use ps + fgrep + wc. Thank you all for your help! Anyway, if one has a more efficient and safe way to do it, I'd like to know! –  brandizzi Dec 14 '10 at 14:55
add comment

htop is nice for this.
It shows you a tree view in addition to sorting by CPU or whatever.

brew install htop
share|improve this answer
add comment

Well, the humane way is to use top. Here is some output:

Processes: 62 total, 3 running, 1 stuck, 58 sleeping, 309 threads      10:33:37
Load Avg: 0.47, 0.57, 0.41  CPU usage: 41.78% user, 7.4% sys, 51.17% idle
SharedLibs: 68K resident, 0B data, 0B linkedit.
MemRegions: 20459 total, 751M resident, 0B private, 390M shared.
PhysMem: 535M wired, 1179M active, 1074M inactive, 2787M used, 1310M free.
VM: 133G vsize, 1024M framework vsize, 374887(174) pageins, 0(0) pageouts.
Networks: packets: 208538/119M in, 37975/3259K out.
Disks: 445803/4601M read, 70299/3187M written.

PID   COMMAND      %CPU TIME     #TH  #WQ  #POR #MREG RPRVT  RSHRD  RSIZE
1046  DataDetector 0.0  00:00.13 5/1  3/1  71+  351+  7548K+ 55M+   17M+
1045  top          5.8  00:00.38 1/1  0    25+  72    1468K  7480K  3312K 
1042  less         0.0  00:00.00 1    0    14   34    360K   2312K  1080K 
1040- cvmsComp_i38 0.0  00:00.03 1    0    18   51    912K   2656K  3384K 
1035  csh          0.0  00:00.02 1    0    17   39    732K   2936K  1816K 
1034  login        0.0  00:00.04 1    0    22   239   3028K  29M    6392K 
1032  Terminal     4.0  00:01.13 7    2    114+ 567+  13M+   97M+   33M+
1025  mdworker     0.0  00:00.11 3    1    48   260   5036K  54M    10M 
1021  Safari       0.0  00:09.56 9    2    132  943   34M    122M   103M 
999   mdworker     0.0  00:05.55 3    1    50   476   12M    77M    99M 
997-  thunderbird- 0.3  00:43.25 12   1    155  1546  73M    110M   147M 
981   Preview      0.0  00:01.84 2    1    103  686   21M    111M   45M 
935-  firefox-bin  4.0  01:43.97 25   1    238  1637  81M    138M   205M 
905-  kass         0.0  00:00.06 2    1    57   394   3760K  53M    9028K 

It's not very script friendly. Here's ps aux:

USER       PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
nope       935   5.3  5.1  1387680 212912   ??  Ss   10:14AM   1:58.11 /Applica
root        56   1.5  2.0   729352  85588   ??  Ss    4:29AM  14:39.52 /Library
nope      1032   1.2  0.8  2892072  34060   ??  R    10:32AM   0:01.80 /Applica
root        59   0.1  0.5   668812  19976   ??  Ss    4:29AM   2:19.72 /Library
_spotlight  1025 0.0  0.3  2533548  10708   ??  SNs  10:29AM   0:00.12 /System/
nope      1021   0.0  2.5  5133320 105128   ??  S    10:26AM   0:09.57 /Applica
nope       999   0.0  2.4  2666868 101324   ??  SNs  10:24AM   0:05.57 /System/
nope       997   0.0  3.6  1211652 150380   ??  Ss   10:24AM   0:43.56 /Users/n
nope       981   0.0  1.1  2919128  46448   ??  S    10:23AM   0:01.86 /Applica
root       905   0.0  0.2   954736   9028   ??  S    10:12AM   0:00.07 /Library
nope       901   0.0  0.5  1027840  19980   ??  S    10:12AM   0:00.26 /Library
nope       900   0.0  0.3  2814856  13464   ??  S    10:12AM   0:00.06 /System/
nope       879   0.0  0.2   957704  10384   ??  S    10:12AM   0:00.39 /Library

Try playing with those. I'm not sure what blocked processes are, but these commands should help.

Good luck!

share|improve this answer
    
I cogitated about counting the number of likes in ps aux output, but I was not sure it lists all processes. Is it so? –  brandizzi Dec 13 '10 at 18:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.