linux-進程線程-setitimer

1、

SYNOPSIS
       #include <sys/time.h>

       int getitimer(int which, struct itimerval *curr_value);
       int setitimer(int which, const struct itimerval *new_value,
                     struct itimerval *old_value);

DESCRIPTION
       These  system  calls provide access to interval timers, that is, timers that initially expire
       at some point in the future, and (optionally) at regular intervals after that.  When a  timer
       expires,  a signal is generated for the calling process, and the timer is reset to the speci‐
       fied interval (if the interval is nonzero).

       Three types of timers—specified via the which argument—are provided,  each  of  which  counts
       against a different clock and generates a different signal on timer expiration:

       ITIMER_REAL    This timer counts down in real (i.e., wall clock) time.  At each expiration, a
                      SIGALRM signal is generated.

       ITIMER_VIRTUAL This timer counts down against the user-mode CPU time consumed by the process.
                      (The  measurement  includes  CPU time consumed by all threads in the process.)
                      At each expiration, a SIGVTALRM signal is generated.

       ITIMER_PROF    This timer counts down against the total (i.e., both user and system) CPU time
                      consumed  by  the process.  (The measurement includes CPU time consumed by all
                      threads in the process.)  At each expiration, a SIGPROF signal is generated.

                      In conjunction with ITIMER_VIRTUAL, this timer can be used to profile user and
                      system CPU time consumed by the process.

2、

Timer values are defined by the following structures:

           struct itimerval {
               struct timeval it_interval; /* Interval for periodic timer */
               struct timeval it_value;    /* Time until next expiration */
           };

           struct timeval {
               time_t      tv_sec;         /* seconds */
               suseconds_t tv_usec;        /* microseconds */
           };

3、

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

int main()
{
struct itimerval myit = {{0,0},{3,0}};
setitimer(ITIMER_REAL,&myit,NULL);
while(1)
{
printf("1111111!\n");
sleep(1);

}
return 0;
}

4、實現了一個alarm功能

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章