Created
November 12, 2009 04:09
-
-
Save anonymous/232587 to your computer and use it in GitHub Desktop.
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
/***** | |
* | |
* | |
*Alex Jones 7b PS emulation | |
**/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <ctype.h> | |
#include <dirent.h> | |
#include <sys/wait.h> | |
DIR *proc; | |
struct dirent *current; | |
int Running = 0; | |
int Sleeping = 0; | |
typedef struct stats | |
{ | |
int PID; | |
int PPID; | |
int Memsize; | |
char State; | |
char *Pname; | |
} PS; | |
PS List[5000]; | |
int listsize = 0; | |
void insert (PS input); | |
void printall(); | |
void printall(){ | |
for(int i = 0 ; i < 10; i++){ | |
printf("%d\n",i); | |
} | |
} | |
void | |
insert (PS input) | |
{ | |
if (listsize == 0) | |
{ | |
List[0] = input; | |
listsize++; | |
return; | |
} | |
else | |
{ | |
int i = 0; | |
while (i < 5000 && List[i].Memsize > input.Memsize) | |
{ | |
i++; | |
} | |
for (int end = listsize; end >= i; end--) | |
{ | |
List[end] = List[end - 1]; | |
} | |
List[i] = input; | |
listsize++; | |
return; | |
} | |
} | |
int | |
main (void) | |
{ | |
proc = opendir ("/proc"); | |
if (proc == NULL) | |
{ | |
printf ("error in directory open\n"); | |
exit (1); | |
} | |
else | |
{ | |
printf (" PID Name State PPID\n"); | |
while ((current = readdir (proc)) != NULL) | |
{ | |
if (isdigit (current->d_name[0])) | |
{ | |
//printf ("fork\n"); | |
//scavenge | |
//printf("%s\n",current -> d_name); | |
char path[100] = "/proc/"; | |
int pos = 6; | |
while (current->d_name[pos - 6] != '\0') | |
{ | |
path[pos] = current->d_name[pos - 6]; | |
pos++; | |
} | |
path[pos++] = '/'; | |
path[pos++] = 's'; | |
path[pos++] = 't'; | |
path[pos++] = 'a'; | |
path[pos++] = 't'; | |
path[pos++] = '\0'; | |
FILE *stat = fopen (path, "r"); | |
printf ("%s\n", path); | |
path[pos] = '\0'; | |
path[pos - 1] = 'm'; | |
printf ("%s\n", path); | |
FILE *statm = fopen (path, "r"); | |
if (stat != NULL) | |
{ | |
PS output; | |
fscanf (stat, "%d ", &output.PID); | |
//printf ("%5d ", output.PID); //valid | |
fscanf (stat, "%s ", output.Pname); | |
//printf ("%20s ", output.Pname); | |
fscanf (stat, "%c ", &output.State); | |
//output.State = 'S'; | |
//printf ("%c ", output.State); | |
fscanf (stat, "%d ", &output.PPID); | |
//output.PPID = 0; | |
//printf ("%5d \n", output.PPID); | |
//printf("%d %s %c %d\n",output.PID, output.Pname, output.State, output.PPID); | |
insert (output); | |
fclose (statm); | |
fclose (stat); | |
} | |
} | |
} | |
} | |
printall(); | |
printf ("Running %d Sleeping %d\n", Running, Sleeping); | |
printf ("done\n"); | |
exit (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment