Number of Running Processes on a Minix system from C code

前端 未结 8 1017
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题