专栏中心

EEPW首页 > 专栏 > setitimer函数使用笔记

setitimer函数使用笔记

发布人:电子禅石 时间:2021-11-16 来源:工程师 发布文章

转载

setitimer函数使用笔记

函数原型int setitimer(int which, const struct itimerval *value,
                     struct itimerval *ovalue);

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.

       ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

       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.  SIG-
                      PROF is delivered upon expiration.

       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 */
            };

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

void PrintMsg(int Num)
{
    printf(
"%s\n""Hello World");

    
return;
}

int main(int argc, char* argv[])
{
    signal(SIGALRM, PrintMsg);

    
struct itimerval tick;
    tick.it_value.tv_sec 
= 10;  //十秒钟后将启动定时器
    tick.it_value.tv_usec = 0;
    tick.it_interval.tv_sec  
=1//定时器启动后,每隔1秒将执行相应的函数
    tick.it_interval.tv_usec = 0;

    
//setitimer将触发SIGALRM信号
    int ret = setitimer(ITIMER_REAL, &tick, NULL);

    
if ( ret != 0)
    {
        printf(
"Set timer error. %s \n", strerror(errno) );

        
return -1;
    }

    printf(
"Wait!\n");

    getchar();

    
return 0;
}

加群:1732700158


专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词: linux

相关推荐

RK3506核心板SDK重磅升级,解锁三核A7实时控制新架构

2025-12-18

Linux 今天 34 岁了

Linus Torvalds 称谷歌工程师提交的 RISC-V 代码为“垃圾”,并表示它“让世界变得更糟糕”

嵌入式系统 2025-08-11

基于ARM的嵌入式Linux系统开发 中

视频 2009-10-22

基于ARM的嵌入式Linux系统开发 上

视频 2009-10-22

实时Linux技术:如何在嵌入式LINUX中应用实时特性

视频 2009-10-22

U-BOOT 与 MontaVista Linux 的移植

由于英特尔裁员和公司重组,主要的英特尔 Linux 驱动项目正在消亡

Linux就AI生成代码定下规则:允许Copilot类工具、拒绝劣质AI代码,错误由人类负责

基于ARM的嵌入式Linux系统开发 下

视频 2009-10-22

53年后,惠普于1972年推出的总线标准获得了稳定的Linux驱动——通用接口总线拥有惊人的8 MB/s带宽

GeForce 590 驱动分支是首个不支持 GTX 9 和 10 系列 GPU 的分支——Linux 版本标志着定义时代显卡的终结

Linux中的fork与exec系列函数分析

嵌入式系统 2025-08-06

linux入门精华

Kontron与Congatec合作开发安全的嵌入式Linux平台

彻底告别486:Linux内核维护者正式移除英特尔486处理器支持

嵌入式Linux优化:加快系统起动和应用起动的过程

视频 2009-10-22

FFT公司9200的板子的资料

ARM 开发板使用手册 在三星 S3C2410开发板上烧写linux

更多 培训课堂
更多 焦点
更多 视频

技术专区