


The most powerful timer socket comes from the POSIX clock series. The actions of creating, initializing and deleting a timer are divided into three different functions: timer_create() (create timer), timer_settime() (initialize timer) device) and timer_delete (destroy it).
Create a timer:
<span style="font-family:SimSun;font-size:18px;color:#ff0000">int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid)</span>
A process can create a specific timer by calling timer_create(). The timer is unique to each process and is not inherited when forking. clock_id indicates which clock the timer is based on, and *timerid loads the ID of the created timer. This function creates the timer and loads its ID into the location pointed to by timerid. The parameter evp specifies the asynchronous notification to be generated when the timer expires. If evp is NULL, then the timer expiration will generate a default signal. For CLOCK_REALTIMER, the default signal is SIGALRM. If you want to generate a signal other than the default signal linux, the program must set evp->sigev_signo to the desired signal number. The member evp->sigev_notify in the structsigevent structure describes the action that should be taken when the timer expires. Generally, the value of this member is SIGEV_SIGNAL, which indicates that a signal will be generated when the timer expires. The program can set the member evp->sigev_notify to SIGEV_NONE to avoid generating a signal when the timer expires.
If several timers generate the same signal, the handler can use evp->sigev_value to determine which timer generated the signal. To implement these functions, the program must use the identifier SA_SIGINFO in the member sa_flags of structsigaction when installing a handler for the signal.
The value of clock_id is as follows:
struct sigevent { <span> </span>int sigev_notify; //notification type <span> </span>int sigev_signo; //signal number <span> </span>union sigval sigev_value; //signal value <span> </span>void (*sigev_notify_function)(union sigval); <span> </span>pthread_attr_t *sigev_notify_attributes; } union sigval { <span> </span>int sival_int; //integer value <span> </span>void *sival_ptr; //pointer value }
Customize the behavior after the timer expires by setting evp->sigev_notify to the following value:
Start a timer:
The timer created by timer_create() has not been started. To associate it with an expiration time and start clock period, use timer_settime().
<span style="font-family:SimSun;font-size:18px;color:#ff0000">int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspect *ovalue);</span>
struct itimespec { struct timespec it_interval; struct timespec it_value; };
Just like settimer(), it_value is used to specify the current timer expiration time. When the timer expires, the value of it_value will be updated to the value of it_interval. If the value of it_interval is 0, the timer is not an interval timer and will return to the unstarted state once the it_value expires. The structure of timespec provides millisecond-level code rate:
struct timespec { time_t tv_sec; long tv_nsec; };
If the value of flags is TIMER_ABSTIME, the time value specified by value will be parsed into an absolute value (the default parsing method for this value is relative to the current time). This changed behavior prevents race conditions during retrieval of the current time, calculation of the relative difference between this time and the desired future time, and starting timers.
If the value of ovalue is not NULL, the previous timer expiration time will be stored in the provided itimerspec. If the timer was not started before, all members of this structure will be set to 0.
Get the remaining time of an active timer:
<span style="font-family:SimSun;font-size:18px;color:#ff0000">int timer_gettime(timer_t timerid,struct itimerspec *value);</span>
Get the number of overrun runs of a timer:
有可能一个定时器到期了,而同一定时器上一次到期时形成的讯号还处于挂起状态。在这些情况下,其中的一个讯号可能会遗失。这就是定时器超限。程序可以通过调用timer_getoverrun来确定一个特定的定时器出现这些超限的次数。定时器超限只能发生在同一个定时器形成的讯号上。由多个定时器linux 定时器程序,甚至是这些使用相同的时钟和讯号的定时器,所形成的讯号就会排队而不会遗失。
<span style="font-family:SimSun;font-size:18px;color:#ff0000">int timer_delete (timer_t timerid);</span>
执行成功时,timer_getoverrun()会返回定时器初次到期与通知进程(比如通过讯号)定时器已到期之间额外发生的定时器到期次数。举例来说linux 定时器程序,在我们之前的事例中,一个1ms的定时器运行了10ms,则此调用会返回9。假如超限运行的次数等于或小于DELAYTIMER_MAX,则此调用会返回DELAYTIMER_MAX。
执行失败时,此函数会返回-1并将errno设定会EINVAL,这个惟一的错误情况代表timerid指定了无效的定时器。
删掉一个定时器:
<span style="font-family:SimSun;font-size:18px;color:#ff0000">int timer_delete (timer_t timerid);</span>
一次成功的timer_delete()调用会销毁关联到timerid的定时器而且返回0。执行失败时,此调用会返回-1并将errno设定会EINVAL,这个惟一的错误情况代表timerid不是一个有效的定时器。
例1:
voidhandle()
time_tt;
charp[32];
time(&t);
strftime(p,sizeof(p),"%T",localtime(&t));
printf("timeis%s/n",p);
intmain()
structsigeventevp;
structitimerspects;
timer_ttimer;
intret;
evp.sigev_value.sival_ptr=&timer;
evp.sigev_notify=SIGEV_SIGNAL;
evp.sigev_signo=SIGUSR1;
signal(SIGUSR1,handle);
ret=timer_create(CLOCK_REALTIME,&evp,&timer);
if(ret)
perror("timer_create");
_sec=1;
_nsec=0;
_sec=3;
_nsec=0;
ret=timer_settime(timer,0,&ts,NULL);
if(ret)
perror("timer_settime");
while(1);
例2:
voidhandle(unionsigvalv)
time_tt;
charp[32];
time(&t);
strftime(p,sizeof(p),"%T",localtime(&t));
printf("%sthread%lu,val=%d,signalcaptured./n",p,pthread_self(),v.sival_int);
return;
intmain()
structsigeventevp;
structitimerspects;
timer_ttimer;
intret;
memset(&evp,0,sizeof(evp));
evp.sigev_value.sival_ptr=&timer;
evp.sigev_notify=SIGEV_THREAD;
evp.sigev_notify_function=handle;
evp.sigev_value.sival_int=3;//作为handle()的参数
ret=timer_create(CLOCK_REALTIME,&evp,&timer);
if(ret)
perror("timer_create");
_sec=1;
_nsec=0;
_sec=3;
_nsec=0;
ret=timer_settime(timer,TIMER_ABSTIME,&ts,NULL);
if(ret)
perror("timer_settime");
while(1);
The above is the detailed content of The most powerful timer interface comes from the POSIX clock series, did you know?. For more information, please follow other related articles on the PHP Chinese website!

