first, the introduction
Proc file system, in the form of file system to provide users with the current state of the system, dynamically read the necessary information from the same kernel, only in memory, does not occupy the external space. Ii. content of the experiment
Question A:
1. CPU Type
2, kernel version
Question B:
1, the time since the start of the system to DD:HH:MM:SS report
Question C:
1, CPU execution User state, System State, idle state time
2, how many times the disk request
3, how many times the context switch
4. How many times has the process been started
Question d:
1. Total Memory
2, available memory
3, the system average load Three, experiment code
/***************************************** * * Proc File System * * Copyright: (C) 2018.3.31 by Shaomingshan * * Compile:gcc-o Main MAIN.C * Execute:./main-a * *****************************************/#include <stdio.h> #include <sys/ time.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #define Lb_size enum Type{sta
Ndard,short,long}; FILE *thisprocfile; Proc Open the file pointer struct timeval now; System time date enum TYPE reporttype;
Observation report type char reptypename[16]; Char *linebuf; Proc file read trip buffer int interval; System load monitoring time interval int duration;
System load monitoring period int iteration; Char c1,c2;
Character Processing unit void Sampleloadavg () {//Observation system load int i=0;
Open load File if ((Thisprocfile = fopen ("/proc/loadavg", "r") = = NULL) {printf ("Open failed\n");
Return
//read out, process read travel, such as removing leading blanks and useless spaces fgets (linebuf, lb_size+1, thisprocfile);
char * C3 = Strtok (Linebuf, ""); Separate the readings from the different fields, and treat them as readable format//print-Processed information content while (C3!= NULL) {i ++;
switch (i) {case 1:printf ("average load within 1 minutes:");
Break
Case 2:printf ("average load within 5 minutes:");
Break
Case 3:printf ("average load within 15 minutes:");
Break
Case 4:printf ("Activity process ratio:");
Break
Case 5:printf ("Recent process number:");
Break
printf ("%s\n", C3);
C3 = Strtok (NULL, "");
} fclose (Thisprocfile);
} void Sampletime () {//Observing System boot time long uptime,idletime;
int Day,hour,minute,second;
int i,j;
Char temp[80];
i=j=0;
Open the timing file/read out, process read travel, such as removing leading blanks and useless spaces thisprocfile = fopen ("/proc/uptime", "R");
Fgets (Linebuf, lb_size+1, thisprocfile);
Separate the readings from the different fields, and process them in a readable format according to the different meanings of the fields//convert the number of seconds from the start time to a long integer uptime = Atol (Strtok (Linebuf, ""));
Convert own clock seconds day = uptime/3600/24;
hour = uptime/3600%24;
minute = uptime/60%60; Second = UpTime%60;
Print the processed information content printf ("Uptime:%2ldd:%2ldh:%2ldm:%2lds\n", day, hour, minute, second);
Converts the idle seconds of the start time to a long integer idletime = Atol (Strtok (NULL, ""));
Convert own clock seconds day = idletime/3600/24;
hour = idletime/3600%24;
minute = idletime/60%60;
second = idletime%60;
Print the processed information content printf ("Idletime:%2ldd:%2ldh:%2ldm:%2lds\n", day, hour, minute, second);
int main (int argc,char *argv[]) {linebuf = (char *) malloc (lb_size+1);
Reporttype = STANDARD;
strcpy (Reptypename, "Standard"); if (argc >1) {sscanf (argv[1),%c%c, &C1,&C2);//Fetch command-line selector if (c1!= '-') {//Prompt for the use of this procedure command parameter E
XIT (1);
} if (C2 = = ' a ') {//observation section A printf ("**********part a **********\n");
Reporttype = short;
strcpy (Reptypename, "short"); Take out and display the system current time/read out and display machine name/read out and display all CPU information/read out and display system version information Thisprocfile = FOP En("/proc/cpuinfo", "R");
while (!feof (Thisprocfile)) {fgets (Linebuf, lb_size+1, thisprocfile);
if (Strstr (LINEBUF, "model name")) {printf ("%s", linebuf);
Break
} fclose (Thisprocfile);
System ("Uname-r");
else if (C2 = = ' B ') {//Observation Part B printf ("**********part b **********\n");
Open the Memory information file//read out the contents of the file//process and display//Observe the system start time sampletime () in a convenient reading format;
else if (C2 = = ' C ') {//observation section C printf ("**********part c**********\n");
Open the System state information file Thisprocfile = fopen ("/proc/stat", "R");
Read all the contents of the file//process and display fgets (Linebuf, lb_size+1, Thisprocfile) in a convenient reading format;
while (!feof (Thisprocfile)) {Char *output = strtok (Linebuf, ""); if (!strcmpPut, "CPU")) {int i = 0;
while (I < 4) {output = Strtok (NULL, "");
switch (i) {case 0:printf ("User state time:%s\n", output);
Break
Case 1:break;
Case 2:printf ("System State Time:%s\n", output);
Break
Case 3:printf ("Idle Time:%s\n", output);
Break
} i++;
} else if (!strcmp (output, "Intr")) {output = Strtok (NULL, "");
printf ("Disk Request:%s\n", output);
else if (!strcmp (output, "Ctxt")) {output = Strtok (NULL, ""); printf ("Context Switch:%s\n", output);
else if (!strcmp (output, "processes")) {output = Strtok (NULL, "");
printf ("Number of startup processes:%s\n", output);
} fgets (Linebuf, lb_size+1, thisprocfile);
} fclose (Thisprocfile);
else if (C2 = = ' d ') {//observation section D printf ("**********part d **********\n");
if (argc<4) {printf ("usage:observer [-b] [-c][-d int dur]\n");
Exit (1);
} Thisprocfile = fopen ("/proc/meminfo", "R");
int i = 0;
Memory information file while (i++ < 3) {fgets (Linebuf, lb_size+1, thisprocfile);
Strtok (Linebuf, "");
Char *output = strtok (NULL, "");
switch (i) {case 1:printf ("Total Memory:");
Break Case 2:printf ("Free Memory:");
Break
Case 3:printf ("Available Memory:");
printf ("%s\n", output);
} fclose (Thisprocfile);
Reporttype = LONG;
strcpy (Reptypename, "Long");
Consecutive interval = ATOL (argv[2]) with time and time intervals specified with command line arguments;
Duration = Atol (argv[3]);
read out the contents of the system load file in a convenient reading format for (int i = 0; i < Duration/interval; ++i) {sampleloadavg ();
System Load Information sleep (interval);
}
}
}
}
iv. Results of Operation
If there is a mistake, please correct me