Linux application programming and network programming three Linux access system information

Source: Internet
Author: User
Tags local time string format

3.3.1. About the concept of time 3.3.1.1, GMT (1) GMT is Greenwich Mean Time, which is the local area of Greenwich. (2) What is the meaning of GMT time? "Use Greenwich local time as global international time" to describe the time of global events, to make it easy for everyone to remember (3) Generally for convenience, a country is uniformly using a local time.  3.3.1.2, UTC time (1) GMT time is used previously, using astronomy to test, in recent years more and more use of UTC atomic clock time. (2) About Beijing time, you can refer to: http://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089456.html 3.3.1.3, time-related parts of the computer (1) Point time and time. Segment time = point time-point time (2) Timer and real-time clock. Timer time is the time, the real-time clock (RTC) is a point of time related to a device. The introduction of time 3.3.2.1 and jiffies in the  3.3.2.linux system (1) jiffies is a global variable in the Linux kernel, which is used to record a value that takes "the time of the kernel" as the length of the unit. (2) When the kernel is configured, a cycle time is defined, in fact, the scheduling system of the Linux kernel is working with the time slice of the cycle. (3) The jiffies variable starts with a reference value, then the kernel will add 1 per cycle time jiffies, and then to any time of the system our current time is jiffies this variable is marked.  3.3.2.2, Linux system How to record time (1) The kernel will read the RTC hardware when booting up to get a time as the initial base time, The base time corresponds to a jiffies value (the base time is converted to jiffies value by subtracting 1970-01-01 00:00:00 +0000 (UTC) from this time period, and then converting the period to jiffies value). This jiffies value exists as the benchmark jiffies value when we turn on the boot. Then the system runs at the end of each clock beat will give jiffies this global variable plus 1, so the operating system uses jiffies this global variable to record the current time. When we need the current time point, we use jiffies this time point to calculate (the calculation method is to calculate the time period corresponding to this jiffies value, and then add 1970-01-01 00:00:00 +0000 (UTC) to get this point in time) (2) In fact, the operating system only reads the RTC at boot time, the entire departmentThe RTC is not functional during the operation of the system. The real purpose of the RTC is to save time between the OS's 2 boot cycles. (3) Understand the time and time together to understand. Jiffies this variable is recorded in fact a period of time (in fact, the current time and 1970-01-01 00:00:00 +0000 (UTC) The difference between this time) (4) A time cycle depends on the configuration of the operating system, modern Linux system is generally 10ms or 1ms. This time is actually the scheduling time, in the kernel with Hz to record and express. If HZ is defined as 1000, the clock beat is 1/hz, which is 1ms. These are used when learning to drive.  3.3.2.3, Linux time-related system calls (1) common time-related APIs and C library functions are 9: time/ctime/localtime/gmtime/mktime/asctime/strftime/ Gettimeofday/settimeofday There are 9: Time/ctime/localtime/gmtime/mktime/asctime/strftime/gettimeofday/settimeofday (2) The time system call returns the current moment in seconds, 1970-01-01 00:00:00 +0000 (UTC) in the past number of seconds. The time inside is the number of seconds to be converted with jiffies. Other functions basically work around time. (3) Gmtime and localtime will change the number of seconds that time gets into a struct TM structure. The difference is that Gmtime gets the international time, and LocalTime gets the time local (refers to the local time that corresponds to the time zone set by the computer on which you run the LocalTime function). Mktime is used to complete the conversion in the opposite direction (struct TM to time_t) (4) If you want to get the string format from the struct TM, you can use either Asctime or strftime. (If you want to get a string format from time_t time with CTime) (5) The time returned by Gettimeofday is expressed collectively by struct timeval and struct timezone, where timeval represents time, And timezone represents the time zone. The settimeofday is used to set the current time and timezone. (6) Summary: No matter which system is called, the resulting time is essentially a time (the time is ultimately calculated from the jiffies recorded in the kernel), except that different functions return time in different formats and with different precision.   3.3.3. Time-related API combat 13.3.3.1, when (1) times can get a current time distance standard starting time 1970-01-01 00:00:00 +0000 (UTC) How many seconds have passed   3.3.3.2, CTime (1) CTime can get an easy-to-observe string format for the current time from time_t. (2) CTime Advantage is very simple to use, can directly get the current time of the string format, direct printing to see. The disadvantage is that the CTime print time format is fixed and cannot be changed according to our ideas. (3) The experimental results show that the time taken by the CTime function takes into account the local time in the computer (the time zone setting in the computer)  3.3.3.3, Gmtime, and localtime (1) Gmtime acquired time: The year is the difference of 1970. The month is 0 for January, the hour is the 0 time zone in UTC time (Beijing is East 8, so Beijing time is 8) (2) Guess the only difference between localtime and gmtime is that localtime is the time base for hours that is set in the current computer, The rest of the same. The practice proves that our guesses are correct.   3.3.4. Time-related API combat 23.3.4.1, Mktime (1) is not mktime when reading from the OS, this mktime is used to set the time for the operating system. 3.3.4.2, Asctime (1) asctime get the current time in a fixed format string format, with the same effect as CTime. The difference is ctime from time_t, and asctime from the struct TM.  3.3.4.3, Strftime (1) asctime and CTime get the time strings are fixed format, can not user-defined format (2) If the user needs to customize the format of time, you need to use strftime. The series  3.3.4.4, Gettimeofday and Settimeofday (1), which is based on the time function, are acquired in seconds, with no more precise time than the second. (2) Sometimes our program wants to get very precise time (for example, in US), which can only be achieved by gettimeofday. Random numbers are used in   3.3.5.linux, random numbers and pseudo-random numbers (1) random numbers are randomly occurring, and there is no regular set of sequences. (2) True completely random numberThe column does not exist, it is just an ideal situation. We usually use random numbers to get a pseudo-random number sequence only through some algorithms. (3) We usually refer to random numbers, which basically mean pseudo-random numbers.  3.3.5.2, Linux random number correlation API (1) Multiple calls to the RAND function can return a pseudo-random number sequence # include <stdlib.h>int rand (void);(2) The Srand function is used to set the seed    3.3.5.3 of the pseudo-random sequence obtained by Rand, and the actual combat demo (1) simply using Rand repeatedly calls N times, you will get a pseudo-random number between 0-rand_max, if you need to adjust the range, A sequence of random numbers can be obtained and then calculated. (2) The simple use of Rand to the pseudo-random number sequence is defective, each execution program gets the same sequence of pseudo-random sequence, unable to get other sequences (3) because the algorithm in Rand is actually through a seed (seed, is actually a primitive parameter, int type), Rand's internal default is to use 1 as seed, the seed of certain algorithm is also certain, then each obtained pseudo-random sequence must be the same. (4) So to each execution of this program to obtain a different pseudo-random sequence, each time to give a different seed. Use the Srand function to set the seed.  3.3.5.4, Summary and description (1) Each time the program executes, a different seed is set with Srand, and then multiple calls to Rand get a pseudo-random sequence, so that a different pseudo-random sequence can be obtained each time. (2) General practice is to use the return value of the time function to do srand parameters.  3.3.5.5, get a real random number in a Linux system (1) The time of some randomly occurring events in the Linux System collection system (for example, a person moving a mouse, such as the operation and coordinates of a touchscreen) as random seeds to generate a random number sequence. code example: #include <stdio.h> #include <stdlib.h>int main (int argc, char **argv) {  int i = 0, val = 0;  if (argc! = 2)     {        printf ("Usage:%s num\n", argv[0]);        retur n-1;   }    PrinTF ("Rand_max =%d.\n", Rand_max);       //2147483647    Srand (atoi (argv[1));  &nbsp ;                            //through the external delivery of seed   &NBSP ; Srand ((unsigned) time (NULL));               //Srand to give the RAND function a changing seed   & nbsp for (i=0; i<20; i++)                            . nbsp    //Set the number of random numbers     {        val = rand ();        printf ("%d" , (val% 10000));               //generate random numbers from 0 to 1000    }    printf ("\ n");  &N Bsp return 0;}      3.3.6.proc File System introduction 3.3.6.1, OS-level debugging (1) Simple program Single Step Debug (2) complex program printf Print information Debug (3) Framework System logging Information Debugging (4) The dilemma of kernel debugging 3.3.6.2, proc virtual file system works (1) The Linux kernel is a very large, very complex, separate program, and debugging is very complicated for such a program. (2) A large project like kernel, it is very troublesome to add/change a function inside, becauseA feature added for you may affect other existing ones. (3) in the early kernel version, although debugging is troublesome, but the master can also rely on the ability of personal vulgarity to ride. But by the time it was about 2.4, the difficulty was very large. (4) In order to reduce the difficulty of kernel debugging and learning, kernel developers have added some attributes to the kernel to debug the kernel, proc file system is an attempt. (5) Proc File system idea is: Build a virtual file system in the kernel/proc, the kernel runtime will be the kernel of some key data structures in the/proc directory in the form of files, some of the specific files, This is equivalent to visualizing the data structures in the invisible kernel to the developers of the kernel. (6) The proc file system gives developers a way to debug the kernel: We watch/proc/xxx files in real time to see the values of specific data structures in the kernel. Before and after we add a new feature, we can see whether the impact of the new feature is right or wrong. (7) The file size of the proc directory is 0, because these files do not exist in the hard disk, he is not a real file, he is just an interface, when we go to read this file, in fact, the kernel is not to go to the hard disk to find this file, Instead, maps a data structure inside the kernel to be read and formatted as a string to return to us. So while we're looking at a file content string, like a normal file, we actually know that this content is real-time from the kernel's data structures, not the hard disk.  3.3.6.3, Common proc documents (1)/proc/cmdline (2)/proc/cpuinfo (3)/proc/devices (4)/proc/interrupts   3.3.7.proc file system using 3.3.7.1, cat to manually view the 3.3.7.2, the program can access the file IO 3.3.7.3, in the shell program with the Cat command in conjunction with regular expressions to obtain and process the kernel information 3.3.7.3, extension: SYS file system (1) SYS file system is essentially the same as the proc file system, is a virtual file system, both in the root directory (one is the/proc directory, the other is the/sys directory), so are not files on the hard disk, "are the data structure in the kernel visual interface." "(2) The difference is that the files in the/proc are read-only, but the files in the/sys are readable and writable. Reading a file in/sys is the value of the data structure in the kernel, and the file written in/sys is the value of the element that sets the data structure in the kernel. (3) The/proc file system was first started in history, and people wanted to debug the kernel with this technique. It's really useful when it's actually done,So many kernel developers go to the kernel to write the code to the/proc directory, and at the beginning of the kernel manager on the use of the proc directory has no experience and no unified planning, and then the result is proc inside the things are many and messy. (4) later felt proc content too much too messy lack of unified planning, and then added the SYS directory. SYS file system has been well planned and agreed upon at the outset, so there are rules for using the SYS catalog later.   Add: Sysfs is a file system used to export kernel objects to user space, through which the user space program can view, and even modify, the kernel data structure. The file system is based on the kernel data structure kobject, and the directory structure of the filesystem reflects the hierarchical structure of the relevant kernel data structure. Because Kobject is the basic structure of the device model, SYSFS also includes information about the device in the system, which provides the topology information of the system hardware. Because SYSFS provides a means of accessing and modifying kernel data structures, kernel modules can also export interfaces to user spaces for accessing and modifying the parameters of a module through the file system. After introducing SYSFS, the kernel interfaces to the user in a way that has the/proc file system, Sysfs file system, and the IOCTL command. Although the SYSFS information is from Kobject, the Kobject and SYSFS associations are not created automatically and must be kobject_add to add a kobject to Sysfs. Because the main part of the kernel that uses kobject is the hardware-related part, SYSFS contains the most important content is hardware-related content, including bus, device, driver and so on. SYSFS mount point is/sys                  

Three Linux access system information for Linux application programming and network programming

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.