All pastes #1592837 Raw Edit

Miscellany

public c v1 · immutable
#1592837 ·published 2009-10-04 17:55 UTC
rendered paste body
/* Name Chris Jackey * Date 10/5/09 * Class CPSC 410 */// FIRST - RENAME THIS FILE TO myps.c#include <stdio.h>#include <stdlib.h>#include <procfs.h>#include <sys/fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <unistd.h>#include <string.h>int main (void) {	DIR *directory; // pointer to the /proc directory.	struct dirent *dirItem;  // each directory item in /proc, these are all named after a system process id.	char fname[25]; // character array to hold the complete path to psinfo for each directory in /proc.	int fd; //  reuseable file descriptor to reference the open psinfo file.	struct psinfo ps; // a psinfo struct to hold the information read in from the psinfo file.	// If the /proc file system can be opened capture a reference to it and continue	// else exit cleanly.	if(opendir("/proc") == NULL){		exit(1);	}	else{		directory = opendir("/proc");	}		// You don't need the . and .. directories so bypass them.		// Pretty print out your headers for each required stat (PID, PPID, CPU, ISIZE, RSIZE, CMD)	printf("PID     PPID    CPU     ISIZE   RSIZE   CMD\n");		/* Using the correct loop type -- remember, for loops are for when you know the number of		 * iterations and while loops are for when you don't know the number but do know the stop		 * condition -- loop through the directory doing the following on each iteration:		 *		 * 1)	Set the fname to the complete path to psinfo [man sprintf].		 * 2)	open [man open(2)] the fname O_RDONLY, make sure to deal with error conditions if the file		 * 		cannot be opened.		 * 3)	If the file is successfully opened, read [man read(2)] the file into the ps struct - note,		 * 		this is the heavy lifter and the trickiest line in the program.		 * 4)	Once you have captured the psinfo in the ps struct then print out the variables of interest		 *		using the C accessor operator, aka, the dot operator (.).		 *		 *		 * NOTE: the read function you are looking for is:		 *		 *		ssize_t read(int fildes, void *buf, size_t nbyte);		 *		 * filedes is the file descriptor you captured when the open was successful.		 * void *buf is a pointer to the buffer to store the read data in.		 * size_t nbyte is the number of bytes to read from the file...read up on the sizeof function		 *		 * Your read function should look something like this		 * read(filedescriptor, an assignment to the address of your ps variable cast to a char *, the sizeof the struct psinfo)		 *		 */	readdir(directory);	readdir(directory);		while((dirItem = readdir(directory)) != NULL){		sprintf(fname, "/proc/%s/psinfo", dirItem->d_name);		fd = open(fname, O_RDONLY);		if(fd != -1){			read(fd, &ps, sizeof(struct psinfo));			char isize[8];			sprintf(isize, "%d", ps.pr_size);			strcat(isize, "K");					char rsize[8];			sprintf(rsize, "%d", ps.pr_rssize);			strcat(rsize, "K");			char cpu[8];			sprintf(cpu, "%.2f", (ps.pr_pctcpu / (float)0x800) * 100);			strcat(cpu, "%");					printf("%-8u%-8u%-8s%-8s%-8s%s\n", ps.pr_pid, ps.pr_ppid, cpu,				isize, rsize, ps.pr_psargs);				}		close(fd);	}	// Properly close the directory	exit(1); //  and exit the program.}