Linux中alarm/setitimer函数(信号函数)

news/2025/2/27 23:06:29/

alarm函数

函数原型: unsigned int alarm(unsigned int seconds);

函数描述:设置定时器(闹钟)。在指定seconds后,内核会给当前进程发送 14)SIGALRM信号。进程收到该信号,默认动作终止。每个进程都有且唯一的一个定时器。

函数返回值:返回0或者剩余的秒数,无失败。

alarm() returns the number of seconds remaining  until  any  previouslyscheduled alarm was due to be delivered, or zero if there was no previ‐ously scheduled alarm.

alarm(5)-----------sleep(2)---------->alarm(5)->接下来还是5秒后产生SIGALRM信号

 return 0;                                               return 3(之前的定时器还差3second结束);

取消定时器:alarm(0),返回旧闹钟剩余的秒数

测试:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <signal.h>
void handler(int signo)
{printf("signo==[%d]\n",signo);
}
int main()
{
//给内核注册信号捕捉函数,看看是不是发送SIFGALRM信号signal(SIGALRM,handler);int n=alarm(10);printf("n==[%d]\n",n);sleep(2);n=alarm(2);//2秒后发送信号printf("n==[%d]\n",n);sleep(5);//让进程休息5秒,不然进程结束,也收不到信号了
}

运行结果我们也可以发现给进程发出SIGALRM信号时,该进程直接终止,并没有sleep(5)那么久才结束

因为sleep()和阻塞时遇到信号会中断,并产生EINTR错误

闹钟实际执行时间=系统时间+用户时间+损耗时间

我们通过测试电脑一秒中能打印多少的数字(printf("[%d]\n",i++);)可知:

调用printf函数打印数字遇到\n才会打印,打印过程涉及到从用户区到内核区的切换(打印一次切换一次),切换次数越多消耗的时间越长,效率越低,损耗的时间越多。

而我们使用文件重定向操作时(./ arlarm > test.log->写到test.log),由于文件操作带缓冲,所以涉及到用户区到内核区的切换次数大大减少(缓冲区满了才从用户区切换到内核区),从而使损耗大大降低

setitimer函数:

函数原型: int setitimer(int which, const struct itimerval *new_value,
                     struct itimerval *old_value);

函数作用:设置定时器,可代替alarm函数,精度微秒us,可以实现周期定时。

函数参数:

which:指定定时方式

自然定时:ITIMER_REAL(计算自然时间) This  timer counts down in real (i.e., wall clock) time.At each expiration, a SIGALRM(14) signal is generated.虚拟空间计时(用户空间):ITIMER_VIRTUAL(只计算进程占用cpu的时间) This timer counts down against the  user-mode  CPU  timeconsumed  by the process.  (The measurement includes CPUtime consumed by all threads in the process.)   At  eachexpiration, a SIGVTALRM signal is generated.运行时计时(用户+内核)ITIMER_PROF(计算占用cpu及执行系统调用的时间)    This  timer  counts  down  against the total (i.e., bothuser and system) CPU time consumed by the process.  (Themeasurement includes CPU time consumed by all threads inthe process.)  At each expiration, a SIGPROF  signal  isgenerated.In  conjunction  with  ITIMER_VIRTUAL, this timer can beused to profile user and system CPU time consumed by theprocess.

 new value:负责设定timeout时间

old_value:存放旧的timeout值,一般设为NULL

 struct itimerval {struct timeval it_interval;设定以后每几秒执行function(周期)struct timeval it_value;设定什么时候执行第一次function};struct timeval {time_t      tv_sec;         秒suseconds_t tv_usec;       微秒};

测试;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <signal.h>
#include <sys/time.h>
void handler(int signo)
{printf("signo=[%d]\n",signo);
//alarm(0);可以取消计时器
}
int main()
{
//	signal(SIGALRM,handler);//int setitimer(int which, const struct itimerval *new_value,//   struct itimerval *old_value);struct itimerval value;
//设置间隔value.it_interval.tv_sec=1;value.it_interval.tv_usec=0;//因为在栈上,不初始化会随机值
//设置第一次发信号的时间value.it_value.tv_sec=2;value.it_value.tv_usec=0;setitimer(ITIMER_REAL,&value,NULL);
//一直执行进程while(1){sleep(1);}}

结果:2秒之后每隔1秒打印编号:

如果我们不写SIGALRM的信号处理函数,接收到SIGALRM后会直接终止进程(默认行为)。


http://www.ppmy.cn/news/1354709.html

相关文章

java 培训班预定管理系统Myeclipse开发mysql数据库web结构jsp编程servlet计算机网页项目

一、源码特点 java 培训班预定管理系统是一套完善的java web信息管理系统 采用serlvetdaobean&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发&#xf…

线性回归-使用ClickHouse机器学习函数

本文字数&#xff1a;5923&#xff1b;估计阅读时间&#xff1a;15 分钟 作者&#xff1a;Ensemble 审校&#xff1a;庄晓东&#xff08;魏庄&#xff09; 本文在公众号【ClickHouseInc】首发 这原本是转发的ensemble analytics的文章。 【https://ensembleanalytics.io/blog/l…

【实战】二、Jest难点进阶(二) —— 前端要学的测试课 从Jest入门到TDD BDD双实战(六)

文章目录 一、Jest 前端自动化测试框架基础入门二、Jest难点进阶2.mock 深入学习 学习内容来源&#xff1a;Jest入门到TDD/BDD双实战_前端要学的测试课 相对原教程&#xff0c;我在学习开始时&#xff08;2023.08&#xff09;采用的是当前最新版本&#xff1a; 项版本babel/co…

Nginx 命令(Ubuntu)

常用命令&#xff1a; 1.查看错误日志&#xff1a; sudo vim /var/log/nginx/error.log 2.重新加载 nignx sudo systemctl reload nginx 3.立即停止Nginx服务。如果Nginx正在运行&#xff0c;它将被终止 sudo systemctl stop nginx 4. 禁止Nginx服务在系统重启时自动启…

ClickHouse--12-可视化工具操作

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 可视化工具操作1 tabixhttp://ui.tabix.io/ 2 DBeaverhttps://dbeaver.io/download/ 可视化工具操作 1 tabix tabix 支持通过浏览器直接连接 ClickHouse&#xff…

微波炉维修笔记

微波主要是靠2.45GHz 左右的微波(12.2cm 波长)加热水分子实现食物加热 所有不要使用金属器皿进行加热&#xff0c;要么因为电磁屏蔽&#xff0c;起不到加热效果&#xff0c;要么火光四射 微波炉基本组成 借鉴姜师傅的视频 碰到不加热其它都正常的问题 1.检查高压电容 使用万…

AI生图软件:让创意无限飞扬

随着科技的飞速发展&#xff0c;人工智能(AI)已经逐渐渗透到我们的日常生活之中&#xff0c;其中包括图像编辑。AI生图软件就是这样一种应用了AI技术的创新产品&#xff0c;它正在改变着图像编辑的方式&#xff0c;让我们能够以前所未有的方式创作和分享视觉内容。 一、什么是A…

Vue2学习第三天

Vue2 学习第三天 1. 计算属性 computed 计算属性实现 定义&#xff1a;要用的属性不存在&#xff0c;要通过已有属性计算得来。 原理&#xff1a;底层借助了Objcet.defineproperty方法提供的getter和setter。 get函数什么时候执行&#xff1f; 初次读取时会执行一次。当依赖…