Linux under C Programming: Setitimer

Source: Internet
Author: User
Tags error handling include printf linux

Setitimer: Similar to alarm, but can be more precise control of the process. Which represents the timing method. Value and oldvalue are pointers to Itmerval. The Setitimer () call successfully returns 0, otherwise it returns-1.

Which value:

Itimer_real: It sends out SIGALRM signals in the real time of the system.

Itimer_virtual: Calculates the time that the process spends in the user state, it sends out the SIGVTALRM signal.

Itimer_prof: The process is computed in user-state and kernel-state time, and it sends out SIGPROF signals.

Itimerval is implemented in Linux2.6.39/include/linux/time.h.

struct Itimerval {     
         
struct timeval it_interval;/* timerinterval * struct timeval 
         
;/* It_value CurrentValue >};

The it_value in the itimerval structure is the reduced time, and the corresponding signal is emitted when the value is 0. Then set the It_value to the It_interval value.

Note:

The Linux signaling mechanism is essentially inherited from Unix systems. The signal mechanism in the early Unix system is simpler and primitive, and then some problems are exposed in practice, so the signals that are built on the early mechanism are called "unreliable signals", the signal value is less than Sigrtmin (sigrtmin=32,sigrtmax=63) Signals are unreliable signals. This is the source of "unreliable signals". The main problem is that the response to the signal is set to the default action each time the process processes the signal. In some cases, it will result in error handling of the signal, so if the user does not want to do so, then call signal () again at the end of the signal processing function and reinstall the signal.

Examples are as follows:

#include <stdio.h>     
#include <sys/time.h>     
#include <signal.h>     
void func (int signumber) ;     
void Main ()     
{     
     structitimerval value;     
     Signal (SIGALRM,&FUNC);     
         
     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,null);     
     while (1)     
         pause ();     
         
}     
         
The void func (int signumber)//signumber is the signal number that the system passes to this function     
{     
     switch (signumber)     
     {     
         CASESIGALRM:     
              printf ("getsigalrm\n");     
              Signal (SIGALRM,&FUNC);     
              break;     
         Default:     
              printf ("nothing\n");     
              break;     
     }     
}

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.