Find the answer to your Linux question:
Results 1 to 7 of 7
Hi All, How to get list of threads of a process using C code in Linux? Thanks in advance for your help...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9

    How to get list of thread in running process


    Hi All,

    How to get list of threads of a process using C code in Linux?

    Thanks in advance for your help

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    Would this be fore another process, or threads in the same process you would be running this function from?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9
    Same process

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    One would think you already have a list of the threads you have started. You can check if they are alive easily enough.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9
    No, I can't since they are not created by me.

    In short our required is that we are creating a share library which can be loaded by a process and that share library analysis the process which load it. Since share library is in address space of the process so I need to find out how many threads by the process and what is status of their stack trace etc.

    Let me know if you need more information..

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    Thanks. That clarifies things somewhat. I'll respond to this after I've had a bit of time to consider the best options. In the meantime, someone else might have some feedback on this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Mar 2010
    Posts
    1
    I see there has not been any activity on this thread for a while, but since it asks almost exactly what I wanted to do and I have come up with a solution, here are some notes.

    I have come up with two approaches to this problem, from reading other posts.

    Firstly, bear in mind that the list of processes is available in /proc

    The first aproach is to use a call to the ps program, like:
    system("ps ax | grep programname > filtereedoutput");
    programname is the name of a process that you want to find, and filteredoutput is a file to contain the resulting output.
    Once you have the output file, you can open it for reading and look for any lines that match the name you are looking for. In my case, all I had to do was count the number of lines in the filteredoutput file. More than 2 lines, my target process was running is running, otherwise I need to re-start it.

    The second approach, which I have only investigated briefly so far, is to take a look at the source code for ps, found on sourceforge.net under the heading of procps, particularly in the minimal.c source file. (Sorry, not allowed to post the full URL yet due to less then 15 posts in the forum).

    I used the system() call approach for my solution in about 10 minutes, including testing. It took 10 minutes just to find where the source code started for the part of ps which gets the process list information. No contest for my application.
    Hope this helps someone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •