Linux timer usage (1)

Source: Internet
Author: User

The purpose of using a timer is nothing more than to periodically execute a task, or to execute a task at a specified time. To achieve this goal, there are generally two common effective methods. One is to use three timers in linux, and the other is to use sleep and the usleep function to sleep the process for a period of time. In fact, another method is to use gettimeofday, difftime and so on to calculate the time interval, and then execute a task when the time is reached, but this method is inefficient, so it is not commonly used.

First, let's take a look at the three internal timers provided by the linux operating system for each process.

ITIMER_REAL: for a specified time interval, this count is reduced according to the actual time. When the time interval is 0, the SIGALRM signal is sent.

ITIMER_VIRTUAL: for a given time interval, the count is reduced when the process is executed, and the SIGVTALRM signal is sent when the time interval is 0.

ITIMER_PROF: given a time interval, when the process is executed or the system is scheduled for the process, the count is reduced. When the time reaches, The SIGPROF signal is sent, which is combined with ITIMER_VIRTUAL, it is often used to calculate the system kernel time and user time.

The functions used include:

# Include

Int getitimer (int which, struct itimerval * value );

Int setitimer (int which, struct itimerval * newvalue, struct itimerval * oldvalue );

Strcut timeval

{

Long TV _sec;/* seconds */

Long TV _usec;/* microseconds */

};

Struct itimerval

{

Struct timeval it_interval;/* interval */

Struct timeval it_value;/* Current Time count */

};

It_interval is used to specify the time at which the task is executed, and it_value is used to save the time before the task is executed. For example, if you specify it_interval to 2 seconds (microsecond to 0), we set the it_value time to 2 seconds to 0 at the beginning, it_value is reduced to 1. After 1 second, it_value is reduced by 1 and becomes 0. At this time, a signal is sent to inform the user that the time is up and the task can be executed ), in addition, the system automatically resets the it_value time to the it_interval value, that is, 2 seconds, and then recalculates the value.

To help you understand this problem, let's look at an example:

# Include

# Include

# Include

# Include

# Include

Static char msg [] = "time is running out \ n ";

Static int len;

// Output a standard error message to indicate that the time has elapsed

Void prompt_info (int signo)

{

Write (STDERR_FILENO, msg, len );

}

// Establish a Signal Processing Mechanism

Void init_sigaction (void)

{

Struct sigaction tact;

/* Signal to the task to be executed. The processing function is prompt_info */

Tact. sa_handler = prompt_info;

Tact. sa_flags = 0;

/* Initialize the Signal Set */

Sigemptyset (& tact. sa_mask );

/* Set up a signal processing mechanism */

Sigaction (SIGALRM, & tact, NULL );


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.