c - 小型机 : Determining size of process table

标签 c linux operating-system system-calls

我想遍历所有可能的进程 ID,但为此,我需要知道进程表的限制。我怎样才能找到它?

我的想法是做类似的事情

    while (counter < table size)
{
    do something
}

最佳答案

我认为在C中没有posix API可以直接获取这些信息,你需要popen()函数调用命令行来获取这种信息,我不推荐system()虽然它与popen类似。( Display all process using a posix function ).

系统最大进程数可以通过命令ulimit设置/检查。

popen("ulimit -a max user processes","r"); 

或者您可以检查当前进程 ID 中的当前最大数量。(ps aux 显示所有进程,sed 查找最后一行,awk 查找进程 ID)

FILE *fp = popen("ps aux|sed -n '$p'|awk '{print $2F}'","r");
if(NULL!=fp)
{
    char buff[1024];
    fgets(buff,1024,fp);
    printf("%s\n",buff);
    pclose(fp);
}

关于c - 小型机 : Determining size of process table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46739985/

相关文章:

c - 在结构中插入定义为函数指针的 C 函数

C - 宏观矛盾

linux - 从 asciidoc 创建 pdf 的 Shell 脚本

php - 使用 PHP 脚本加载测试服务器?

c# - 如何获取 "friendly"操作系统版本名称?

linux - 将 I/O 驱动的调度合并到操作系统内核中

assembly - 操作系统中的加载程序如何工作?

c - 如何移动整个二维数组

c - 如何旋转 SSE/AVX vector

php - 从另一个 cron 作业创建 cron 作业