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<stdio.h>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<stdio.h>#include<sys/time.h>#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.