Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux Proc File System Experiment     - The Linux kernel and AVL tree in red-black tree (Programming)

- Circular list of Java programming (Programming)

- Getting the Linux shell expr use (Programming)

- The difference between equals and == in Java (Programming)

- Memcached and Redis (Linux)

- SSH configuration under Linux (Linux)

- Linux initialization init systems - Systemd (Linux)

- Two programs HAProxy + Heartbeat (note, the secondary server vip, normal HAProxy boot method) (Server)

- Android memory optimization of the memory cache (Linux)

- MySQL view (Database)

- Java collections series (Programming)

- To configure Samba to share files with Windows under CentOS (Linux)

- Boost notes --Thread - problems encountered in the initial use on Ubuntu (Programming)

- To install the latest version of Shotwell 0.18 under Ubuntu (Linux)

- Ubuntu 14.04 Install WordPress on Nginx (Server)

- expdp / impdp use version parameter data migration across versions (Database)

- Linux host dual LAN transceiver package ARP problem (Linux)

- Java implementation chain store binary tree (Programming)

- jQuery update the content and method of use 3.0 (Programming)

- CentOS network configuration 7, and set the host name and IP-bound problems (Linux)

 
         
  Linux Proc File System Experiment
     
  Add Date : 2018-11-21      
         
         
         
  / Proc directory on a Linux file system called the proc file system (virtual file system), it is stored kernel state information, including cpu, memory and process information. proc file system has many advantages: application to get the kernel data without switching to kernel mode, increasing the security of the system (such as ps command is the process of obtaining information through proc); the application can directly change the kernel parameters through the proc, so without recompiling the kernel can change and optimize the behavior of the kernel. In short, proc obtain internal information system for the user application provides a safe, convenient interface. proc in memory, do not take external memory.

Here is the file / proc directory:

apm Advanced Power Management Information
cmdline kernel command line
Cpuinfo information about Cpu
Devices can be used in the device (block device / character devices)
Dma Used DMS channels
Filesystems Supported file systems
Interrupts interrupts use
Ioports I / O ports use
Kcore kernel core impression
Kmsg kernel messages
Ksyms kernel symbol table
Loadavg Load Balancing
Locks kernel lock
Meminfo memory information
Misc Miscellaneous
Modules loaded module list
Mounts mounted file system
Partitions identification system partition table
Rtc Real time clock
Slabinfo Slab pool info
Stat comprehensive statistics state table
Swap space utilization Swaps
Version kernel version
Uptime system uptime
Here is my own view cpu and write a kernel version information and the start time of the program:

Because the file to be involved in reading and writing, first introduce several documents read and write functions.

1) fgetc reads a character from the stream, and increase the location of the file pointer, often used in conjunction with the EOF, all the characters in the stream read out.

2) putchar outputs a character to the terminal.

3) fgets (char * s, int size, FILE * stream), reads a line from the file stream into s, size is generally the size of the array s and s with the character '/ 0' End.

4) int feof (FILE * stream) determine whether you are experiencing the end of the file, if they return a nonzero value, and 0 otherwise.

5) int fscanf (FILE * stream, const char * format, ...) from the file stream (or standard input) the input format, use the following:

#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;
}

Results of the (command-line input 99 abcdefghijk, since the end of the encounter fscanf spaces and line, to keep the 99 i, abcde to save s):

#include < stdio.h>
#include < sys / time.h>

#define LB_SIZE 80
enum TYPE {STANDARD, SHORT, LONG};
FILE * thisProcFile; // Proc open file pointer
struct timeval now; // system time date
enum TYPE reportType; // the type of observation report
char repTypeName [16];
char * lineBuf; // read out buffer
int interval; // system overload detect interval
int duration; // system overload detect duration
int iteration;
char c1, c2; // character handle uint

void 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 ++;
 
 // Print
 for (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 system
 days = (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);
 

 // Idletime
 days = (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 starting
void sampleTime ()
{
 // Open timer file
 FILE * 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 time
   sampleTime ();

  } Else if (c2 == 'c')
  {
   // Get cpu info and kernel version
   printf ( "************************* CPU & kernel ******************* ****** \ n ");
   
   getCPUinfo ();

   getKernelVersion ();
   
  }
 }
}

carried out:

./main -b view memory information and formatted start time.

./main -c view cpu type and kernel version.
     
         
         
         
  More:      
 
- Linux 6 install Oracle 11g (64bit) (Database)
- Ubuntu 12.04 LTS installation configuration JDK1.6.0_45 (Linux)
- CV: Linux command displays the progress of the run command (Linux)
- Hadoop 0.23 compile common errors (Server)
- MySQL + Corosync + Pacemaker + DRBD build highly available MySQL (Server)
- Setting Linux desktop environment, achieve HiDPI display support (Linux)
- C language files update in real time (Programming)
- Installation of Theano + CUDA under Ubuntu (Linux)
- Linux security settings Notes (Linux)
- Zabbix Agent (Server)
- 64-bit Windows Server 2012 R2 install Oracle 10g Second Edition (Database)
- Use mysqldump MySQL database backup - Linux Shell Scripting (Database)
- Through eight skills to let you become a super Linux end-user (Linux)
- Linux supports serial output method (Linux)
- Linux (CentOS) SSH login without password authentication (Linux)
- Windows 8.1 hard drive to install Ubuntu 14.04 dual system reference tutorials and multi-drive Precautions (Linux)
- How the program is executed (Programming)
- How to cool down your Ubuntu system (Linux)
- Ubuntu firewall installation and configuration (Linux)
- 7 extremely dangerous Linux commands (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.