Question
Need Help please, my program does not work 2.2 PartB WriteadefaultversionofprogramtoreportthebehavioroftheLinuxkernelbyinspectingkernelstate. The program should print the following values to the user screen or console: CPU
Need Help please, my program does not work
2.2 PartB WriteadefaultversionofprogramtoreportthebehavioroftheLinuxkernelbyinspectingkernelstate. The program should print the following values to the user screen or console: CPU type and model Kernel version Amount of time since the system was last booted, in the form dd:hh:mm:ss (for example, 3 days 13 hours 46 minutes 32 seconds would be formatted as 03:13:46:32) report date (gettimeofday) and machine hostname 2.3 PartC Write a second version of the program in Part B that prints the same information as the default version plus the following: The amount of time the CPU has spent in user mode, in system mode, and idle The number of disk requests made on the system The number of context switches that the kernel has performed The time when the system was last booted The number of processes that have been created since the system was booted 2 2.4 PartD Extend the program again so that it also prints the following: The amount of memory congured into this computer The amount of memory currently available A list of load averages (each averaged over the last minute) Thisinformationwillallowanotherprogramtoplotthesevaluesagainsttimesothatausercansee how the load average varied over some time interval. Allow the user to specify how the load average is sampled. To do this you will need two additional parameters: One to indicate how often the load average should be read from the kernel One to indicate the time interval over which the load average should be read Therstversionofyourprogrammightbecalledby observer andthesecondversionby observer -s. Thenthethirdversioncouldbecalledbyobserver -l 2 60,wherebytheloadaverageobservation would run for 60 seconds, sampling the kernel table about once every 2 seconds. To observe the load on the system you need to ensure that the computer is doing some work rather than simply running your program. For example, open and close windows, move windows around, and even run some programs in other windows. 3 AttackingtheProblem Linux,Solaris,andotherversionsofUNIXprovideaveryusefulmechanismforinspectingthekernelstate, called the /proc le system. This is the key mechanism that you can use to do this exercise. 3.1 The/procFileSystem The /proc le system is an OS mechanism whose interface appears as a directory in the conventional UNIX le system (in the root directory). You can change to /proc just as you change to any other directory. For example, bash$ cd /proc makes /proc the current directory. Once you have made /proc the current directory, you can list its contents by using the ls command. The contents appear to be ordinary les and directories. However, a le in /proc or one of its subdirectories is actually a program that reads kernel variables and reports them as ASCII strings. Some of these routines read the kernel tables only when the pseudo le is opened, whereas othersreadthetableseachtimethattheleisread. Thusthevariousreadfunctionsmightbehavedifferently than you expect, since they are not really operating on les at all. The/procimplementationprovidedwithLinuxcanreadmanydifferentkerneltables. Severaldirectories as well as les are contained in /proc. Each le reads one or more kernel variables, and the subdirectories withnumericnamescontainmorepseudolestoreadinformationabouttheprocesswhoseprocessIDisthe same as the directory name. The directory self contains process-specic information for the process that is using /proc. To read /proc pseudo les contents, you open the le and then use the stdio library routines such as fgets() or fscanf() to read the le. The exact contents of the /proc directory tree vary among different Linux versions, so look at the documentation in the proc man page: 3 bash$ man proc Filesin/procarereadjustlikeordinaryASCIIles. Forexample,whenyoutypetotheshellacommand such as cat /proc/version you will get a message printed to stdout that resembles the following: Linux version 2.2.12 (gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)) #1 Mon Sep 27 10:40:35 EDT 1999 3.2 Usingargcandargv IfyoudoPartCorDinadditiontoPartB,youwillneedtopassparameterstoyourprogramfromtheshell. For example, suppose that your solution program is named observer. To solve Part B, you could call it with $ observer whereas to solve Part C or D, you could call with $ observer -s If the program is providing information required for Part D, it might be called by $ observer -l 10 600 computing information once every 10 seconds until 600 seconds have elapsed. Thefollowingcodesegmentisanexampleforhandlingthethreedifferentwaysthat observer canbe called. A C program may have the header le of the form: int main(int argc, char *argv[]) You may omit the two arguments, argc and argv if no parameters are to be passed to the program by the shell. Alternately, they can be initialized so that argc is the number of symbols on the command line and argv is an array of pointers to character strings symbols on the command line. For example, if the observer program is called with no parameters, then argc will be set to 1 and argv[0] will point to the string observer. In the second example, argc will be set to 2, with argv[0] pointing to the string observer andargv[1]pointing to the string -s. The C main program can now reference these arguments as follows: #include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started