Number of running processes on a Minix system from C code

So, at first it seemed simple, but after going around Google and here the answer doesn’t seem as simple as I thought.

Basically, I edit the MINIX kernel as part of a hands-on course for my operating systems, and I need to add a small function that highlights the number of running processes when I press a function key on the information server. I figured out how to integrate the functionality, so that all other things work, but for a living I can’t figure out how to get the current number of processes running in the system into my C code and into a variable to print.

At first I thought that there would be a great Syscall, such as SYS_NUMPROCS or something that would return a value but no luck. Then I tried to output the pipeline from the system ("ps -ax | wc -l") to the file and the file would not be created. I tried using popen (), and you were out of luck - even with a simple “ls” read in the buffer, it just bombs the code and “freezes” from running the code, so there is no way out.

So, now I'm really puzzled, and any help would be super-awesome, because at that moment I had exhausted all the obvious options.

The only two things that I can think of right now is a loop counting all the processes, but first you need to go to the list of system processes, and I heard the vague things talking about / proc / as a directory, but I don’t know how to get access to it or to launch it or how it will be connected with obtaining the number of processes in the first place.

Thanks stack (lol pun) guys :)

In addition, I did not include the code explicitly, because nothing that I wrote aside from the main printf'ing for cosmetic output, because none of the things I tried caused me joy: /

: , . C, ​​ . ​​UNIX (MINIX), ​​Linux, .

popen(), , :

public void cos_dmp(){
    char buffer[512];
    FILE * f;

    f = popen("ps -ax | wc -l","r");

    fgets(buffer, sizeof(buffer),f);

  //buffer should now contain result of popen()

     printf(buffer);
}

, , , , . , , system().

: , . - , ?:/

+3
8
struct kinfo kinfo;
int nr_tasks, nr_procs;
getsysinfo(PM_PROC_NR, SI_KINFO, &kinfo);
nr_procs = kinfo.nr_pro;

+2

, ps. ; ,

+3

, , system("ps -ax | wc -l"), , , popen, .

, ( , ) - opendir("/proc") , . /proc, .

, , , "/proc/3432", , pid "3432". , , .


:
+3

, . (MINIX 3.1) : ( ANSI C)

.

, , - .

#include "../pm/mproc.h"

/* inside function */

struct mproc *mp;
int i, n=0;

printf("Number of running processes:\n");

getsysinfo(PM_PROC_NR, SI_PROC_TAB, mproc);

for (i = 0; i<NR_PROCS; i++) {
    mp = &mprocs[i];
    if (mp->mp_pid == 0 && i != PM_PROCS_NR) continue;
    n++;   
}

printf("%d", n);

/* function end */
+1

, , - . Minix 3.3 VMware .
pm /usr/src/minix/servers/pm glo.h, , pm . , , , procs_in_use, EXTERN int procs_in_use;
, printf("%d\n",procs_in_use); . , fork() .

: ,

struct kinfo kinfo;
int nr_tasks, nr_procs;
getsysinfo(PM_PROC_NR, SI_KINFO, &kinfo);
nr_procs = kinfo.nr_procs;

. SI_KINFO , SI_PROC_TABLE. , . sys_getkinfo(&kinfo), , . , kinfo.nr_procs , , , 256, , NR_PROCS. , kinfo.nr_tasks , , 5.

+1

: http://procps.sourceforge.net/

, . :), , PS , pm100.

0

, , (.. task_struct) ( , , ).

( , ):

struct task_struct *p;
unsigned int count = 0;
for_each_process(task) {
    count++;
}
0

: http://sourceforge.net/p/readproc/code/ci/master/tree/

#include"read_proc.h"
int main(void)
{
     struct Root * root=read_proc();
     printf("%lu\n",root->len);
     return 0;
}
0

All Articles