您可以在 iPhone 相机上设置多长时间的定时器?当您在 iPhone 的相机应用程序中访问定时器选项时,您将获得在两种模式之间进行选择的选项:3 秒 (3s)和10 秒 (10s)。当您手持 iPhone 时,您可以使用第一个选项从前置或后置摄像头快速自拍。第二个选项在场景中很有用,可以在远处将 iPhone 安装到三脚架上来点击合影或自拍。 如何在 iPhone 相机上设置定时器 虽然在 iPhone 相机上设置定时器是一个相当简单的过程,但具体操作方式因所使用的 iPhone 机型而异。

如何实现Workerman文档中的定时器功能Workerman是一款强大的PHP异步网络通信框架,它提供了丰富的功能,其中就包括定时器功能。使用定时器可以在指定的时间间隔内执行代码,非常适合定时任务、轮询等应用场景。接下来,我将详细介绍如何在Workerman中实现定时器功能,并提供具体的代码示例。第一步:安装Workerman首先,我们需要安装Worker

PHP的POSIX扩展是一组允许PHP与POSIX兼容操作系统进行交互的函数和常量。POSIX(PortableOperatingSystemInterface)是一组操作系统接口标准,旨在允许软件开发人员编写可在各种UNIX或UNIX类操作系统上运行的应用程序。本文将介绍如何使用PHP的POSIX扩展,包括安装和使用。一、安装PHP的POSIX扩展在

Java定时器:如何设置每天定时执行任务?在日常的Java开发中,我们经常会遇到需要每天定时执行某个任务的需求。比如说每天凌晨1点执行数据备份任务,或者每天晚上8点发送日报邮件等等。那么在Java中,我们可以使用定时器来实现这样的功能。Java提供了多种定时器的实现方式,本文将介绍基于Timer和ScheduledExecutorService两种方式来设置

定时器的表达式用于定义任务的执行计划。定时器的表达式是基于“在给定的时间间隔之后执行任务”的模型。表达式通常由两个部分组成:一个初始延迟和一个时间间隔。

定时器的工作原理可以分为硬件定时器和软件定时器两种类型。硬件定时器的工作原理是时钟信号源提供稳定的时钟信号作为计时器的基准。计数器从预设值开始计数,每当时钟信号到达时计数器递增。当计数器达到预设值时,定时器会触发一个中断信号通知中断控制器处理相应的中断服务程序。在中断服务程序中,可以执行一些预定的操作。软件定时器的工作原理是通过编程语言或系统提供的库函数或系统调用来实现的等等。

On April 20, 2015, Redox OS surfaced as a new microkernel operating system "with a focus on safety, freedom, reliability, correctness, and pragmatism." Written in Rust and assembly language, this project was inspired by pieces of code such

Java定时器:如何设置每月定时执行任务?引言:在开发中,经常会遇到需要每月定时执行任务的场景,例如每月更新统计数据、定期发送报表等。Java提供了多种定时器实现方式,本文将介绍如何使用Java定时器来实现每月定时执行任务,并提供具体的代码示例。一、使用Timer类实现每月定时执行任务Timer类是Java提供的最基础的定时器类,通过它可以实现简单的定时任务


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
