Linux信号(5) --时间与定时器

三种不同精度的睡眠

1.sleep

#include <unistd.h>
unsigned int sleep(unsigned int seconds);

RETURN VALUE

   Zero if the requested time has elapsed, or the number of seconds left to  sleep,  if  the call was interrupted by a signal handler.

//保证睡眠5秒
int sleepTime = 5;
do
{
    sleepTime = sleep(sleepTime);
}
while (sleepTime > 0);

2.usleep(以微秒为单位)

int usleep(useconds_t usec);

The  type useconds_t is an unsigned integer type capable of holding integers in the range [0,1000000]. 

Programs will be more portable if they never mention this type  explicitly.

3.nanosleep(以纳秒为单位)

#include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);

req指定睡眠的时间, rem返回剩余的睡眠时间

struct timespec
{
    time_t tv_sec;        /* seconds: 秒 */
    long   tv_nsec;       /* nanoseconds: 纳秒 */
};

三种时间结构

time_t   //seconds
struct timeval {
	long    tv_sec;         /* seconds */
	long    tv_usec;        /* microseconds 微秒*/
};
struct timespec {
	time_t tv_sec;        /* seconds */
	long   tv_nsec;       /* nanoseconds */
};

定时器

setitimer  间歇性参数SIGALRM信号

#include <sys/time.h>
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));

setitimer()比alarm功能强大,支持3种类型的定时器

参数

  第一个参数which指定定时器类型

  第二个参数是请求的时间

  第三个参数是定时器原来所关联的值

struct itimerval
{
    struct timeval it_interval; /* next value : 产生信号的间隔时间*/
    struct timeval it_value;    /* current value : 第一次产生信号的时间*/
};


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

which值

  ITIMER_REAL: 经过指定的时间后,内核将发送SIGALRM信号给本进程 (用的较多)

  ITIMER_VIRTUAL : 程序在用户空间执行指定的时间后,内核将发送SIGVTALRM信号给本进程 

  ITIMER_PROF : 进程在内核空间中执行时,时间计数会减少,通常与ITIMER_VIRTUAL共用,代表进程在用户空间与内核空间中运行指定时间后,内核将发送SIGPROF信号给本进程。

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>


#define ERR_EXIT(m) \
    do \
    { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

void handler(int sig)
{
    printf("recv a sig=%d\n", sig);
}

int main(int argc, char *argv[])
{
    if (signal(SIGALRM, handler) == SIG_ERR)
        ERR_EXIT("signal error");

    struct timeval tv_interval = {1, 0};
    struct timeval tv_value = {5, 0};
    struct itimerval it;
    it.it_interval = tv_interval;
    it.it_value = tv_value;
    setitimer(ITIMER_REAL, &it, NULL);

    for (;;)
        pause();
    return 0;
}

可以看到第一次发送信号是在5s以后,之后每隔一秒发送一次信号

获得产生时钟信号的剩余时间

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>


#define ERR_EXIT(m) \
    do \
    { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)


int main(int argc, char *argv[])
{
    struct timeval tv_interval = {1, 0};
    struct timeval tv_value = {1, 0};
    struct itimerval it;
    it.it_interval = tv_interval;
    it.it_value = tv_value;
    setitimer(ITIMER_REAL, &it, NULL);

    int i;
    for (i=0; i<10000; i++);

//第一种方式获得剩余时间
    struct itimerval oit;
    setitimer(ITIMER_REAL, &it, &oit);//利用oit获得剩余时间产生时钟信号
    printf("%d %d %d %d\n", (int)oit.it_interval.tv_sec, (int)oit.it_interval.tv_usec, (int)oit.it_value.tv_sec, (int)oit.it_value.tv_usec);
//第二种方式获得剩余时间
    //getitimer(ITIMER_REAL, &it);
    //printf("%d %d %d %d\n", (int)it.it_interval.tv_sec, (int)it.it_interval.tv_usec, (int)it.it_value.tv_sec, (int)it.it_value.tv_usec);

    return 0;
}

getitimer无需设置新时钟获取旧的时钟信息

猜你喜欢

转载自blog.csdn.net/Alatebloomer/article/details/81808275