setitimer()函数

定时器时间函数

struct itimerval:struct itimerval *new_value,其定义如下:struct itimerval {struct timeval it_interval;  /*next value*/struct timeval it_value; /*current value*/};struct timeval {long tv_sec; /*seconds*/lonng tv_usec /*microseconds*/};

 

和alarm函数类是,用于定时操作。

函数原型为

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

其中which参数表示类型,可选的值有: 

  1. ITIMER_REAL:以系统真实的时间来计算,它送出SIGALRM信号。 
  2. ITIMER_VIRTUAL:以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。
  3. ITIMER_PROF:以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号。

old_value的值一般为NULL。

settimer工作机制是,先对it_value倒计时,当it_value为零时触发信号,然后重置为it_interval,继续对it_value倒计时,一直这样循环下去。

/***
setitimer.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<signal.h>void myfunc(int signo)
{puts("hello world.");
}int main()
{struct itimerval it,oldit;signal(SIGALRM,myfunc);it.it_value.tv_sec = 5;it.it_value.tv_usec = 0;it.it_interval.tv_sec = 3;it.it_interval.tv_usec = 0;if(setitimer(ITIMER_REAL,&it,&oldit) == -1){perror("setitimer error ");exit(1);}while(1);return 0;
}

运行结果:

root@ubuntu:/mnt/hgfs/ShareWindows/shiyanlou/C/Flappy_Bird# gcc setitimer.c -o setitimer

root@ubuntu:/mnt/hgfs/ShareWindows/shiyanlou/C/Flappy_Bird# ./setitimer

hello world.

hello world.

hello world.

hello world.

hello world.

hello world.

^C

执行之后,间隔5秒之后打印hello,world。然后在间隔三秒打印,后续的都是间隔3秒。

 

这里我们知道:new_value.it_interval是用来控制第一次定时时长,new_value.it_value是用来定时后续定时的时长,并且是程序不死就连续定时(即,信号被我们捕捉,不执行默认动作了)。

 

如果想知道定时器还有多久定到时间,再次调用setitimer函数,取出old_value可查看。

 

转载于:https://www.cnblogs.com/wanghao-boke/p/11577919.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384818.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

shell编程题(三)

将一目录下所有的文件的扩展名改为bak #! /bin/bashfor i in ls domv $i ${i%%.*}.bak done ${i%%.*} 截掉一个变量字符串第一个"."以及其右侧的所有字符&#xff0c;即最短前缀匹配。 #! /bin/bashdir./ #指定文件夹for file in $dir*.txt #指定文件夹下面文件类型…

VMware安装VMwaretools

默认点击“安装VMware Tools&#xff08;T&#xff09;”选项下载好安装包 下载的安装包放在计算机的media目录下 进入/media/ubuntu14-04/VMware Tools目录&#xff1a; cd /media/ubuntu14-04/VMware Tools 将安装包复制到tmp文件夹下&#xff1a; cp VMwareTools-10.0.5-322…

shell编程题(四)

编译当前目录下的所有.c文件 #!/bin/bashif [ $# -lt 1 ] ;then #如果输入参数小于1则报错 $# C语言中的argv[0] 输入参数个数echo "Please follow up file.c!"echo "eg: ./make.sh xxx.c"exit fiif [[ $2 "debug" ]] ;then #如果第三个参数…

stat函数

int lstat(const char *path,struct stat*buf) 当文件是一个符号链接时&#xff0c;lstat返回的是该符号链接本身的信息&#xff0c;而stat返回的是该链接指向的文件的信息。 struct stat {dev_t st_dev; //设备号码ino_t st_ino; //inode节点号mo…

字符串函数参数传入传出(字符串反转)

/*** strstr.c ***/ #include<stdio.h> #include<string.h>//求字符串p中abcd出现的次数 //自定义函数接口完成业务函数和main函数分开 int getCount(char *mystr,char *sub,int *ncount) {int ret 0;if(mystr NULL || sub NULL || ncount NULL){ret -1;print…

字符串函数参数传入传出(去空格)

字符串作为函数参数传入传出 /*** delSpace.c ***/ #include<stdio.h> #include<string.h>int DelSpace(char *str) {int iRet -1;int i ;int j ;int ncount 0;char *out str;if(NULL str ){printf("the point in is NULL\n");return iRet;}i 0;j …

字符串的规范使用

