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

简介: 三种不同精度的睡眠1.sleep#include unsigned int sleep(unsigned int seconds);RETURN VALUE   Zero if ...

三种不同精度的睡眠

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.

//示例
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
struct timeval {
	long    tv_sec;         /* seconds */
	long    tv_usec;        /* microseconds 微秒*/
};
struct timespec {
	time_t tv_sec;        /* seconds */
	long   tv_nsec;       /* nanoseconds */
};

setitimer

#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
{
    time_t      tv_sec;         /* seconds: 秒 */
    suseconds_t tv_usec;        /* microseconds: 微秒 */
};

which值

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

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

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

/**示例1:
1.在启动进程的5秒之后产生信号
2.然后每隔1秒产生一次信号
**/
int main()
{
    if (signal(SIGALRM, signalAction) == SIG_ERR)
        err_exit("signal error");

    struct itimerval it;
    struct timeval it_interval = {1, 0};
    struct timeval it_value = {5, 0};
    it.it_interval = it_interval;
    it.it_value = it_value;
    if (setitimer(ITIMER_REAL, &it, NULL) == -1)
        err_exit("setitimer error");

    while (true)
        pause();
}
/**示例2:
获取itimerval 结构体
**/
int main()
{
    struct itimerval it;
    struct timeval it_interval = {1, 0};
    struct timeval it_value = {5, 0};
    it.it_interval = it_interval;
    it.it_value = it_value;
    if (setitimer(ITIMER_REAL, &it, NULL) == -1)
        err_exit("setitimer error");

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

    struct itimerval oldIt;

//    if (setitimer(ITIMER_REAL, &it, &oldIt) == -1)
//        err_exit("setitimer error");
    // 在不重新设置时钟的情况下获取剩余时间
    if (getitimer(ITIMER_REAL, &oldIt) == -1)
        err_exit("getitimer error");

    cout << oldIt.it_interval.tv_sec << ' ' << oldIt.it_interval.tv_usec
         << ' ' << oldIt.it_value.tv_sec << ' ' << oldIt.it_value.tv_usec << endl;
}

附:秒-微秒-纳秒的转换

  S(秒)、ms(毫秒)、μs(微秒)、ns(纳秒),其中:1s=1000ms,1 ms=1000μs,1μs=1000ns

目录
相关文章
|
22天前
|
存储 人工智能 数据管理
深入理解Linux操作系统之文件系统管理探索人工智能:从理论到实践的旅程
【8月更文挑战第30天】在探索Linux的无限可能时,我们不可避免地会遇到文件系统管理这一核心话题。本文将深入浅出地介绍Linux文件系统的基础知识、操作命令及高级技巧,帮助你更有效地管理和维护你的系统。从基础概念到实践应用,我们将一步步揭开Linux文件系统的神秘面纱。
|
25天前
|
Linux 调度
Linux0.11 信号(十二)(下)
Linux0.11 信号(十二)
19 1
|
1月前
|
存储 Linux 调度
|
25天前
|
存储 Unix Linux
Linux0.11 信号(十二)(上)
Linux0.11 信号(十二)
21 0
|
1月前
|
Linux
|
1月前
|
存储 安全 Linux
Linux存储安全:系统更新和补丁管理的策略与实践
【8月更文挑战第19天】安全是一个持续的过程,需要不断地评估、更新和改进策略。
33 0
|
1月前
|
存储 安全 Linux
Linux存储安全:数据加密的实践与策略
【8月更文挑战第19天】数据加密是Linux存储安全的基石之一。通过使用LUKS进行磁盘加密和使用GnuPG进行文件加密,可以显著提高数据的安全性。
43 0
|
1月前
|
存储 监控 安全
Linux存储安全:访问控制的实践与策略
【8月更文挑战第18天】Linux存储安全:访问控制的实践与策略
39 0
|
1月前
|
存储 安全 Linux
Linux存储安全:深入实践与案例分析
【8月更文挑战第18天】Linux存储安全是一个多层次、多维度的问题,需要从物理安全、访问控制、数据加密、审计监控、系统更新、备份策略等多个方面综合考虑。通过本文介绍的具体措施和案例代码,读者可以更好地理解如何在Linux系统中实施存储安全措施。安全是一个持续的过程,需要不断地评估、更新和改进策略。
60 0
|
2月前
|
监控 安全 Linux
Linux命令ssltap的深入解析与应用实践
`ssltap`是一个假想的Linux命令,用于模拟SSL/TLS流量分析。它捕获、解密(如果有密钥)并分析加密流量,提供实时监控、协议解析和安全审计。特点包括实时性、灵活性、可扩展性和安全性。示例用法包括捕获特定端口流量和实时监控会话状态。在实际操作中应注意私钥安全、性能影响及合规性,建议定期审计和自动化监控。