In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what is the process scheduling of Linux kernel source code?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Linux kernel source code process scheduling" article.
1 Linux time system
The most basic time unit of a computer is the clock cycle, such as fetching instructions, executing instructions, accessing memory and so on. Time system is a very important part of computer system, especially for Unix time-sharing system. The main task of the time system is to maintain system time and prevent a process from monopolizing CPU and other resources, that is, the scheduling of the driver process.
1.1 clock hardware
Most PCs have two clock sources, called RTC and OS (operating system) clocks. RTC (Real Time Clock, real-time clock), also known as CMOS clock, is a chip on the PC motherboard. It is powered by batteries and can maintain the date and time even if the system is powered off. Because it is independent of the operating system, it is also called hardware clock. It provides a timing standard for the whole computer and is the clock data of the most primitive layer.
Linux only uses RTC to get the time and date; however, by acting on the / dev/rtc device file, it also allows the process to program RTC. By executing the / sbin/clock system program, the system administrator can configure the clock.
The OS clock is generated by the timing / counting chip on the PC motherboard, and the work of the chip is controlled by the operating system. The basic unit of the OS clock is the counting cycle of the chip. When booting, the operating system takes the time data in RTC to initialize the OS clock, and then forms the OS clock through the downward count of the counting chip, which should be called a counter. The OS clock is only valid when powered on and is completely controlled by the operating system, so it is also known as the soft clock or system clock. Below we focus on the generation of OS clocks.
OS clock output pulse signal, connected to the interrupt controller to generate interrupt signal, trigger the clock interrupt to be discussed later, by the clock interrupt service program to maintain the normal operation of the OS clock.
1.2 clock operation mechanism
The relationship between RTC and OS clocks is often referred to as the clock operation mechanism of the operating system. Generally speaking, RTC is the time benchmark of the OS clock, and the operating system initializes the OS clock by reading RTC. After that, the two keep running synchronously and maintain the system time together. What does it mean to keep running synchronously? It means that during the operation of the operating system, the information in the RTC will be refreshed or corrected at regular intervals.
Fig. 2 clock operation mechanism
As we can see, RTC is in the * layer, providing the most raw clock data. The OS clock is built on RTC, and after initialization is completed, it will be completely controlled by the operating system and will be disconnected from RTC. The operating system provides all time-related services to the application through the OS clock.
1.3 Linux time benchmark
Above we learned about RTC (real-time clock, hardware clock) and OS clock (system clock, soft clock). Let's describe the OS clock in detail below. The OS clock is generated by an interrupt triggered by an output pulse generated by a programmable timing / counter. The period of the output pulse is called a clock tick. The time in the computer is measured by the clock tick, and each time the clock ticks, the system time is added by 1. The operating system can get other time formats in seconds or milliseconds according to the number of ticks of the current clock.
A "time benchmark" is defined to simplify calculation so that the time in the computer is simply expressed as the tick of the clock starting from this time benchmark. "the time benchmark is defined by the designer of the operating system. For example, the time benchmark for DOS is January 1, 1980, the time benchmark for Unix is 12:00 on January 1, 1970, and the time benchmark for Linux is 0 am on January 1, 1970.
1.4 Linux time system
The time recorded by the OS clock is commonly referred to as system time. System time is measured in "clock tick", and the frequency of clock interrupts determines the length of a clock tick. For example, if there are 100 clock interrupts per second, then a clock tick is 10 milliseconds (10ms), accordingly, the system time will increase by 1 per 10ms.
In Linux, the global variable jiffies is used to represent the number of clock ticks since the system is started. Defined in / kernel/time.c as follows:
Unsigned long volatile jiffies
Based on jiffies, Linux provides the following time format suitable for people's habits, which is defined in / include/linux/time.h as follows:
Struct timespec {/ * this is a very precise representation * /
Long tv_sec; / * second (second) * /
Long tv_nsec; / * nanosecond: 1/1000000000 seconds (nanosecond) * /
}
Struct timeval {/ * ordinary precision * /
Int tv_sec; / * seconds * /
Int tv_usec; / * microseconds: 1/1000000 seconds (microsecond) * /
}
Struct timezone {/ * time zone * /
Time difference between int tz_minuteswest;/* Greenwich mean time and the West * /
Int tz_dsttime;/* time correction method * /
}
Tv_sec stands for second, tv_usec for microsecond, and tv_nsec for nanosecond. The purpose of defining tb_usec and tv_nsec is to adapt to different usage requirements, and to choose these two representations according to the requirements of time accuracy in different situations. In addition, Linux defines time expressions that are more in line with popular habits: year, month, and day. But everything remains the same, and all time applications are based on jiffies. In short, jiffies is generated by clock interruptions!
2 clock interrupt
2.1 generation of clock interrupts
A "clock interrupt" is a particularly important interrupt because it motivates the activity of the entire operating system. The system uses clock interrupts to maintain system time and promote environment switching to ensure that all processes share CPU; using clock interrupts for accounting, supervising system work and determining future scheduling priorities. It can be said that the "clock interrupt" is the pulse of the entire operating system.
The physical generation of clock interrupts is shown in figure 3:
Figure 3 physical connection of 8253 and 8259A
The pulse signal is connected to pin 0 of the interrupt controller 8259A_1 and triggers a periodic interrupt. we call this interrupt the clock interrupt, the cycle of the clock interrupt, that is, the cycle of the pulse signal, which we call "tick-tock" or "tick". In essence, clock interrupt is only a periodic signal, which is completely hardware behavior. This signal triggers CPU to execute an interrupt service program, and we call this service program clock interrupt.
The whole process of realizing clock interrupt by 2.2.Linux
1. Functions related to clock interrupts
Let's take a look at the service program triggered by the clock interrupt, which is complex and distributed in different source files, including the following functions:
Clock interrupt program: timer_interrupt ()
Interrupt service general routine do_timer_interrupt ()
Clock function: do_timer ()
Interrupt the installer: setup_irq ()
Interrupt return function: ret_from_intr ()
The calling relationships of the first three functions are as follows:
Timer_interrupt ()
Do_timer_interrupt ()
Do_timer ()
(1) timer_interrupt ()
This function is called about once per 10ms. In fact, the timer_interrupt () function is a wrapper routine that doesn't really do much. The main statement of this function is to call the do_timer_interrupt () function.
(2) do_timer_interrupt ()
The do_timer_interrupt () function has two main tasks, one is to call do_timer (), and the other is to maintain the real-time clock (RTC, which is written back at regular intervals). In fact, the modern code is in / arch/i386/kernel/time.c. In order to highlight the theme, the author rewrites the following functions for readers to understand:
Static inline void do_timer_interrupt (int irq, void * dev_id, struct pt_regs * regs)
{
Do_timer (regs); / * call the clock function and equate the clock function with the clock interrupt * /
If (xtime.tv_sec > last_rtc_update + 660)
Update_RTC ()
/ * update the time information in RTC every 11 minutes to keep the OS clock synchronized with the RTC clock. 11 minutes is 660 seconds. Xtime.tv_sec is in seconds. Last_rtc_update records the value from the last RTC update * /
}
Where xtime is the timeval type mentioned earlier, which is a global variable.
(3) clock function do_timer () (in / kernel/sched.c)
Void do_timer (struct pt_regs * regs)
{
(* (unsigned long *) & jiffies) + +; / * update the system time, which guarantees atomicity of the jiffies operation * /
Update_process_times ()
+ + lost_ticks
If (! User_mode (regs))
+ + lost_ticks_system
Mark_bh (TIMER_BH)
If (tq_timer)
Mark_bh (TQUEUE_BH)
}
Among them, the update_process_times () function is related to process scheduling. As can be seen from the name of the function, it deals with variables related to the current process and time, for example, to update the current process's time slice counter counter, if counter
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.