Linux system programming-setitimer functions

Source: Internet
Author: User

Function: In Linux system programming, Setitimer is a function that is used frequently to realize the function of delay and timing.

Header file: sys/time.h

Function Prototypes:

int setitimer (int  which,              conststruct itimerval *New_value,               struct itimerval *old_value);

Parameter meaning:

1, the which parameter is used to set the timer type, the optional value is

(1) Itimer_real: Set the timer to time the system is real, and send SIGALRM signal after running the specified time.

(2) Itimer_virtual: When the setup process executes in user space, the time count is reduced. Send SIGVTALRM signal after running a specified time

(3) Itimer_prof: When the setup process executes in kernel space, the time count is reduced. Sends a SIGPROF signal after the specified time has elapsed.

2, New_value parameters: used to set the timer timing time

3, Old_value parameters: Rarely used, often set to null. It is used to store the New_value value that was set when the last Setitimer call was made.

Itimerval Structural Body:

 struct   timeval{time_t tv_sec;  /*   s       */  suseconds_t tv_usec;  /*   microseconds (10^ ( -6))  */ };  struct   itimerval{ struct  timeval it_interval; /*   time to generate the timer signal again     */    struct  timeval it_value; /*   The first time a timer signal is generated  */ };  

SetTimer mechanism: First it_value in the which parameter setting countdown, when the It_value is zero will send a signal. The it_value is then reset to the value of It_interval. Finally restart the new round of timing, in this way the cycle continues.

Attention:

1, the value of It_value is 0, will not send a signal. So to be able to send a signal, It_value is worth more than 0.

2, the value of It_interval is 0, the timer will only send a signal, that is, only delay, can not be timed.

Demo Program:

1#include <stdio.h>2#include <sys/time.h>3#include <unistd.h>4#include <stdlib.h>5#include <string.h>6#include <signal.h>7 8 voidCall_back (intSig)9 {Ten     Static intCount =1; Oneprintf"call_back:%d\n", count++); A } -  - intMainvoid) the { - signal (SIGALRM, call_back); -     structitimerval New_value; -memset (&new_value,0,sizeof(New_value)); +  -New_value.it_interval.tv_sec =1;//Set the timer time to 1S +New_value.it_interval.tv_usec =0; ANew_value.it_value.tv_sec =2;//set the first timer time to 2S atNew_value.it_value.tv_usec =0; -  -     intret = Setitimer (itimer_real, &New_value, NULL); -     if(0!=ret) -     { -Perror ("Setitimer"); inExit (-1); -     } to GetChar (); +  -     return 0; the}

Operation Result:

Linux system programming-setitimer functions

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.