Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1485125
  • 博文数量: 181
  • 博客积分: 3308
  • 博客等级: 中校
  • 技术积分: 2227
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-03 12:03
个人简介

我是zoro

文章分类

全部博文(181)

文章存档

2015年(1)

2013年(35)

2012年(39)

2011年(50)

2010年(56)

分类: LINUX

2010-11-21 22:42:29

NAME
       getitimer, setitimer - get or set value of an interval timer

SYNOPSIS
       #include

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

DESCRIPTION
       The  system  provides  each  process with three interval timers, each decrementing in a distinct time domain.  When any timer expires, a signal is
       sent to the process, and the timer (potentially) restarts.
    系统为进程提供三种类型的计时器,每一类以不同的时间域递减其值。当计时器到时,信号被发送给进程,之后计时器重启动。
       ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expiration.以系统真实的时间来计算,计时器到时后发送信号SIGALRM。

       ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.只以该进程在执行的时间来计算,计时器到时后发送信号SIGVTALRM。

       ITIMER_PROF    decrements both when the process executes and when the system is executing on behalf of the process.  Coupled with  ITIMER_VIRTUAL,
                      this  timer is usually used to profile the time spent by the application in user and kernel space.  SIGPROF is delivered upon expi‐
                      ration.

       Timer values are defined by the following structures:

           struct itimerval {
               struct timeval it_interval; /* next value间隔时间 */
               struct timeval it_value;    /* current value第一次发送信号时间 */
           };

           struct timeval {
               long tv_sec;                /* seconds */
               long tv_usec;               /* microseconds */
           };

       The function getitimer() fills the structure pointed to by curr_value with  the  current  setting  for  the  timer  specified  by  which  (one  of
       ITIMER_REAL,  ITIMER_VIRTUAL,  or ITIMER_PROF).  The element it_value is set to the amount of time remaining on the timer, or zero if the timer is
       disabled.  Similarly, it_interval is set to the reset value.

       The function setitimer() sets the specified timer to the value in new_value.  If old_value is non-NULL, the old  value  of  the  timer  is  stored
       there.

       Timers  decrement from it_value to zero, generate a signal, and reset to it_interval.  A timer which is set to zero (it_value is zero or the timer
       expires and it_interval is zero) stops.

       Both tv_sec and tv_usec are significant in determining the duration of a timer.

       Timers will never expire before the requested time, but may expire some (short) time afterwards, which depends on the system timer resolution  and
       on  the  system  load; see time(7).  (But see BUGS below.)  Upon expiration, a signal will be generated and the timer reset.  If the timer expires
       while the process is active (always true for ITIMER_VIRTUAL) the signal will be delivered immediately when generated.  Otherwise the delivery will
       be offset by a small time dependent on the system loading.
RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
例子:
    下面函数的作用是设置间隔定时器,自定义显示文件内容的速度,例如执行./test /etc/passwd把文件passwd中的内容按自定义的速度显示出来。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <signal.h>

#define BUFSIZE 5

void alarm_handler(int s)
{

}

int main(int argc, char **argv)
{
    if (argc != 2)
        return -1;

    int fd, ret;
    char buf[BUFSIZE];
    struct itimerval cur;

    cur.it_value.tv_sec = 5;
    cur.it_value.tv_usec = 0;
    cur.it_interval.tv_sec = 0;
    cur.it_interval.tv_usec = 50000;

    signal(SIGALRM, alarm_handler);

    setitimer(ITIMER_REAL, &cur, NULL);

    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(1);
    }

    while (1) {
        pause();
        ret = read(fd, buf, BUFSIZE);
        if (ret == 0)
            break;
        write(1, buf, ret);    
    }
    
    close(fd);
    exit(0);
}


阅读(2591) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-11-22 17:52:36

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com