Proc file system

Source: Internet
Author: User
Tags strtok

Proc file system

The proc file system is a virtual file system. It uses the man proc command to describe proc as a pseudo file system. It provides some kernel data structure interfaces. Generally, the file system is mounted to the/proc Directory, which is usually read-only, but some files also allow some kernel variables to be modified.

Here are some examples:

// Obtain CPU information cat/proc/couinfo

Here are some of my outputs:

// Get the memory information cat/proc/meminfo

The output is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxjb2RlIGNsYXNzPQ = "hljs mel"> There are many files in the proc file system that can obtain some useful information.
You can use man proc to understand the proc file system as a whole.

Based on my understanding of the proc file system, I use the C language to obtain some information in the current system, including:
| Question A |
| 1: CPU type |
| 2: kernel version |
| Question B |
| 1: system running time |
| Question C |
| 1: cpu status |
| 2: disk requests |
| 3: Number of context switches |
| 4: Total number of processes |
| Question D |
| 1: Total memory size |
| 2: available memory |
| 3: Average system load |

The following is my code:

/*************************************** * ***** Function: observe linux kernel behavior /*********************************** * ***********/# include  # Include  # Include  # Include  // FILE pointer, used to read some files in the proc FILE system * thisProcFile; // obtain the CPU type void CPUinfo () {char s [1000]; char gu [10] = "model name"; // open the file and read if (thisProcFile = fopen ("/proc/cpuinfo", "r ")) = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} while (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ":"); if (strncmp (s, gu, 10) = 0) {// ": "For the delimiter to separate the string temp = strtok (NULL,": "); printf (" cputype: % s \ n ", temp); fclose (thisProcFile ); return ;}}// obtain the kernel version void Kernel_version () {char s [1000]; if (thisProcFile = fopen ("/proc/version", "r ")) = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} printf (" kernel version: \ n "); while (! Feof (thisProcFile) {if (fgets (s, 100, thisProcFile )! = NULL) printf ("% s", s);} fclose (thisProcFile);} // obtain the system runtime void time_run () {char s [100]; if (thisProcFile = fopen ("/proc/uptime", "r") = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} if (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ""); int times = atoi (temp); // convert the time to int run_days = times/86400; int run_hours = (times % 86400)/3600; int run_minutes = (times % 3600)/60; int run_seconds = (times % 60); printf ("the system Runtime is: % d day % d hour % d minute % d Second \ n ", run_days, run_hours, run_minutes, run_seconds);} fclose (thisProcFile);} void sampleLoadAvg () {// observe system load char s [100]; if (thisProcFile = fopen ("/proc/loadavg "," R ") = NULL) {printf (" % s \ n "," Open file failed !! "); Exit (0);} fgets (s, 100, thisProcFile); fclose (thisProcFile); char * temp = strtok (s ,""); char * array [5]; int m = 0; while (temp! = NULL) {array [m] = temp; temp = strtok (NULL, ""); m ++;} printf ("Average number of processes in one minute: % s \ n ", array [0]); printf (" Average number of processes within five minutes: % s \ n ", array [1]); printf ("Average number of processes within 15 minutes: % s \ n", array [2]); printf ("Number of processes running/total number of processes: % s \ n ", array [3]); printf ("recently running process ID: % s \ n", array [4]); fclose (thisProcFile );} // obtain the CPU status void cpu_state () {char s [1000]; if (thisProcFile = fopen ("/proc/stat", "r") = NULL) {printf ("% s \ n", "Open file failed!! "); Exit (0);} if (fgets (s, 1000, thisProcFile )! = NULL) {char * temp = strtok (s, ""); char * array [11]; int m = 0; while (temp! = NULL) {array [m] = temp; temp = strtok (NULL, ""); m ++;} printf ("CPU time used to execute the user State: % s jiffies \ n ", array [1]); printf (" time used for CPU system state execution: % s jiffies \ n ", array [3]); printf ("CPU idle state execution time: % s jiffies \ n", array [4]) ;}fclose (thisProcFile );} // obtain the number of context switches void ctxt () {char s [1000]; char gu [4] = "ctxt "; if (thisProcFile = fopen ("/proc/stat", "r") = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} while (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ""); if (strncmp (s, gu, 4) = 0) {temp = strtok (NULL, ""); printf ("Number of context switches: % s \ n", temp); fclose (thisProcFile); return ;}}} // obtain the number of Created processes void processes () {char s [1000]; char gu [9] = "processes "; if (thisProcFile = fopen ("/proc/stat", "r") = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} while (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ""); if (strncmp (s, gu, 9) = 0) {temp = strtok (NULL, ""); printf ("the number of Created processes is % s \ n", temp); fclose (thisProcFile); return ;}}// obtain the total memory void MemTotal () {char s [100]; char gu [8] = "MemTotal"; if (thisProcFile = fopen ("/proc/meminfo", "r") = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} while (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ":"); if (strncmp (s, gu, 8) = 0) {temp = strtok (NULL ,": "); printf (" total memory: % s \ n ", temp); fclose (thisProcFile); return ;}}// obtain the active memory void MemFree () {char s [100]; char gu [7] = "MemFree"; if (thisProcFile = fopen ("/proc/meminfo", "r") = NULL) {printf ("% s \ n", "Open file failed !! "); Exit (0);} while (fgets (s, 100, thisProcFile )! = NULL) {char * temp = strtok (s, ":"); if (strncmp (s, gu, 7) = 0) {temp = strtok (NULL ,": "); printf (" available memory: % s \ n ", temp); fclose (thisProcFile); return ;}} int main (int argc, char * argv []) {// sampleLoadAvg (); load balancing // CPUinfo (); // CPU information // Kernel_version (); // kernel version information // time_run (); // obtain the system running time // cpu_state (); // obtain the CPU status // ctxt (); // processes (); // MemTotal (); // MemFree (); char in; printf ("========================================== ===\ n "); printf ("| enter the question to view: | \ n"); printf ("| question A | \ n"); printf ("| 1: CPU type | \ n "); printf (" | 2: kernel version | \ n "); printf (" | Problem B | \ n "); printf (" | 1: system running time | \ n "); printf (" | Problem C | \ n "); printf (" | 1: cpu status | \ n "); printf ("| 2: Number of disk requests | \ n"); printf ("| 3: Number of context switches | \ n"); printf ("| 4: total number of processes | \ n "); printf (" | problem D | \ n "); printf (" | 1: Total memory | \ n "); printf (" | 2: available memory | \ n "); printf (" | 3: Average system load | \ n "); printf ("========================================== ===\ n "); scanf ("% c", & in); if (in = 'A') {CPUinfo (); Kernel_version (); // kernel version information} else if (in = 'B') {time_run (); // obtain system runtime} else if (in = 'C ') {cpu_state (); // obtain the CPU status ctxt (); processes () ;}else if (in = 'D') {MemTotal (); // total memory MemFree (); // available memory sampleLoadAvg (); // average system load }}    

Below are some of my outputs:





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.