How to get (from terminal) total number of threads (per process and total for all processes)












26















I tried googling it, but I can't find it. I am looking for:




  1. number of threads in process X


  2. total number of threads running currently











share|improve this question

























  • stackoverflow.com/questions/268680/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Nov 3 '15 at 17:15
















26















I tried googling it, but I can't find it. I am looking for:




  1. number of threads in process X


  2. total number of threads running currently











share|improve this question

























  • stackoverflow.com/questions/268680/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Nov 3 '15 at 17:15














26












26








26


9






I tried googling it, but I can't find it. I am looking for:




  1. number of threads in process X


  2. total number of threads running currently











share|improve this question
















I tried googling it, but I can't find it. I am looking for:




  1. number of threads in process X


  2. total number of threads running currently








system-info






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '11 at 18:24









enzotib

63.2k6133154




63.2k6133154










asked Dec 19 '11 at 14:16









NoSenseEtAlNoSenseEtAl

2031310




2031310













  • stackoverflow.com/questions/268680/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Nov 3 '15 at 17:15



















  • stackoverflow.com/questions/268680/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Nov 3 '15 at 17:15

















stackoverflow.com/questions/268680/…

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 3 '15 at 17:15





stackoverflow.com/questions/268680/…

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 3 '15 at 17:15










5 Answers
5






active

oldest

votes


















43














To get the number of threads for a given pid:



ps -o nlwp <pid>


To the get the sum of all threads running in the system:



ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'





share|improve this answer



















  • 1





    ps -o nlwp <pid> returns NLWP :), what does that mean ?

    – Siddharth
    May 4 '13 at 5:12






  • 1





    @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

    – jcollado
    May 7 '13 at 2:45











  • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

    – bufh
    Aug 9 '16 at 9:00



















7














For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.






