LinuxProc File System Experiment (code)

Source: Internet
Author: User
The proc directory on Linux is a file system called proc file system (virtual file system). It stores kernel status information, including cpu and memory

The/proc directory on Linux is a file system called proc file system (virtual file system). It stores kernel status information, including cpu, memory, and processes. The proc file system has many advantages: applications do not need to switch to the kernel state to obtain kernel data, which increases system security (for example, the ps command is to obtain process information through proc ); applications can directly change kernel parameters through proc, so that they can change and optimize kernel behavior without re-compiling the kernel. In short, proc provides a secure and convenient interface for your applications to obtain internal system information. Proc is in memory and does not occupy external memory.

The files in the/proc directory are as follows:

Apm Advanced Power Management Information
Cmdline Kernel command line
Cpuinfo Cpu information
Devices Available devices (block devices/character devices)
Dma Used DMS channels
Filesystems Supported file systems
Interrupts Interrupted use
Ioports I/O port usage
Kcore Core kernel impressions
Kmsg Kernel message
Ksyms Kernel symbol table
Loadavg Server load balancer
Locks Kernel lock
Meminfo Memory information
Misc Miscellaneous
Modules Load Module List
Mounts File system loaded
Partitions Partition Table recognized by the system
Rtc Real time clock
Slabinfo Slab pool info
Stat Comprehensive statistics Status Table
Swaps Utilization of swap space
Version Kernel version
Uptime Normal system running time

The following is a program that I wrote to view the cpu and kernel version information and startup time:

Because file read and write operations are involved, first introduce several file read and write functions.

1) fgetc reads a character from the stream and adds the position of the file pointer. it is often used with EOF to read all the characters in the stream.

2) putchar outputs a character to the terminal.

3) fgets (char * s, int size, FILE * stream) reads a row from the FILE stream to s. The size is generally the size of the array s, s ends with the character '/0.

4) int feof (FILE * stream) determines whether a FILE ends. If yes, a non-zero value is returned; otherwise, the value is 0.

5) int fscanf (FILE * stream, const char * format,...) formatted input from the FILE stream (or standard input), usage:

#include
 
  int main(){       int i;    char s[5];    fscanf(stdin, "%d %5[a-z]%*s", &i, s);    printf("%d %s \n", i, s);    return 0;    }
 
Execution result (enter 99 abcdefghijk in the command line. because fscanf encounters a space and ends with a line change, 99 is saved to I and abcde is saved to s ):

#include
 
  #include
  
   #define LB_SIZE 80enum TYPE{STANDARD, SHORT, LONG};FILE *thisProcFile; //Proc open file pointerstruct timeval now; //system time dateenum TYPE reportType; //the type of observation reportchar repTypeName[16];char *lineBuf; //read out bufferint interval; //system overload detect intervalint duration; //system overload detect durationint iteration;char c1, c2; //character handle uintvoid getTwoTime(FILE *fp) {long uptime, idletime;int day, hour,minute, second;int m, n;char temp[80];m = n = 0;int ret;int i = 0;char s[100][100];int j;while((ret=fscanf(fp, "%s", s[i])) != EOF) i++;//printfor(j = 0; j < i; j++){printf("%s\n", s[j]);}uptime = atol(s[0]);idletime = atol(s[1]);printf("<<---------------------___________------------------------->\n");printf("the uptime of system -----------------------------> %ld\n", uptime);printf("the idletime of process -----------------------------> %ld\n", idletime);printf("<<---------------------___________------------------------->\n");/*time_t uptime_timet = uptime;printf("xixixixixi%ld\n", uptime_timet);char *result = ctime(&uptime_timet); printf("hahahahahahah %s\n", result);*/int days;int hours;int minutes;int seconds;//uptime of systemdays = (int)uptime/86400;hours = (int)uptime%86400/3600;minutes = (int)uptime%3600/60;seconds = (int)uptime%3600%60;printf("the uptime of system-------------days %d----hours %d-----minutes %d-----seconds %d----\n", days, hours, minutes, seconds);//idletimedays = (int)idletime/86400;hours = (int)idletime%86400/3600;minutes = (int)idletime%3600/60;seconds = (int)idletime%3600%60;printf("the idletime of system-------------days %d----hours %d-----minutes %d-----seconds %d----\n", days, hours, minutes, seconds);}//get time from startingvoid sampleTime(){//open timer fileFILE *fp;if((fp=fopen("/proc/uptime", "r")) == NULL){printf("not open /proc/uptime");exit(0);}getTwoTime(fp);fclose(fp);}void getCPUinfo(){FILE *cpuinfo;char ch;if((cpuinfo=fopen("/proc/cpuinfo","r")) == NULL){printf("not open /proc/cpuinfo");exit(0);}/*while((ch=fgetc(cpuinfo)) != EOF)putchar(ch);fclose(cpuinfo);*/printf("*********************cpu*******************\n");while(!feof(cpuinfo)){fgets(lineBuf, LB_SIZE+1, cpuinfo);printf("%s", lineBuf);}fclose(cpuinfo);}void getKernelVersion() {FILE *version;char ch;if((version=fopen("/proc/version","r")) == NULL){printf("not open /proc/version");exit(0);}printf("*****************version*************************\n");while(!feof(version)){fgets(lineBuf, LB_SIZE+1, version);printf("%s", lineBuf);}fclose(version);}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);if(c1 != '-')  exit(1);if(c2 == 'b'){printf("********************************Memory Info*********************************\n");//open memory info FILE *meminfo; char ch;if((meminfo=fopen("/proc/meminfo","r")) == NULL){printf("not open /proc/meminfo");exit(0);}while((ch=fgetc(meminfo)) != EOF)putchar(ch);fclose(meminfo);printf("********************************TIME*********************************\n");//cat the start timesampleTime();} else if(c2 == 'c'){//get cpu info and kernel versionprintf("*************************CPU & kernel*************************\n");getCPUinfo();getKernelVersion();}}}
  
 
Run:

./Main-B view the memory information and formatted start time.

./Main-c view CPU type and kernel version.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.