Linux timer settings

Source: Internet
Author: User

10.5.2 proficient in timer settings

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/itimer.h>int getitimer(int which, struct itimerval *value);int setitimer(int which, const struct itimerval *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, as shown in table 10-6:

Table 10-6 parameter which and timer type

Value

Description

Signal sending

Itimer_real

Specifies the actual time, which is of the same type as alarm.

Sigalrm

Itimer_virt

The actual execution time of the scheduled process in user mode.

Sigvtalrm

Itimer_prof

The scheduled process is in the user and core states.
The actual execution time.

Sigprof

The signals sent to the process are different 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 timeval it_interval;/* next scheduled value */struct timeval it_value;/* This scheduled value */}

The structure timeval describes a time that is precise to the subtle:

Struct timeval {long TV _sec;/* Second (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 it_interval to 3 seconds:

struct itimerval 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:

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, as shown in code 10-11:

Code 10-11 precise timer instance (section self/code/chapter10/time4.c)

# Include <sys/select. h> # include <sys/itimer. 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 ++ ); signal (sigprof, timefunc);/* capture timing signal */} void main () {struct itimerval value; 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 (sigprof, timefunc);/* capture timing signal */setitimer (itimer_prof, & Value, null);/* timed start */while (1 );}

Compile and run code 10-11:

# make time4cc -O -o time4 time4.c # ./time4ITIMER_PROF[0]ITIMER_PROF[1]ITIMER_PROF[2]ITIMER_PROF[3]

 

Related Article

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.