share|improve this answer

































    2














    I'm basing this answer around ps axms. ps is a great tool for listing what's running.



    If you want to filter that by a process, you could try something like this:



    echo $(( `ps axms | grep firefox | wc -l`  - 1))


    We subtract 1 because grep will show in that list.



    For all threads in general this should work:



    echo $(( `ps axms | wc -l`  - 1))


    We subtract one this time because there is a header row.






    share|improve this answer
























    • This is inaccurate, as it reports an extra thread per process

      – Score_Under
      2 days ago



















    1














    To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time.
    But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.






    share|improve this answer































      0














      On linux specifically, here is one way to do it per-process:



      #!/bin/sh
      while read name val; do
      if [ "$name" = Threads: ]; then
      printf %s\n "$val"
      return
      fi
      done < /proc/"$1"/status


      You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.



      To get the thread count for the whole system, this suffices:



      #!/bin/sh
      count() {
      printf %s\n "$#"
      }
      count /proc/[0-9]*/task/[0-9]*


      These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk-based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).






      share|improve this answer

























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "89"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f88972%2fhow-to-get-from-terminal-total-number-of-threads-per-process-and-total-for-al%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        43














        To get the number of threads for a given pid:



        ps -o nlwp <pid>


        To the get the sum of all threads running in the system:



        ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'





        share|improve this answer



















        • 1





          ps -o nlwp <pid> returns NLWP :), what does that mean ?

          – Siddharth
          May 4 '13 at 5:12






        • 1





          @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

          – jcollado
          May 7 '13 at 2:45











        • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

          – bufh
          Aug 9 '16 at 9:00
















        43














        To get the number of threads for a given pid:



        ps -o nlwp <pid>


        To the get the sum of all threads running in the system:



        ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'





        share|improve this answer



















        • 1





          ps -o nlwp <pid> returns NLWP :), what does that mean ?

          – Siddharth
          May 4 '13 at 5:12






        • 1





          @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

          – jcollado
          May 7 '13 at 2:45











        • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

          – bufh
          Aug 9 '16 at 9:00














        43












        43








        43







        To get the number of threads for a given pid:



        ps -o nlwp <pid>


        To the get the sum of all threads running in the system:



        ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'





        share|improve this answer













        To get the number of threads for a given pid:



        ps -o nlwp <pid>


        To the get the sum of all threads running in the system:



        ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 19 '11 at 14:41









        jcolladojcollado

        7,11811923




        7,11811923








        • 1





          ps -o nlwp <pid> returns NLWP :), what does that mean ?

          – Siddharth
          May 4 '13 at 5:12






        • 1





          @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

          – jcollado
          May 7 '13 at 2:45











        • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

          – bufh
          Aug 9 '16 at 9:00














        • 1





          ps -o nlwp <pid> returns NLWP :), what does that mean ?

          – Siddharth
          May 4 '13 at 5:12






        • 1





          @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

          – jcollado
          May 7 '13 at 2:45











        • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

          – bufh
          Aug 9 '16 at 9:00








        1




        1





        ps -o nlwp <pid> returns NLWP :), what does that mean ?

        – Siddharth
        May 4 '13 at 5:12





        ps -o nlwp <pid> returns NLWP :), what does that mean ?

        – Siddharth
        May 4 '13 at 5:12




        1




        1





        @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

        – jcollado
        May 7 '13 at 2:45





        @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.

        – jcollado
        May 7 '13 at 2:45













        You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

        – bufh
        Aug 9 '16 at 9:00





        You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid

        – bufh
        Aug 9 '16 at 9:00













        7














        For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.






        share|improve this answer






























          7














          For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.






          share|improve this answer




























            7












            7








            7







            For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.






            share|improve this answer















            For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 28 '14 at 0:36









            Eric Carvalho

            41.6k17115146




            41.6k17115146










            answered Feb 28 '14 at 0:15









            anaken78anaken78

            7112




            7112























                2














                I'm basing this answer around ps axms. ps is a great tool for listing what's running.



                If you want to filter that by a process, you could try something like this:



                echo $(( `ps axms | grep firefox | wc -l`  - 1))


                We subtract 1 because grep will show in that list.



                For all threads in general this should work:



                echo $(( `ps axms | wc -l`  - 1))


                We subtract one this time because there is a header row.






                share|improve this answer
























                • This is inaccurate, as it reports an extra thread per process

                  – Score_Under
                  2 days ago
















                2














                I'm basing this answer around ps axms. ps is a great tool for listing what's running.



                If you want to filter that by a process, you could try something like this:



                echo $(( `ps axms | grep firefox | wc -l`  - 1))


                We subtract 1 because grep will show in that list.



                For all threads in general this should work:



                echo $(( `ps axms | wc -l`  - 1))


                We subtract one this time because there is a header row.






                share|improve this answer
























                • This is inaccurate, as it reports an extra thread per process

                  – Score_Under
                  2 days ago














                2












                2








                2







                I'm basing this answer around ps axms. ps is a great tool for listing what's running.



                If you want to filter that by a process, you could try something like this:



                echo $(( `ps axms | grep firefox | wc -l`  - 1))


                We subtract 1 because grep will show in that list.



                For all threads in general this should work:



                echo $(( `ps axms | wc -l`  - 1))


                We subtract one this time because there is a header row.






                share|improve this answer













                I'm basing this answer around ps axms. ps is a great tool for listing what's running.



                If you want to filter that by a process, you could try something like this:



                echo $(( `ps axms | grep firefox | wc -l`  - 1))


                We subtract 1 because grep will show in that list.



                For all threads in general this should work:



                echo $(( `ps axms | wc -l`  - 1))


                We subtract one this time because there is a header row.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 19 '11 at 14:31









                OliOli

                221k86561763




                221k86561763













                • This is inaccurate, as it reports an extra thread per process

                  – Score_Under
                  2 days ago



















                • This is inaccurate, as it reports an extra thread per process

                  – Score_Under
                  2 days ago

















                This is inaccurate, as it reports an extra thread per process

                – Score_Under
                2 days ago





                This is inaccurate, as it reports an extra thread per process

                – Score_Under
                2 days ago











                1














                To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time.
                But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.






                share|improve this answer




























                  1














                  To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time.
                  But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.






                  share|improve this answer


























                    1












                    1








                    1







                    To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time.
                    But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.






                    share|improve this answer













                    To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time.
                    But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 21 '15 at 7:02









                    LaksithLaksith

                    1396




                    1396























                        0














                        On linux specifically, here is one way to do it per-process:



                        #!/bin/sh
                        while read name val; do
                        if [ "$name" = Threads: ]; then
                        printf %s\n "$val"
                        return
                        fi
                        done < /proc/"$1"/status


                        You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.



                        To get the thread count for the whole system, this suffices:



                        #!/bin/sh
                        count() {
                        printf %s\n "$#"
                        }
                        count /proc/[0-9]*/task/[0-9]*


                        These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk-based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).






                        share|improve this answer






























                          0














                          On linux specifically, here is one way to do it per-process:



                          #!/bin/sh
                          while read name val; do
                          if [ "$name" = Threads: ]; then
                          printf %s\n "$val"
                          return
                          fi
                          done < /proc/"$1"/status


                          You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.



                          To get the thread count for the whole system, this suffices:



                          #!/bin/sh
                          count() {
                          printf %s\n "$#"
                          }
                          count /proc/[0-9]*/task/[0-9]*


                          These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk-based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).






                          share|improve this answer




























                            0












                            0








                            0







                            On linux specifically, here is one way to do it per-process:



                            #!/bin/sh
                            while read name val; do
                            if [ "$name" = Threads: ]; then
                            printf %s\n "$val"
                            return
                            fi
                            done < /proc/"$1"/status


                            You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.



                            To get the thread count for the whole system, this suffices:



                            #!/bin/sh
                            count() {
                            printf %s\n "$#"
                            }
                            count /proc/[0-9]*/task/[0-9]*


                            These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk-based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).






                            share|improve this answer















                            On linux specifically, here is one way to do it per-process:



                            #!/bin/sh
                            while read name val; do
                            if [ "$name" = Threads: ]; then
                            printf %s\n "$val"
                            return
                            fi
                            done < /proc/"$1"/status


                            You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.



                            To get the thread count for the whole system, this suffices:



                            #!/bin/sh
                            count() {
                            printf %s\n "$#"
                            }
                            count /proc/[0-9]*/task/[0-9]*


                            These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk-based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 2 days ago

























                            answered 2 days ago









                            Score_UnderScore_Under

                            32415




                            32415






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Ask Ubuntu!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f88972%2fhow-to-get-from-terminal-total-number-of-threads-per-process-and-total-for-al%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How did Captain America manage to do this?

                                迪纳利

                                南乌拉尔铁路局