Timer in Linux: Alarm () and setitimer ()

Source: Internet
Author: User

There are two types of timers in Linux:

1. Alarm
Alarm () and signal () are enough if they are not required to be accurate.
Unsigned int alarm (unsigned int seconds)

Function Description: Alarm () is used to set the signal sigalrm to be transmitted to the current process after the number of seconds specified by the seconds parameter. If the seconds parameter is 0, the previously set alarm is canceled and the remaining time is returned.
Return Value: returns the remaining seconds of the previous alarm. If no alarm is set, 0 is returned.

After alarm () is executed, the process continues to be executed. In the later stage (after alarm), the process will receive the signal sigalrm and execute its processing function in seconds.

# Include
# Include
# Include

Void sigalrm_fn (INT sig)
{
Printf ("alarm! /N ");
Alarm (2 );
Return;
}

Int main (void)
{
Signal (sigalrm, sigalrm_fn );
Alarm (1 );
While (1) pause ();
}

2. setitimer ()
Int setitimer (INT which, const struct itimerval * value, struct itimerval * ovalue ));
Setitimer () is more powerful than alarm and supports three types of Timers:

Itimer_real: calculated based on the real time of the system. It sends the sigalrm signal.
Itimer_virtual:-calculate the time spent by the process in the user State, and it sends the sigvtalrm signal.
Itimer_prof: calculates the time spent by the process in the user and kernel modes, and sends the sigprof signal.

Setitimer () the first parameter which specifies the timer type (one of the above three); the second parameter is an instance of the structure itimerval; the third parameter can not be processed.
If setitimer () is called successfully, 0 is returned; otherwise,-1 is returned.

The following is a simple example of setitimer calling. In this example, a sigalrm is sent every second and a sigvtalrm signal is sent every 0.5 seconds:

# Include
# Include
# Include
# Include
# Include
# Include

Int sec;

Void sigroutine (INT signo)
{
Switch (signo ){
Case sigalrm:
Printf ("catch a signal -- sigalrm/N ");
Signal (sigalrm, sigroutine );
Break;
Case sigvtalrm:
Printf ("catch a signal -- sigvtalrm/N ");
Signal (sigvtalrm, sigroutine );
Break;
}
Return;
}

Int main ()
{
Struct itimerval value, ovalue, value2; // (1)
SEC = 5;
Printf ("process ID is % d/N", getpid ());
Signal (sigalrm, sigroutine );
Signal (sigvtalrm, sigroutine );

Value. it_value. TV _sec = 1;
Value. it_value. TV _usec = 0;
Value. it_interval. TV _sec = 1;
Value. it_interval. TV _usec = 0;

Setitimer (itimer_real, & Value, & ovalue); // (2)
Value2.it _ value. TV _sec = 0;
Value2.it _ value. TV _usec = 500000;
Value2.it _ interval. TV _sec = 0;
Value2.it _ interval. TV _usec = 500000;

Setitimer (itimer_virtual, & value2, & ovalue );

For (;;)
;
}

(1) struct itimerval

Struct itimerval {
Struct timeval it_interval;/* timer interval */
Struct timeval it_value;/* Current Value */
};

Itimerval: I --> Interval
Val --> Value

It_value in the itimerval structure is the time for reduction. When this value is 0, a corresponding signal is sent. Then, it_value is set to it_interval.

(2) setitimer ()
Setitimer () sets a timer for the process in which it is located, if itimerval. if the value of it_interval is not 0 (neither of the two it_interval fields is 0), the timer will remain valid (a signal will be sent every time)

Note: The Linux signal mechanism is basically inherited from UNIX systems. In early Unix systems, the signal mechanism was relatively simple and original, and some problems were exposed in practice. Therefore,
The signals established on the early mechanism are called "unreliable signals", and the signals whose signal values are smaller than sigrtmin (sigrtmin = 32, sigrtmax = 63) are unreliable.
. This is the source of "unreliable signal. The main problem is that each time a process processes a signal, it sets the response to the signal as the default action. In some cases, it may lead to incorrect signal processing; therefore,
If you do not want this operation, call signal () again at the end of the signal processing function to reinstall the signal.

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.