Observe Linux Behavior

Source: Internet
Author: User
Observe the Linux behavior-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1./proc file system

The/proc file system is a mechanism used by the kernel and kernel module to send information to the process (so it is called/proc ). This pseudo file system allows you to interact with the internal data structure of the kernel to obtain useful information about the process. on the fly, you can change the settings (by changing the kernel parameters ). Unlike other file systems,/proc exists in the memory rather than on the hard disk. If you view the file/proc/mounts (like the mount command to list all mounted file systems), you will see a line like this:

Grep proc/mounts
/Proc rw 0 0

/Proc is controlled by the kernel and there is no device carrying/proc. Because/proc stores state information controlled by the kernel, the logical location of most of this information is in the memory controlled by the kernel. If you perform 'LS-l' on/proc, you can see that most files are 0 bytes large. However, you can see some information when you view these files. How is this possible? This is because the/proc file system registers itself to the virtual file system layer (VFS) like other conventional file systems. However, the/proc file system will not establish the corresponding files and directories based on the information in the kernel until VFS calls it to request files and directories of I-nodes.

1) load the proc file system

If the proc file system is not loaded yet, run the following command to load the proc file system:

Mount-t proc/proc
The above command will successfully load your proc file system. For more details, see the man page of the mount command.

/Proc files can be used to access information about the Kernel Status, computer attributes, and the status of running processes. Most files and directories in/proc provide the latest information about the physical environment of the system. Although files in/proc are virtual, they can still be viewed using any file editor or programs such as 'more', 'less ', or 'cat. When the editing program tries to open a virtual file, the file is created out of thin air by the information in the kernel.

2) obtain useful system/kernel information
The proc file system can be used to collect useful information about the system and the running kernel. The following are some important documents:
*/Proc/cpuinfo-CPU information (model, family, cache size, etc)
*/Proc/meminfo-physical memory, swap space, and other information
*/Proc/mounts-List of mounted file systems
*/Proc/devices-list of available devices
*/Proc/filesystems-Supported file systems
*/Proc/modules-loaded modules
*/Proc/version-kernel version
*/Proc/cmdline-kernel command line parameters entered at system startup
In proc, there are more than the files listed above. Readers who want to learn more can get more information about every file in/proc or read references [1. We recommend that you use 'more' instead of 'cat' unless you know that this file is small because some files (such as kcore) may be very long.

3) Open, close, and read proc files

Files in/pro can be read like normal ASCII files. to read the content of a pseudo file, you can open the file and use the routines in the stdio Library such as fgets () or fsacnf () to read files. The following is an example of reading/proc/sys/kernel/hostname in the experiment:

FILE * thisProcFile = NULL;

ThisProcFile = fopen ("/proc/sys/kernel/hostname", "r ");

Fgets (lineBuf, LB_SIZE + 1, thisProcFile );

Printf ("Machine hostname: % s", lineBuf );

Fclose (thisProcFile );


2. Some linux functions and c functions and methods used in the experiment

1) linux functions-time () and ctime ()

To output the current system time, you can use the following two functions:

# Innclude

Time_t time (time_t * tloc );

Char * ctime (const time_t * clock );


The time function returns the number of seconds since, January 1, January 1, 1970. It is stored in the time_t structure. However, this function returns

The return value has no practical significance for us. In this case, we use the second function to convert the number of seconds to a string.

. The return type of this function is fixed: A string with a possible value of. Thu Dec 7 14:58:59 2006

The length is fixed to 26.

2) argc and argv in C Language

A c program may have the following headers:

Int main (int argc, char * argv [])

If you do not pass parameters to shell, you can omit these two parameters,

Argc indicates the number of input file names and parameters. argv [0] indicates the file name argv []. Otherwise, it indicates the parameter, such as observer? L 10 600

Argc = 4, argv [0] = "observer", argv [1] = "-l "......

The c main program can reference these parameters as follows:

Int main (int argc, char * argv [])

// Determine the report type

ReportType = STANDARD;

Strcpy (repTypeName, "Standard ");

If (argc> 1)

{

Sscanf (argv [1], "% c", & c1, & c2 );

}

If (c1! = '-')

{

Fprintf (stderr, "usage: observer [-s] [-l int dur] \ n ");

Exit (1 );

}

If (c2 ='s ')

{

ReportType = SHORT;

Strcpy (repTypeName, "Short ");

}

If (c2 = 'l ')

{

ReportType = LONG;

Strcpy (repTypeName, "Long ");

Interval = atoi (argv [2]);

Duration = atoi (argv [3]);

}

3) Some functions in c

<1> conversion functions

Atoi function: converts a string to an integer.

Related functions: atoi, atol, strtodd, strtol, strtoul

Header file # include

Define the function int atof (const char * nptr );

<2> Input and Output Functions

-Printf (), sprintf (), sscanf (), gets (), getch (), scanf (), puts ()

<3> file operation functions

-Fopen (), fclose (), fgets (), fputs (), fscanf (), fprintf (), fgetc (), fputc (), freal (), fwrite ()

Conclusion:

The/proc file system provides a file-based internal Linux interface. It can be used to determine the status of various devices and processes of the system.

The C function library and the linux function library define many functions related to various operations. They can be used to read and modify/proc.

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.