Linux C gets the total number of currently running processes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.