Created
November 19, 2013 19:10
-
-
Save pronto/7550755 to your computer and use it in GitHub Desktop.
Starting at line 331 of proc/sysinfo.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) { | |
struct direct *ent; | |
DIR *proc; | |
*running=0; | |
*blocked=0; | |
if((proc=opendir("/proc"))==NULL) crash("/proc"); | |
while(( ent=readdir(proc) )) { | |
char tbuf[32]; | |
char *cp; | |
int fd; | |
char c; | |
if (!isdigit(ent->d_name[0])) continue; | |
sprintf(tbuf, "/proc/%s/stat", ent->d_name); | |
fd = open(tbuf, O_RDONLY, 0); | |
if (fd == -1) continue; | |
memset(tbuf, '\0', sizeof tbuf); // didn't feel like checking read() | |
read(fd, tbuf, sizeof tbuf - 1); // need 32 byte buffer at most | |
close(fd); | |
cp = strrchr(tbuf, ')'); | |
if(!cp) continue; | |
c = cp[2]; | |
if (c=='R') { | |
(*running)++; | |
continue; | |
} | |
if (c=='D') { | |
(*blocked)++; | |
continue; | |
} | |
} | |
closedir(proc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment