Linux timer settings

Source: Internet
Author: User

The timer set by the function alarm can only be accurate to seconds, while the following functions can theoretically be accurate to the subtle:

# Include <sys/select. h>

# Include <sys/time. h>

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

Int setitimer (INT which, const structitimerval * value, struct itimerval * ovalue );

The setitimer function provides three timers, which are independent of each other. If any timer is completed, a scheduled signal is sent to the process and the timer is automatically re-timed. The which parameter determines the timer type. The real time of the signal sending itimer_real is the same as the alarm type. Sigalrm itimer_virt: Specifies the actual execution time of the process in the user State. The actual execution time of the sigvtalrmitimer_prof scheduled process in the user and core states. The sigprof timers send different signals to the process when the timer is completed. The itimer_real class timer sends the sigalrm signal, the itimer_virt class timer sends the sigvtalrm signal, and the itimer_real class timer sends the sigprof signal.

The function alarm is essentially a low-precision, non-heavy itimer_real class timer. It can only be accurate to seconds, and each setting can only generate a timer. The timer set by the setitimer function is different. They can not only time the time to the subtle (theoretically), but also automatically cycle the time.

In a Unix process, the alarm and itimer_real timers cannot be used at the same time.

The structure itimerval describes the composition of the Timer:

Struct itimerval {

Struct Tim. it_interval;/* next scheduled value */

Struct Tim. it_value;/* set the value of this timer */

}

The structure Tim. describes a precise time to the subtle:

Struct Tim .{

Long TV _sec;/* seconds (1000000 microseconds )*/

Long TV _usec;/* subtle */

}

The setitimer function sets a timer. The parameter value points to an itimerval structure, which determines the set timer information. The structure member it_value specifies the first scheduled time, and the structure member it_interval specifies the next scheduled time. When the timer is working, reduce the time value of it_value to 0, send a signal, assign it_value to the value of it_interval, and start the timer again. If the value of it_value is set to 0, the timer is stopped. If the value of it_value is not 0 but the value of it_interval is 0, the timer is terminated after one timer. When the setitimer function is called successfully, 0 is returned. Otherwise,-1 is returned. If the parameter ovalue is not empty, the last timer status is returned. The getitimer function obtains the current timer status. The integer parameter which specifies the read timer type, and the parameter value returns the timer status. If the function is successfully called, 0 is returned. Otherwise,-1 is returned.

 

 

Example 1. Set a timer to generate a sigalrm signal every 2.5 seconds.

A: Assign the it_interval and it_value values of the itimerval structure to 2.5 seconds:

Struct itimerval value;

Value. it_value. TV _sec = 2;

Value. it_value. TV _usec = 500000;

Value. it_interval. TV _sec = 2;

Value. it_interval. TV _usec = 500000;

Setitimer (itimer_real, & Value, null );

The timer set by the setitimer function can be scheduled repeatedly without multiple calls.

Example 2. set a timer. The process sends a signal for the first time after being executed for 1 second in the user State. A signal is sent every 3 seconds after the process is executed in the user State.

A: Assign it_value to members of the itimerval structure to 1 second, and assign it_interval to 3 seconds to structitimerval value;

Value. it_value. TV _sec = 1;

Value. it_value. TV _usec = 0;

Value. it_interval. TV _sec = 3;

Value. it_interval. TV _usec = 0;

Setitimer (itimer_virt, & Value, null );

Example 3: cancel an itimer_prof class timer.

A: Assign the it_value values of the members of the itimerval structure to 0 seconds:

Struct itimerval value;

Value. it_value. TV _sec = 1;

Value. it_value. TV _usec = 0;

Setitimer (itimer_prof, & Value, null );

Example 4: Set a 1.5-second real-time timer, which is automatically canceled when only one signal is sent.

A: Assign it_value to members of the itimerval structure to 1.5 seconds, and assign it_interval to 0 seconds to the members: struct itimerval value;

Value. it_value. TV _sec = 1;

Value. it_value. TV _usec = 500000;

Value. it_interval. TV _sec = 0;

Value. it_interval. TV _usec = 0;

Setitimer (itimer_real, & Value, null );

 

Precise timer instance

An example of a precise timer is designed here. The process sends the timer signal sigprof every 1.5 seconds and prints the timer times when the signal is received. You can type ctrl_c or delete to end the program.

 

# Include <sys/select. h>

# Include <sys/time. h>

# Include <stdio. h>

# Include <unistd. h>

# Include <signal. h>

Int n = 0;

Void timefunc (INT sig)/* Scheduled event Code */

{

Fprintf (stderr, "itimer_prof [% d] \ n", N ++ );

}

Void main ()

{

Struct itimervalvalue;

Value. it_value. TV _sec = 1;/* Scheduled for 1.5 seconds */

Value. it_value. TV _usec = 500000;

Value. it_interval. TV _sec = 1;/* Scheduled for 1.5 seconds */value. it_interval. TV _usec = 500000;

Signal (sigalrm, timefunc);/* capture scheduled signals */

Setitimer (itimer_real, & Value, null);/* timed start */

While (1 );

}

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.