Overview.
This project is to write a C++ program named hostinfo that reports Linux kernel state information. Your program will create instances of several C++ classes (which you will also write), each designed to inspect a particular part of the kernel's state, and then use that instance to report the state of that part of the kernel.
Details.
Linux maintains its state information in a variety of data structures that are known as kernel variables. The declarations of these data structures are generally stored in /usr/src/linux/, mostly in the include directory.
To provide an easy-to-use interface to this state information, Linux provides a pseudo file system called /proc. /proc is a pseudo file system because the "files" in it are actually programs that read kernel variables and report their values as ASCII strings. Thus, a C++ program can "open" one of these "files" and read from it as if it were a text file. You should read the man pages for proc, as well as take time to explore the contents of the pseudo-files in /proc.
Your program should allow a user to type:
hostinfo [switches]with its subsequent behavior being determined by the switches the user provides:
hostinfo -i --memshould display the number of interrupts generated by the devices specified above, plus the memory statistics specified above. Similarly,
hostinfo -m --cpu -b --intshould display all of the required information. When given no or erroneous switches, your program should display an appropriate message indicating the correct usage of the hostinfo command.
To process these command-line switches, your main function will need to use its argc and argv parameters. If you have not used these before, see any good C or C++ book for information on how to do so.
To provide these services, your program must use four classes: CPUInspector, InterruptInspector, MemoryInspector, and BootStatsInspector, plus a fifth Inspector class in which you store attributes/operations common to the first four classes.
The constructor for each class should open the pseudo file containing the information it reports. Since each subclass will have to open a pseudo-file, the code to actually open the file and the ifstream to it should be stored in the superclass (i.e., Inspector) constructor.
Each class should provide a separate accessor method for each attribute it retrieves. For example, the BootInspector class should provide getElapsedTime(), getProcessesCreated(), and getContextSwitches() methods. Each method should return its result as a string.
Note that the values returned by some methods (e.g., MemoryInspector::getTotalMemory()) should be the same from invocation to invocation of the method, while the values returned by other methods (e.g., MemoryInspector::getFreeMemory()) may change from invocation to invocation. Your classes should take advantage of this, and only re-open the file containing a kernel variable's value if its value is one that can change during program execution. (Hint: read static values once, in the class constructor, and define an accessor method to report those values; report dynamic values by reading them anew from the pseudo-file; where feasible, let the subclasses inherit values and methods from the Inspector class).
Plan of Action.
0. Read up on argc and argv, and explore /proc using cd and cat.
1. Get a skeleton program working that correctly processes command-line switches, plus a Makefile to compile and link it. (Hint: Process the entire command-line before doing anything else, using the switches specified to set bool variables. Then use those bool variables to control your program.)
2. Design your Inspector class hierarchy, specifying which methods go where.
3. Build and test the Inspector superclass.
4. Build and test each subclass.
5. Modify your program to make use of your classes.
6. Thoroughly test your system.
7. Print and fill out the top part of the project project gradesheet. Then complete the rest of the turn-in requirements that are discussed below.
Feel free to discuss the project, the Unix manual pages, and the /proc pseudo files with myself or your classmates. You are not to look at the code of anyone else.
Your program should be reasonably efficient in terms of its use of both time and space. It should also be fully documented: with an opening (header)
Turn in: The project grade sheet (you need to fill out the top part), attached to a copy of each file you create for the project, plus a hard copy of a script file, in which you
Due date: Wed, Feb. 8, 11:59 p.m.