The command to get the total number of currently running processes is:
-l
The source code for the total number of currently running processes is as follows:
#include <stdio.h>#include <stdlib.h>#include <dirent.h>#include <string.h>#include <ctype.h>#include <errno.h>intMainintargcChar*argv[]) {DIR *DP;structDirent *dirp;intI, 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("Current number of system running processes%d\n", count); Closedir (DP);return 0;}
Summarize:
大概的编程思想就是统计 /proc 目录下 所有以数字命名的文件夹 个数。
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux C gets the total number of currently running processes