有一个字符串”1a2b3d4z”,&#xff1b; 要求写一个函数实现如下功能&#xff0c; 功能1&#xff1a;把偶数位字符挑选出来&#xff0c;组成一个字符串1。valude&#xff1b;20分 功能2&#xff1a;把奇数位字符挑选出来&#xff0c;组成一个字符串2&#xff0c;valude 20 功能…

字符串的规范使用(二)

键值对&#xff08;”key valude”&#xff09;字符串&#xff0c;在开发中经常使用&#xff1b; 要求1&#xff1a;请自己定义一个接口&#xff0c;实现根据key获取valude&#xff1b;40分 要求2&#xff1a;编写测试用例。30分 要求3&#xff1a;键值对中间可能有n多空格&am…

字符串逆序打印

版本1 两个指针从头到尾和从尾到头交换内容逆序 /*** str_reverse.c ***/ #include<stdio.h> #include<string.h>int main() {char buf[] "abcdrfg";int len strlen(buf);char *p1 buf;char *p2 buf len - 1;while(p1 < p2){char c *p1;*p1 *p…

realloc()函数

原型&#xff1a;extern void *realloc(void *mem_address, unsigned int newsize); 参数&#xff1a; mem_address&#xff1a; 要改变内存大小的指针名newsize &#xff1a; 新的内存大小。 如果分配内存减少&#xff0c;realloc仅仅改变索引的信息。 如果将…

fgets()函数

声明&#xff1a;  char *fgets(char *str&#xff0c;int n,FILE* stream) 参数&#xff1a;  str—这是指向一个字符数组的指针&#xff0c;该数组存储了要读取的字符串 n – 这是要读取的最大字符数&#xff08;包括最后的空字符&#xff09;。通常是使用以str传递的数组…

C语言实现文件类型统计函数

#include<dirent.h> #include<limits.h> #include<sys/stat.h> #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h>#define FTW_F 1 //标记非目录文件 #define FTW_D 2 //标…

C语言实现多线程排序

#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h>/* 声明变量 */ int array_length, file_length; int *array_master; FILE *freader;/* 用于从文件读取数据 */ int *read_file(char *fname) {freader fopen(fnam…

linux线程操作

初始化条件变量 int pthread_cond_init(pthread_cond_t *cv,pthread_cond_attr *cattr); 函数返回值&#xff1a;返回0表示成功&#xff0c;返回其他表示失败。 参数&#xff1a; pthread_cond_attr是用来设置pthread_cond_t的属性&#xff0c;当传入的值是NULL的时候表…

Linux下多线程模拟停车场停车

#include<stdio.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<pthread.h>#define ONE_SECOND 1000000 #define RANGE 10 #define PERIOD 2 #define NUM_THREADS 4typedef struct {int *carpark; //用一个数组来模…

【C++学习之路】第一章——C++核心方法总论

1 C核心方法总论 1.1 核心思想 通过实际项目来学习编程&#xff0c;更高效掌握编程规则&#xff0c;以及明白各种语法规则的实际应用。 实验思想&#xff1a;任何C的参考资料都不可能覆盖你遇到的所有问题&#xff0c;这个时候&#xff0c;最好的办法就是&#xff0c;编辑代…

【Verilog HDL学习之路】第一章 Verilog HDL 数字设计总论

1 Verilog HDL 数字设计总论 1.1 几个重要的概念 EDA&#xff08;Electronic Design Automation&#xff09; 电子技术自动化 EDA工具 类似于软件工程中的IDE&#xff08;集成开发环境&#xff09;&#xff0c;能够使用Verilog HDL语言描述电路设计&#xff0c;并且能够通过逻…

【学会如何学习系列】从婴儿到大学——学习的本质从未改变过

从婴儿到大学——学习的本质从未改变过 从我们出生一直到现在&#xff0c;其实&#xff0c;学习的本质从来都没有改变过&#xff0c;并且&#xff0c;婴儿时期的我们&#xff0c;是学习能力最强的时候&#xff0c;随着我们不断长大&#xff0c;外界的诱惑越来越多&#xff0c;…

【汇编语言学习之路】第一章 汇编语言核心方法论

版权声明&#xff1a;本学习笔记是本人根据小甲鱼“汇编语言学习课程”和《汇编语言》&#xff08;王爽&#xff09;的书籍&#xff0c;来记录笔记的 1 汇编语言核心方法论 1.1 学习汇编语言的必要性 汇编语言与机器语言是一一对应关系&#xff0c;它的本质是机器语言的代号。…

蓝桥单片机赛题及模拟题代码

链接&#xff1a;https://pan.baidu.com/s/1BVB6VILEed0ufqRDMhvALg 提取码&#xff1a;ukx7