获取当前执行进程总数的命令为:

 ps auxw | wc -l

获取当前执行进程总数的源代码例如以下:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
int main(int argc, char *argv[])
{
    DIR *dp;
    struct dirent *dirp;
    int i, len,count = 0;

    if( (dp = opendir("/proc") )== NULL) {
        fprintf(stderr,"%s file %d line %s",__FILE__,__LINE__, strerror(errno));
        exit(1);
    }
    printf("1\n");
    while((dirp = readdir(dp)) != NULL) {
        if(dirp->d_type == DT_DIR) {
            len = strlen(dirp->d_name);
            for( i = 0; dirp->d_name[i] != 0; ++i) {
                if( ! isdigit(dirp->d_name[i])) {
                    break;
                }
            }
            if( len == i) {
                printf("d_name: %s\n",dirp->d_name);
                ++count;
            }

        }
    }
    printf("当前系统执行进程数 %d\n",count);
    closedir(dp);
    return 0;
}

总结:

大概的编程思想就是统计 /proc 文件夹下 全部以数字命名的文件夹 个数。

相关文章:

  • 2021-11-14
  • 2021-12-06
  • 2022-12-23
  • 2022-03-08
  • 2021-07-29
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2022-12-23
  • 2021-12-28
  • 2021-12-03
  • 2021-08-19
  • 2021-12-28
相关资源
相似解决方案