The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C Code
Posted by: Jaa | January 28, 2010 @ 12:39am
C Code
[
Download
]
#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <errno.h> #include <ctype.h> #include <string.h> int main (int argc, char *argv[]) { DIR *dir_pointer, *proc_dir; struct dirent *dir_structure, *proc_struct; FILE *fp; int pid, ppid; char *pname; char pstate; int i = 0; /*open directory stream*/ dir_pointer = opendir("/proc/"); printf("openning /proc/ dir\n"); char *digit_dir; //all process id char **subdir; //subdirectory for the processes subdir = (char**) calloc(sizeof(char**), 5000); //store at least 5000 processes int j = 0; /*reading reading files and directory*/ while((dir_structure = readdir(dir_pointer))) { i++; digit_dir = (char*) (dir_structure->d_name); //store only directory that are process ID if(isdigit(dir_structure->d_name[0])){ subdir[j]= (char*) dir_structure->d_name; j++; } } char *root = "/proc/"; char *file; char *new; printf("pid \t pname \t \t \t pstate \t ppid\n"); while(*(subdir)!=NULL){ new = (char *)calloc(strlen(root) + strlen(*subdir) + 1, sizeof(char)); strcat(new, root); strcat(new, *subdir); file = (char *)calloc(strlen(new) + strlen("stats") + 1, sizeof(char)); strcat(file, new); strcat(file, "/stat"); fp = fopen(file,"r"); if (fp == NULL) perror ("Error opening file"); else { pname = (char*) calloc(sizeof(char*),10); fscanf(fp, "%d", &pid); fscanf(fp, "%s", pname); fscanf(fp, "%c", &pstate); fscanf(fp, "%d", &ppid); printf("%d\t%s\t\t%c\t\t\t\t%d\n",pid,pname,pstate,ppid); fclose (fp); } *(subdir++); } return 0; }
Syntax Highlighting
[
Open in new window
]
Author Comments
code reading process state information from proc
Rating
4.63 / 8
68 Votes
http://codebin.yi.org/467
page generated in 0.00 seconds