通过 C 代码计算 Minix 系统上正在运行的进程数
所以,一开始这看起来很简单,但是在爬行谷歌和这里之后,答案似乎并不像我最初想象的那么简单。
基本上,我正在编辑一个 MINIX 内核,作为我的操作系统课程实践的一部分,并且我必须添加一个小功能,当您在信息服务器中按下功能键时,该功能可以显示正在运行的进程数。我已经弄清楚如何集成该功能,以便所有其他东西都可以工作,但是对于我来说,我无法弄清楚如何将系统中运行的当前进程数放入我的 C 代码中并放入一个变量中打印出。
首先,我认为会有一个漂亮的系统调用,例如 SYS_NUMPROCS 或返回值的东西,但没有运气。 然后,我尝试将系统(“ps -ax | wc -l”)的输出通过管道传输到文件,但该文件不会创建。我尝试使用 popen() ,但也没有运气 - 即使使用简单的“ls”读入缓冲区,它也会轰炸代码并“挂起”代码的运行,因此没有输出。
所以现在我真的很困惑,任何帮助都会非常棒,因为在这一点上我已经用尽了所有明显的选择。
我现在能想到的唯一两件事是循环计算所有进程,但首先你必须访问系统的进程列表,而且我听说过关于 /proc/ 作为目录的模糊说法,但我没有'不知道如何访问/运行它,或者它如何链接到首先获取进程数。
谢谢堆栈(哈哈双关语),伙计们:)
另外,我没有明确包含代码,因为除了用于装饰输出的基本 printf'ing 之外,我没有编写任何内容,因为我尝试过的所有事情都没有给我带来任何快乐: /
编辑注释:伙计们,这是一个内核编辑 - 我正在编写函数来打印系统 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() 调用的输出之外,还必须有更好的方法来执行此操作。
再次编辑:上面的代码可以在用户程序中完美运行,但在内核函数中无法运行。有人知道为什么吗?:/
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 part of a practical for my Operating Systems course, and I have to add a little function that spits out the number of running processes when you hit a function key in the Information Server. I've figured out how to integrate the functionality so all the other stuff works, but for the life of me, I can not figure out how to get the current number of processes running in the system into my C code and into a variable to print out.
First I thought there'd be a nifty Syscall like SYS_NUMPROCS or something that'd return the value, but no luck.
Then, I tried piping output from a system("ps -ax | wc -l") to a file and the file wouldn't create. I tried using popen() and no luck there either - even with a simple "ls" read into a buffer, it just bombs the code and "hangs" the run of the code, so there's no output.
So now I'm truly stumped, and any help would be super awesome, because at this point I've exhausted all the obvious options.
The only two things I can think of now would be a loop counting all the processes, but first you have to get to the system's process list, and I've heard vague things said about /proc/ as a directory, but I haven't a clue how to access/run through that or how it'd link up to getting the number of processes in the first place.
Thanks a stack (lol pun), guys :)
Also, I haven't included code explicitly because nothing I've written aside from basic printf'ing for cosmetic output, because none of the things I've tried gave me any joy :/
Edit notes: Guys, this is a kernel edit - I'm writing the function to printf the information in a system C file, then recompiling the kernel and rebooting the system to test. It's a UNIX (MINIX) kernel, not a Linux kernel, and it's not a user mode program.
My code for popen(), as some of you requested, is as follows:
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);
}
That's a bit of a hacked together version from what I remember and keeping it ultra simple and showing you guys that's what I was trying to do. Again though, there must be a better way to do this aside from essentially calling the output of a system() call.
Edit again: the above code woks perfectly from a user program but won't work from the kernel function. Anybody have an idea why?:/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这将为您提供正在运行的进程数
This will get you the number of processes running
尝试看看 ps 做了什么。看它的源代码;它知道有多少个进程
try looking to see what ps does. Look at its source code; it knows how many processes there are
也许您可以向我们展示您为捕获
system("ps -ax | wc -l")
结果而编写的代码,或者您为使用popen
编写的代码以及我们可以帮助您诊断问题。无论如何,我能想到的计算系统上现有(与正在运行的)进程数量的最有效方法是
opendir("/proc")
并计算十进制数字字符串的条目数。系统中的每个进程都会用/proc的子目录来表示,以小数点命名该进程的进程 ID 号。因此,例如,如果您找到“/proc/3432”,那么您就知道存在一个 pid 为“3432”的进程。只需计算您找到的名称为十进制数字的子目录的数量即可。
Assumptions:
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 usepopen
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:
所以我一直遇到同样的问题并找到了解决方案。 (MINIX 3.1) 在计算进程数的方法中使用以下代码:
(这是 ANSI C)
它只是运行进程表并计算进程数。
我知道这是一个旧线程,但它可能会对将来的某人有所帮助。
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.
我在大学有同样的作业,所以如果将来有人需要我的解决方案,我会发布我的解决方案。我正在使用 Minix 3.3 和 VMware Player 作为虚拟机。
在pm服务器中的/usr/src/minix/servers/pm位置有一个glo.h文件,其中包含pm使用的各种全局变量服务器。幸运的是,在该文件中,有一个名为 procs_in_use 的变量,定义为
EXTERN int procs_in_use;
系统调用中的简单 printf("%d\n",procs_in_use); 将显示当前正在运行的进程数。您可以通过在循环中间的用户空间程序中添加
fork()
函数来测试这一点。再提一下:第一个答案
对我来说不起作用。
SI_KINFO
不再存在,因此您应该使用SI_PROC_TABLE
。此外,权限也可能存在问题,因此您将无法从常规系统调用中调用此函数。还有一个替代函数 sys_getkinfo(&kinfo) 可以从新的系统调用中调用,其作用与上述相同。问题是 kinfo.nr_procs 不会返回当前进程数,而是返回操作系统中可以存在的最大用户进程数,默认为 256,并且可以在定义 NR_PROCS 的文件中手动更改。另一方面,kinfo.nr_tasks
将返回操作系统可以容纳的最大内核进程数,默认为 5。I had the same assignment at my university so i will post my solution if someone needs it in future. I am using Minix 3.3 and VMware player for virtual machines.
In pm server at location /usr/src/minix/servers/pm there is glo.h file which contains various global variables used by pm server. In that file, there is fortunately one variable called procs_in_use defined as
EXTERN int procs_in_use;
So simple
printf("%d\n",procs_in_use);
from a system call will show the number of current processes running. You can test this by addingfork()
function in your user space program in the middle of a loop.One more mention : first answer that says
didn't work out for me.
SI_KINFO
no more exists so you should useSI_PROC_TABLE
. Also there can be problems with permissions, so you will not be able to call this function from your regular system call. There is alternative functionsys_getkinfo(&kinfo)
that can be called from your fresh system call and that will do the same as the above. The problem iskinfo.nr_procs
will not return number of current processes but number of maximum user processes that can be in operative system which is 256 by default, and can be changed manually in file where NR_PROCS is defined. On the other handkinfo.nr_tasks
will return maximum number of kernel processes that can be held by operative system, which is 5 by default.看看这个: http://procps.sourceforge.net/
它有许多小型实用程序的源代码做这些事情。这将是一次很好的学习经历:)而且我认为 PS 就在那里,正如 pm100 所指出的那样。
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.
如果您正在编辑内核,解决此问题的最有效方法是在每次创建进程(即,task_struct 条目)时维护计数(并确保在进程每次终止时递减计数)。
您始终可以使用内置宏循环遍历内核中的进程列表(但其成本昂贵,因此您应该尽量避免它):
If you're editing the kernel, the most efficient way to solve this problem is to maintain a count every time a process (i.e., a task_struct entry) is created (and make sure you decrement the count every where a process terminates).
You could always loop through the list of processes in the kernel using the built-in macro (but its expensive, so you should try to avoid it):
看看这个: http://sourceforge.net/p/readproc/code/ ci/master/树/
Check this out: http://sourceforge.net/p/readproc/code/ci/master/tree/