Linux Process interval timer Itimer

Source: Internet
Author: User
Article Title: Linux system process interval timer Itimer. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The Interval Timer (itimer) refers to the Timer using the "interval" value (interval) as the timing method. When the Timer starts, the Interval value will be reduced. When the interval value is reduced to 0, we will say that the interval timer expires. Compared with the kernel dynamic timer mentioned in the previous section, the biggest difference between the two is that the timer timing method is different. The kernel timer is timed by its expiration time expires value. When the global variable jiffies value is greater than or equal to the expires value of the kernel dynamic timer, we say the kernel timer expires. The interval timer is actually timing through a decreasing counter. Although the two timers are different, they are also related to each other. If we reduce the interval counter of the interval timer by 1 for every clock cycle, in this case, the interval timer is actually the kernel dynamic timer (we will see that the real interval timer of the process is implemented through the kernel timer ).

The interval timer is mainly applied to user processes. Each Linux Process has three interrelated intervals timers. Their respective interval counters are defined in the task_struct structure of the process, as shown below (include/linux/sched. h ):

Struct task_struct {

......

Unsigned long it_real_value, it_prof_value, it_assist_value;

Unsigned long it_real_incr, it_prof_incr, it_assist_incr;

Struct timer_list real_timer;

......

}

(1) Real interval timer (ITIMER_REAL): After the interval timer is started, every tick of the timer reduces the interval counter by 1 regardless of whether the process is running or not. When the value is reduced to 0, the kernel sends a SIGALRM signal to the process. It_real_incr, a member of the Structure Type task_struct, indicates the initial value of the interval counter of the real interval timer, and it_real_value indicates the current value of the interval counter of the real interval timer. Because the interval timer is essentially the same as the kernel timer in the previous section, Linux uses the real_timer kernel dynamic timer embedded in the task_struct structure to implement the real interval timer ITIMER_REAL.

2) virtual interval timer ITIMER_VIRT: Also known as the user-state interval timer of the process. The it_virt_incr and it_virt_value members in the structure task_struct indicate the initial value and current value of the interval counter of the virtual interval timer, respectively. Both are measured in the number of tick times. After the virtual interval timer is started, the current it_interval _value of the interval counter can be reduced by 1 only when the process is running in the user State. When the value is reduced to 0, the kernel sends the SIGVTALRM signal (Virtual alarm clock signal) to the process and resets it_virt_value to the initial value it_virt_incr. For details, see the implementation of the do_it_virt () function in section 7.4.3.

(3) PROF interval timer ITIMER_PROF: The it_prof_value and it_prof_incr members in the task_struct structure of the process indicate the current value and initial value of the interval counter of the PROF interval timer respectively (both in the unit of clock tick ). When the PROF interval timer of a process is started, as long as the process is running, whether in the user or core state, every tick of the clock reduces the it_prof_value of the interval counter by 1. When the value is reduced to 0, the kernel sends a SIGPROF signal to the process and resets it_prof_value to the initial value it_prof_incr. For details, see the do_it_prof () function in section 7.4.3.

Linux defines the index identifier for the preceding three process interval timers in the include/linux/time. h header file, as shown below:

# Define ITIMER_REAL 0

# Define ITIMER_VIRTUAL 1

# Define ITIMER_PROF 2

7.7.1 Data Structure itimerval

Although the interval counter of the interval timer in the kernel is measured in the unit of the number of times the clock is ticking, it is obviously inconvenient for users to specify the initial values of the interval counter of the interval timer in the unit of clock ticking, because the unit of time used by users is second, millisecond, or microsecond. Therefore, Linux defines the data structure itimerval to allow users to specify the interval value of the timer in seconds or microseconds. The definition is as follows (include/linux/time. h ):

Struct itimerval {

Struct timeval it_interval;/* timer interval */

Struct timeval it_value;/* current value */

};

The it_interval Member indicates the initial value of the interval counter, while the it_value Member indicates the current value of the interval counter. The two members are all variables of the timeval structure type, so their accuracy can reach microseconds.

Mutual conversion between timeval and jiffies

Because the internal representation of the interval counter of the interval timer is different from that of the external representation, it is necessary to convert the timeval structure in microseconds and the jiffies in the unit of the number of clock ticking times. Therefore, Linux implements two functions in kernel/itimer. c to implement mutual conversion between them-the tvtojiffies () function and the jiffiestotv () function. Their source code is as follows:

Static unsigned long tvtojiffies (struct timeval * value)

{

Unsigned long sec = (unsigned) value-> TV _sec;

Unsigned long usec = (unsigned) value-> TV _usec;

If (sec> (ULONG_MAX/HZ ))

Return ULONG_MAX;

Usec + = 1000000/HZ-1;

Usec // = 1000000/HZ;

Return HZ * sec + usec;

}

Static void jiffiestotv (unsigned long jiffies, struct timeval * value)

{

Value-> TV _usec = (jiffies % HZ) * (1000000/HZ );

Value-> TV _sec = jiffies/HZ;

}

7.7.2 underlying operating mechanism of real interval timer ITIMER_REAL

The underlying operating mechanisms of the interval timer ITIMER_VIRT and ITIMER_PROF are implemented by the do_it_virt () and do_it_prof () functions respectively, so we will not repeat them here (see section 7.4.3 ).

Because the interval timer ITIMER_REAL is essentially no different from the kernel dynamic timer. Therefore, the kernel uses the kernel dynamic timer to implement the ITIMER_REAL interval timer of the process. Therefore, the task_struct structure sets up a member variable real_timer of the timer_list structure type. The function pointer function of the dynamic timer real_timer is always set to point to the function it_real_fn () by the initialization macro INIT_TASK of the task_struct structure (). (Include/linux/sched. h ):

# Define INIT_TASK (tsk)

......

Real_timer :{

Function: it_real_fn

}

......

}

The real_timer linked list element list and data members are always initialized by the process when they are created as null and the address of the task_struct structure of the process, as shown in the following (kernel/fork. c): int do_fork (......)

{

......

P-> it_real_value = p-> it_1__value = p-> it_prof_value = 0;

P-> it_real_incr = p-> it_1__incr = p-> it_prof_incr = 0;

Init_timer (& p-> real_timer );

P-> real_timer.data = (unsigned long) p;

......

}

[1] [2] [3] [4] Next page

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.