Number of Running Processes on a Minix system from C code

前端 未结 8 1011
盖世英雄少女心
盖世英雄少女心 2021-01-03 06:14

So, this seemed simple at first, but after crawling Google and here, the answer doesn\'t seem as simple as I first thought.

Basically, I\'m editing a MINIX kernel as

相关标签:
8条回答
  • 2021-01-03 06:44

    So I have been having the same problem and found a solution. (MINIX 3.1) within the method to count the processes use this code: (This is ANSI C)

    It simply runs through the process table and counts the number of processes.

    I know this is an old thread but it might help someone in the future.

    #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 */
    
    0 讨论(0)
  • 2021-01-03 06:45

    Perhaps you could show us the code your wrote for capturing the result of system("ps -ax | wc -l"), or the code you wrote to use popen and we could help you diagnose the problem with it.

    Regardless, the most efficient way I can think of to count the number of existing (not the same as running) processes on the system is to opendir("/proc") and count the number of entries that are strings of decimal digits. Each process in the system will be represented by a subdirectory of /proc, named after the decimal process id number of that process.

    So, if you find "/proc/3432", for example, then you know that there exists a process with a pid of "3432". Simply count the number of subdirectories you find whose names are decimal numbers.


    Assumptions:

    • You are asking about Linux, not MINIX.
    • You are writing a user-mode program, not modifiying the kernel.
    0 讨论(0)
  • 2021-01-03 06:46

    Check this out: 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 讨论(0)
  • 2021-01-03 06:50

    try looking to see what ps does. Look at its source code; it knows how many processes there are

    0 讨论(0)
  • 2021-01-03 06:57

    Check this out: http://procps.sourceforge.net/

    It's got source to a number of small utilities that do these kinds of things. It'll be a good learning experience :) and I think PS is i n there as pm100 noted.

    0 讨论(0)
  • 2021-01-03 07:00
    struct kinfo kinfo;
    int nr_tasks, nr_procs;
    getsysinfo(PM_PROC_NR, SI_KINFO, &kinfo);
    nr_procs = kinfo.nr_pro;
    

    This will get you the number of processes running

    0 讨论(0)
提交回复
热议问题