Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include ... int main(int argc, char *argv[]) { ... char repTypeName[16]; ... /* Determine report type */ reportType = STANDARD; 4 strcpy(repTypeName, "Standard"); if (argc > 1) { sscanf(argv[1], "%c%c", &c1, &c2); if (c1 != -) { fprintf(stderr, "usage: observer [-s][-l int dur] "); exit(1); } if (c2 == s) { reportType = SHORT; strcpy(repTypeName, "Short"); } if (c2 == l) { reportType = LONG; strcpy(repTypeName, "Long"); interval = atoi(argv[2]); duration = atoi(argv[3]); } } ... } 3.3 OrganizingaSolution For Part C and D, your programs must have different arguments on the command line. Therefore, one of your rst actions should be to parse the command line with which the program is called so as to determine the shell parameters being passed to it via theargvarray. You nish initializing by getting the current time of day and printing a greeting that includes the name of the machine you are inspecting: #include ... FILE *thisProcFile; /* Finish initializing ... */ gettimeofday(&now, NULL); // Get the current time printf("Status report type %s at %s ", repTypeName, ctime(&(now.tv_sec))); // Get the host filename and print it thisProcFile = fopen (" /proc/sys/kernel/hostname", "r"); fgets(lineBuf, LB_SIZE+1, thisProcFile); printf("Machine hostname: %s", lineBuf); fclose(thisProcFile); Now you are ready to do the work, that is, to start reading kernel variables by using various /proc les. The previous code segment contains an example of how to read the /proc/svs/kernel/hostname le. You can useitasaprototypeforsolvingtheexercisebyreadingotherpseudoles. Thiswillrequiresomeexploration of the /proc and inspection of the various pseudo les as you investigate different directories. In Part D, you are to compute a load average. For this, your code needs to sleep for a while, wake up, sample the current load average, and then go back to sleep. Here is a code fragment that will accomplish that work. 5 #include #include /* some initialization ... */ struct timeval now; /* Finish initializing ... */ while (iteration < duration) { sleep(interval); sampleLoadAvg(); iteration += interval); } Now you are ready to create the entire solution: #include #include ... int main(int argc, char *argv[]) { ... char repTypeName[16]; ... /* Determine report type */ reportType = STANDARD; strcpy(repTypeName, "Standard"); if (argc > 1) { sscanf(argv[1], "%c%c", &c1, &c2); if (c1 != -) { fprintf(stderr, "usage: observer [-s][-l int dur] "); exit(l); } if (c2 == s) { reportType = SHORT; strcpy(repTypeName, "Short"); } if (c2 == l) { reportType = LONG; strcpy(repTypeName, "Long"); interval = atoi(argv[2]); duration = atoi(argv[3]); } } /* Finish initialization */ gettimeofday(&now, NULL); // Get the current time printf("Status report type %s at %s ", repTypeName, ctime(&(now.tv_sec))); /* Get the host filename and print it */ thisProcFile = fopen("/proc/sys/kernel/hostname", "r"); fgets(lineBuf, LB_SIZE+1, thisProcFile); printf("Machine hostname: %s", lineBuf); fclose(thsProcFile); /* Code to read the relevant /proc files */ ... 6 while (iteration < duration) { sleep(interval); sampleLoadAvg(); iteration += interval); } exit 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions

Question

Find the distance between the points. (1, 6, 3), (-2, 3, 5)

Answered: 1 week ago

Question

please dont use chat gpt AI 5 2 0 .

Answered: 1 week ago

Question

Conduct a needs assessment. page 269

Answered: 1 week ago