Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>And of course you can do it in hardcore-way using good old C</p> <p>find_cpu.c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #define MAX_CHILDREN 100 /** * System command execution output * @param &lt;char&gt; command - system command to execute * @returb &lt;char&gt; execution output */ char *system_output (const char *command) { FILE *pipe; static char out[1000]; pipe = popen (command, "r"); fgets (out, sizeof(out), pipe); pclose (pipe); return out; } /** * Finding all process's children * @param &lt;Int&gt; - process ID * @param &lt;Int&gt; - array of childs */ void find_children (int pid, int children[]) { char empty_command[] = "/bin/ps h -o pid --ppid "; char pid_string[5]; snprintf(pid_string, 5, "%d", pid); char *command = (char*) malloc(strlen(empty_command) + strlen(pid_string) + 1); sprintf(command, "%s%s", empty_command, pid_string); FILE *fp = popen(command, "r"); int child_pid, i = 1; while (fscanf(fp, "%i", &amp;child_pid) != EOF) { children[i] = child_pid; i++; } } /** * Parsign `ps` command output * @param &lt;char&gt; out - ps command output * @return &lt;int&gt; cpu utilization */ float parse_cpu_utilization (const char *out) { float cpu; sscanf (out, "%f", &amp;cpu); return cpu; } int main(void) { unsigned pid = 1; // getting array with process children int process_children[MAX_CHILDREN] = { 0 }; process_children[0] = pid; // parent PID as first element find_children(pid, process_children); // calculating summary processor utilization unsigned i; float common_cpu_usage = 0.0; for (i = 0; i &lt; sizeof(process_children)/sizeof(int); ++i) { if (process_children[i] &gt; 0) { char *command = (char*)malloc(1000); sprintf (command, "/bin/ps -p %i -o 'pcpu' --no-headers", process_children[i]); common_cpu_usage += parse_cpu_utilization(system_output(command)); } } printf("%f\n", common_cpu_usage); return 0; } </code></pre> <p>Compile: </p> <pre><code>gcc -Wall -pedantic --std=gnu99 find_cpu.c </code></pre> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload