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

三种不同精度的睡眠

1.sleep

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>  
  2. 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.

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例  
  2. int sleepTime = 5;  
  3. do  
  4. {  
  5.     sleepTime = sleep(sleepTime);  
  6. }  
  7. while (sleepTime > 0);  

2.usleep(以微秒为单位)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 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(以纳秒为单位)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <time.h>  
  2. int nanosleep(const struct timespec *req, struct timespec *rem);  

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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct timespec  
  2. {  
  3.     time_t tv_sec;        /* seconds: 秒 */  
  4.     long   tv_nsec;       /* nanoseconds: 纳秒 */  
  5. };  

三种时间结构

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. time_t  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct timeval {  
  2.     long    tv_sec;         /* seconds */  
  3.     long    tv_usec;        /* microseconds 微秒*/  
  4. };  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct timespec {  
  2.     time_t tv_sec;        /* seconds */  
  3.     long   tv_nsec;       /* nanoseconds */  
  4. };  

setitimer

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/time.h>  
  2. int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));  

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

参数

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

  第二个参数是请求的时间

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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct itimerval  
  2. {  
  3.     struct timeval it_interval; /* next value : 产生信号的间隔时间*/  
  4.     struct timeval it_value;    /* current value : 第一次产生信号的时间*/  
  5. };  
  6. struct timeval  
  7. {  
  8.     time_t      tv_sec;         /* seconds: 秒 */  
  9.     suseconds_t tv_usec;        /* microseconds: 微秒 */  
  10. };  

which值

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

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

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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**示例1: 
  2. 1.在启动进程的5秒之后产生信号 
  3. 2.然后每隔1秒产生一次信号 
  4. **/  
  5. int main()  
  6. {  
  7.     if (signal(SIGALRM, signalAction) == SIG_ERR)  
  8.         err_exit("signal error");  
  9.   
  10.     struct itimerval it;  
  11.     struct timeval it_interval = {1, 0};  
  12.     struct timeval it_value = {5, 0};  
  13.     it.it_interval = it_interval;  
  14.     it.it_value = it_value;  
  15.     if (setitimer(ITIMER_REAL, &it, NULL) == -1)  
  16.         err_exit("setitimer error");  
  17.   
  18.     while (true)  
  19.         pause();  
  20. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**示例2: 
  2. 获取itimerval 结构体 
  3. **/  
  4. int main()  
  5. {  
  6.     struct itimerval it;  
  7.     struct timeval it_interval = {1, 0};  
  8.     struct timeval it_value = {5, 0};  
  9.     it.it_interval = it_interval;  
  10.     it.it_value = it_value;  
  11.     if (setitimer(ITIMER_REAL, &it, NULL) == -1)  
  12.         err_exit("setitimer error");  
  13.   
  14.     for (int i = 0; i < 100000; ++i)  
  15.         ;  
  16.   
  17.     struct itimerval oldIt;  
  18.   
  19. //    if (setitimer(ITIMER_REAL, &it, &oldIt) == -1)  
  20. //        err_exit("setitimer error");  
  21.     // 在不重新设置时钟的情况下获取剩余时间  
  22.     if (getitimer(ITIMER_REAL, &oldIt) == -1)  
  23.         err_exit("getitimer error");  
  24.   
  25.     cout << oldIt.it_interval.tv_sec << ' ' << oldIt.it_interval.tv_usec  
  26.          << ' ' << oldIt.it_value.tv_sec << ' ' << oldIt.it_value.tv_usec << endl;  
  27. }  

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

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

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值