In-depth analysis of Linux kernel source code-Process Scheduling (1) (1)

Source: Internet
Author: User

1 Linux Time System

The most basic time unit of a computer is the clock cycle, such as command taking, command execution, and memory access. Time system is a very important part of computer systems, especially for Unix time-based systems. The main task of the Time System is to maintain the system time and prevent a process from occupying the CPU and other resources exclusively, that is, the scheduling of the driver process.

Clock hardware 1.1

Most PCs have two clock sources: RTC and OS. RTCReal Time Clock (Real-Time Clock) is also called a CMOS Clock. It is a chip on the PC motherboard. It is powered by a battery, and can maintain the date and Time even if the system is powered off. Because it is independent from the operating system, it is also called the hardware clock. It provides a timing standard for the entire computer and is the most primitive and bottommost clock data.

In Linux, only RTC is used to obtain the time and date. However, by acting on the/dev/rtc device file, the process is also allowed to program RTC. The system administrator can configure the clock by executing/sbin/clock system programs.

The OS clock is generated from the timing/counting chip on the PC motherboard and is controlled by the operating system. The basic unit of the OS clock is the counting cycle of the chip. At startup, the operating system obtains the time data in RTC to initialize the OS clock. Then, the OS clock is formed by counting down the chip. It is also called a counter. The OS clock is only valid at startup and fully controlled by the operating system. Therefore, it is also called a soft clock or a system clock. Next we will focus on the generation of OS clock.

The OS clock outputs a pulse signal and receives an interrupt signal from the interrupt controller, which triggers the clock interruption to be described later. The clock interruption Service Program maintains the normal operation of the OS clock.

1.2 clock operation mechanism

The relationship between RTC and OS clock is also called the clock operating mechanism of the operating system. Generally, RTC is the time reference of the OS clock. The operating system initializes the OS clock by reading RTC. After that, the two remain in sync and jointly maintain the system time. What does it mean to keep running synchronously? It means that information in RTC is refreshed or corrected at every fixed time during the operation of the operating system.

Figure 2 clock operation mechanism

We can see that RTC is at the bottom layer and provides the most primitive clock data. The OS clock is built on RTC. After initialization, it is completely controlled by the operating system and is out of relationship with RTC. The operating system provides all time-related services to applications through the OS clock.

1.3 Linux time reference

We have learned about RTC real-time clock, hardware clock, OS clock system clock, and soft clock ). The following describes the OS clock. OS clock is generated by the output pulse generated by the programmable timer/counter to trigger the interruption. The cycle of the output pulse is called a "clock tick ". The time in the computer is in the unit of clock tick. Every time the clock tick, the system time will increase by 1. The operating system can obtain other time formats in seconds or milliseconds based on the current number of tick answers.

The purpose of defining the "time benchmark" is to simplify the calculation, so that the time in the computer can be expressed as the number of clock tick answers starting from this time benchmark. "The time benchmark is defined by the operating system designer. For example, the DOS time benchmark is January 1, 1980, the Unix time benchmark is January 1, 1970 twelve o'clock A.M., And the Linux time benchmark is on January 1, January 1, 1970.

1.4 Linux Time System

The OS clock record time is generally referred to as the system time. The system time is in the unit of "clock tick answer", and the frequency of clock interruption determines the length of a clock tick answer. For example, there are 100 clock interruptions per second, the answer to a clock is 10 ms (10 ms). Correspondingly, the system time increases by 1 every 10 ms.

In Linux, the global variable jiffies is used to indicate the number of clock ticking times since the system was started. The definition in/kernel/time. c is as follows:

Unsigned long volatile jiffies

Based on jiffies, Linux provides the following time formats suitable for people's habits. The definitions in/include/linux/time. h are as follows:

Struct timespec {/* this is a highly accurate representation */

Long TV _sec;/* second )*/

Long TV _nsec;/* nanoseconds: one thousandth of a second nanosecond )*/

};

Struct timeval {/* Common precision */

Int TV _sec;/* seconds */

Int TV _usec;/* microseconds: microsecond per second in one million )*/

};

Struct timezone {/* Time Zone */

Int tz_minuteswest;/* time difference between Greenwich Mean Time and Western Region */

Int tz_dsttime;/* time correction method */

};

TV _sec indicates second), TV _usec indicates microsecond), and TV _nsec indicates nanosecond ). The purpose of defining tb_usec and TV _nsec is to apply different usage requirements. These two representations are selected for different scenarios based on the requirements for time accuracy. In addition, Linux also defines the time representation that is used to better meet the common habits: year, month, and day. However, all time applications are built on jiffies. In short, jiffies is produced by clock interruption!

2 clock interruption


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.