有没有一种简便的方法来获取Linux中当前的进程数?

编程入门 行业动态 更新时间:2024-10-26 15:15:21
本文介绍了有没有一种简便的方法来获取Linux中当前的进程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望我的(基于C/C ++)程序显示一个数字指示器,该指示器指示本地系统上当前存在多少个进程.经常会查询运行次数的值(例如每秒一次)以更新我的显示.

是否有一种简便的方法来获取该号码?显然,我可以称其为"ps ax | wc -l",但我不希望强迫计算机产生一个进程并解析几百行文本,以得出一个整数.

该程序主要在Linux下运行,但也可能在MacOS/X或Windows下运行,因此与这些OS相关的技术也将有所帮助.

理想情况下,我正在寻找类似此,但在Linux下可用(getsysinfo()似乎更像是Minix的东西).

解决方案

....当然,在我发布问题1分钟后,我便找到了答案:sysinfo 将返回(除其他外)一个表明有多少进程的字段.

也就是说,如果有人知道与sysinfo()等效的MacOS/X和/或Windows,我仍然对此感兴趣.

更新:这是我最终得到的功能.

#ifdef __linux__#include< sys/sysinfo.h>#elif已定义(__APPLE__)#include< sys/sysctl.h>#elif定义(WIN32)#include< Psapi.h>#万一int GetTotalNumProcesses(){#如果已定义(__linux__)struct sysinfo si;返回(sysinfo(& si)== 0)吗?(int)si.procs:(int)-1;#elif已定义(__APPLE__)size_t长度= 0;静态const int名称[] = {CTL_KERN,KERN_PROC,KERN_PROC_ALL,0};返回(sysctl((int *)names,(((sizeof(names)/sizeof(names [0]))-1,NULL,& length,NULL,0)== 0)?(int)(length/sizeof(kinfo_proc)):(int)-1;#elif定义(WIN32)DWORD aProcesses [1024],cbNeeded;返回EnumProcesses(aProcesses,sizeof(aProcesses),& cbNeeded)吗?(cbNeeded/sizeof(DWORD)):-1;#别的返回-1;#万一}

I want my (C/C++ based) program to display a numeric indicator of how many processes are currently present on the local system. The number-of-running-processes value would be queried often (e.g. once per second) to update my display.

Is there a lightweight way to get that number? Obviously I could call "ps ax | wc -l", but I'd prefer not to force the computer to spawn a process and parse several hundred lines of text just to come up with a single integer.

This program will be running primarily under Linux, but it might also run under MacOS/X or Windows also, so techniques relevant to those OS's would be helpful also.

Ideally I'm looking for something like this, except available under Linux (getsysinfo() appears to be more of a Minix thing).

解决方案

.... and of course 1 minute after I post the question, I figure out the answer: sysinfo will return (amongst other things) a field that indicates how many processes there are.

That said, if anyone knows of a MacOS/X and/or Windows equivalent to sysinfo(), I'm still interested in that.

Update: Here's the function I finally ended up with.

#ifdef __linux__ # include <sys/sysinfo.h> #elif defined(__APPLE__) # include <sys/sysctl.h> #elif defined(WIN32) # include <Psapi.h> #endif int GetTotalNumProcesses() { #if defined(__linux__) struct sysinfo si; return (sysinfo(&si) == 0) ? (int)si.procs : (int)-1; #elif defined(__APPLE__) size_t length = 0; static const int names[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; return (sysctl((int *)names, ((sizeof(names)/sizeof(names[0]))-1, NULL, &length, NULL, 0) == 0) ? (int)(length/sizeof(kinfo_proc)) : (int)-1; #elif defined(WIN32) DWORD aProcesses[1024], cbNeeded; return EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded) ? (cbNeeded/sizeof(DWORD)) : -1; #else return -1; #endif }

更多推荐

有没有一种简便的方法来获取Linux中当前的进程数?

本文发布于:2023-08-07 19:51:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法来   简便   进程   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!