Linux programming Timer

Source: Internet
Author: User

Create a Timer:

Int timer_create (clockid_t clock_id, struct sigevent * EVP, timer_t * timerid)

A process can call timer_create () to create a specific timer, which is owned by each process rather than inherited during fork. Clock_id indicates the clock on which the timer is based. * timerid loads the ID of the created timer. This function creates a timer and places its ID in the position pointed to by timerid. The EVP parameter specifies the asynchronous notification to be generated when the timer expires. If EVP is null, a default signal is generated when the timer expires. For clock_realtimer, the default signal is sigalrm. To generate other signals except the default signal, the program must set EVP-> sigev_signo as the expected signal code. The member EVP-> sigev_notify In the struct sigevent Structure describes the actions that should be taken when the timer expires. Generally, the value of this Member is sigev_signal, which indicates that a signal is generated when the timer expires. The program can set the member EVP-> sigev_notify to sigev_none to prevent signals generated when the timer expires.

 


If several timers generate the same signal, the handler can use EVP-> sigev_value to identify which timer generates the signal. To implement this function, the program must use the flag sa_siginfo in sa_flags of struct sigaction when installing the handler for the signal.

 

The value of clock_id is as follows:

Clock_realtime: systemwide realtime clock.

Clock_monotonic: represents monotonic time. cannot be set.

Clock_process_cputime_id: High Resolution per-process timer.

Clock_thread_cputime_id: thread-specific timer.

Clock_realtime_hr: High Resolution version of clock_realtime.

Clock_monotonic_hr: High Resolution version of clock_monotonic.

 

Struct sigevent

{

Int sigev_notify; // Notification Type

Int sigev_signo; // Signal Number

Union sigval sigev_value; // signal value

Void (* sigev_policy_function) (Union sigval );

Pthread_attr_t * sigev_policy_attributes;

}

Union sigval

{

Int sival_int; // integer value

Void * sival_ptr; // pointer Value

}

You can set EVP-> sigev_notify to the following values to customize the timer behavior after expiration:

Sigev_none: Do nothing. Only timeout information is queried through timer_gettime and timer_getoverrun.

Sigev_signal: when the timer expires, the kernel sends the signal specified by sigev_signo to the process. In the signal processing program, si_value is set to sigev_value.

Sigev_thread: when the timer expires, the kernel will (within this process) create a thread with sigev_icationication_attributes as the thread attribute, and let it execute sigev_notify_function, and pass in sigev_value as a parameter.

 

Start a Timer:

The timer created by timer_create () is not started. You can use timer_settime () to associate it with an expiration time and the start clock period ().

Int timer_settime (timer_t timerid, int flags, const struct itimerspec * value, struct itimerspect * ovalue );

 

Struct itimespec {

Struct timespec it_interval;

Struct timespec it_value;

}; 

Like settimer (), it_value is used to specify the expiration time of the Current timer. When the timer expires, the value of it_value will be updated to the value of it_interval. If the value of it_interval is 0, the timer is not a time interval timer. Once the it_value expires, it will return to the idle state. The timespec structure provides a millisecond-level resolution:

Struct timespec {

Time_t TV _sec;

Long TV _nsec;

};

If the flags value is timer_abstime, the time value specified by the value is interpreted as the absolute value (the default Interpretation Method for this value is relative to the current time ). This modified behavior can avoid obtaining the current time, calculating the relative difference between "this time" and "expected future time", and creating competition conditions during timer startup.

If the value of ovalue is not null, the previous timer expiration time will be stored in the provided itimerspec. If the timer is not started, all the members of this structure are set to 0.

 

 

Obtain the remaining time of an active Timer:

Int timer_gettime (timer_t timerid, struct itimerspec * value );

 

Obtain the number of off-limit operations of a Timer:


A timer may expire, and the signal generated when the last timer expires is still suspended. In this case, one of the signals may be lost. This means that the timer exceeds the limit. The program can call timer_getoverrun to determine the number of times a specific timer has exceeded this limit. The timer overrun can only occur on the signal generated by the same timer. Signals generated by multiple timers, even those using the same clock and signal, will be queued and will not be lost.

Int timer_getoverrun (timer_t timerid );

When the execution is successful, timer_getoverrun () will return the number of times the timer expires between the initial expiration of the timer and the notification process (for example, through the signal) when the timer expires. For example, in our previous example, if a 1 ms timer runs for 10 ms, 9 is returned for this call. If the number of times of exceeding the limit is equal to or greater than delaytimer_max, the call returns delaytimer_max.

When the execution fails, this function returns-1 and sets errno to Val. This unique error indicates that timerid specifies an invalid timer.

 

Delete a Timer:

Int timer_delete (timer_t timerid );

A successful timer_delete () call will destroy the timer associated with timerid and return 0. When the execution fails, the call will return-1 and the errno will be set to Val. This unique error indicates that timerid is not a valid timer.

 

Example 1:

Void handle ()
{
Time_t T;
Char P [32];

Time (& T );
Strftime (p, sizeof (P), "% t", localtime (& T ));

Printf ("time is % s/n", P );
}

 

Int main ()
{
Struct sigevent EVP;
Struct itimerspec ts;
Timer_t timer;
Int ret;

EVP. sigev_value.sival_ptr = & timer;
EVP. sigev_notify = sigev_signal;
EVP. sigev_signo = SIGUSR1;
Signal (SIGUSR1, handle );

Ret = timer_create (clock_realtime, & EVP, & timer );
If (RET)
Perror ("timer_create ");

TS. it_interval. TV _sec = 1;
TS. it_interval. TV _nsec = 0;
TS. it_value. TV _sec = 3;
TS. it_value. TV _nsec = 0;

Ret = timer_settime (timer, 0, & TS, null );
If (RET)
Perror ("timer_settime ");

While (1 );
}

 

Example 2:

Void handle (Union sigval V)
{
Time_t T;
Char P [32];

Time (& T );
Strftime (p, sizeof (P), "% t", localtime (& T ));

Printf ("% s thread % lu, val = % d, signal captured./N", P, pthread_self (), V. sival_int );
Return;
}

 

Int main ()
{
Struct sigevent EVP;
Struct itimerspec ts;
Timer_t timer;
Int ret;

Memset (& EVP, 0, sizeof (EVP ));
EVP. sigev_value.sival_ptr = & timer;
EVP. sigev_notify = sigev_thread;
EVP. sigev_policy_function = handle;
EVP. sigev_value.sival_int = 3; // as the handle () parameter

Ret = timer_create (clock_realtime, & EVP, & timer );
If (RET)
Perror ("timer_create ");

TS. it_interval. TV _sec = 1;
TS. it_interval. TV _nsec = 0;
TS. it_value. TV _sec = 3;
TS. it_value. TV _nsec = 0;

Ret = timer_settime (timer, timer_abstime, & TS, null );
If (RET)
Perror ("timer_settime ");

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.