Two Linux timers

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 <stdio. h>

# Include <time. h>

# Include <sys/time. h>

# Include <stdlib. h>

# Include <signal. h>

 

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 <stdio. h>

# Include <time. h>

# Include <sys/time. h>

# Include <stdlib. h>

# Include <signal. h>

 

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 (;;)

 

;

 

}

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.