Skip to content

Instantly share code, notes, and snippets.

@pronto
Created November 19, 2013 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pronto/7550755 to your computer and use it in GitHub Desktop.
Save pronto/7550755 to your computer and use it in GitHub Desktop.
Starting at line 331 of proc/sysinfo.c
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