1

I would like to get the number of ALL running processes and ALL running threads of my Linux system. I need this information IN a C application. From the terminal, I can get his info with

$ ps -A --no-headers | wc -l

for processes and

$ ps -AL --no-headers | wc -l

for processes including tasks.

I didn't find this info in /proc.
Any suggestions?

4
  • 1
    popen()? Commented Oct 11, 2018 at 10:47
  • One linux way is to look in the virtual folder proc/self/ eg ls /proc/self/task -lL shows the 1 thread ls has launched. Commented Oct 11, 2018 at 11:18
  • Are you looking for the processes/threads that are children of a given (C) process ? Or are you looking to query the total number of processes/threads from a C process ? I interpret your question as the latter, but others seem to interpret it as the former. Commented Oct 11, 2018 at 11:27
  • @SanderDeDycker I wasn't clear enough. It would like to have the number of ALL processes and ALL thread running on my linux system. This must be done within my C application. Commented Oct 11, 2018 at 11:44

3 Answers 3

3

The ps examples in your question don't really give you the information you're asking about: the first lists all the processes on your system, not just those spawned by a particular program, and similarly the second lists the number of threads in every process.

If you want to find information about threads spawned by a particular process, you can look in /proc under /proc/<pid>/task. For example, here is a process with a single thread:

bash-4.4$ ls /proc/15355/task/
15355

And here's one that has three threads (in addition to the main thread):

bash-4.4$ ls /proc/15295/task/
15295  15296  15297  15298

The corresponding ps -L output for that process looks like:

bash-4.4$ ps -L -p 15295
  PID   LWP TTY          TIME CMD
15295 15295 pts/4    00:00:00 python
15295 15296 pts/4    00:00:00 python
15295 15297 pts/4    00:00:00 python
15295 15298 pts/4    00:00:00 python

Getting the number of running processes from /proc takes a little more work, since Linux only maintains information about a process' parent, rather than it's children. That means you would need to scan through /proc and find every process for which the parent is your target process...and then repeat recursively for each of those processes.

You can use something like pstree to get this information, of course, but that output isn't really designed to be machine parseable.

Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't clear enough and edited my question. I indeed was asking for ALL processes and ALL threads running.
2

Every running process has a corresponding directory /proc/<pid>. You can use that to count the number of running processes (by counting all sub-directories of /proc that are numeric).

Inside each of these directories you can check /proc/<pid>/status to get information about the process. Specifically, the line Threads: <cnt> gives you the number of threads for that process.

Refer to man proc for more info on the /proc (pseudo) file system.

Comments

0

The simplest is to parse the output of commands using popen.

The following:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void parse_output(char *buf, size_t bufsize, const char cmd[]) 
{
    assert(buf != NULL);
    assert(cmd != NULL);

    FILE *fp;

    // add dynamic allocation here
    memset(buf, 0, bufsize);

    if ((fp = popen(cmd, "r")) == NULL) {
        printf("Error opening pipe!\n");
        exit(-__LINE__);
    }

    // worst speed ever. And strlen is called twice...
    while (fgets(&buf[strlen(buf)], bufsize - strlen(buf), fp) != NULL);

    if(pclose(fp))  {
        printf("Command not found or exited with error status\n");
        exit(-__LINE__);
    }
}


int main() {
    char buf[256];
    long num;

    parse_output(buf, sizeof(buf), "ps -A --no-headers | wc -l");
    if (sscanf(buf, "%ld", &num) != 1) {
        exit(-__LINE__);
    }
    printf("Number of processes: %ld\n", num);

    parse_output(buf, sizeof(buf), "ps -AL --no-headers | wc -l");
    if (sscanf(buf, "%ld", &num) != 1) {
        exit(-__LINE__);
    }
    printf("Number of processes including tasks: %ld\n", num);

}

will output on my system:

$ gcc 1.c && ./a.out
Number of processes: 241
Number of processes includeing tasks: 867

2 Comments

after a while running this function i got this issue: error: can not access /proc error: can not access /proc error: can not access /proc Error opening pipe! sscanf error process count Error opening pipe! sscanf error thread_count Error opening pipe! sscanf error process count Error opening pipe! sscanf error thread_count Segmentation fault
after a while? thread_count ? sscanf error process count? Segmentation fault? This code just runs once and exists. These doesn't look like messages from this code. Probably the best action would be to create a new question including an MCVE and including errors you posted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.