原文出处:http://blog.sina.com.cn/s/blog_6a1837e90100uhl3.html

alarm也称为闹钟函数,alarm()用来设置信号SIGALRM在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds为0,则之前设置的闹钟会被取消,并将剩下的时间返回。要注意的是,一个进程只能有一个闹钟时间,如果在调用alarm之前已设置过闹钟时间,则任何以前的闹钟时间都被新值所代替。

所需头文件
  #include<unistd.h>

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

函数参数
  seconds:指定秒数

函数返回值
  成功:如果调用此alarm()前,进程已经设置了闹钟时间,则返回上一个闹钟时间的剩余时间,否则返回0。

  出错:-1

代码

#include<unistd.h>
#include<signal.h>
void handler()

{
   
printf("Hello\n");
    signal(SIGALRM, handler);
//让内核做好准备,一旦接受到SIGALARM信号,就执行 handler
    alarm(5);
}

void main()
{
    int i;
   
handler();
   
for(i=1;i<21;i++)

{
       
printf("sleep %d ...\n",i);
       
sleep(1);
    }
}

结果:刚开始在main中执行一次,然后每隔5秒执行一次handler()

Hello
sleep 1 ...
sleep 2 ...
sleep 3 ...
sleep 4 ...
sleep 5 ...
Hello
sleep 6 ...
sleep 7 ...
sleep 8 ...
sleep 9 ...
sleep 10 ...
Hello
sleep 11 ...
sleep 12 ...
sleep 13 ...
sleep 14 ...
sleep 15 ...
Hello
sleep 16 ...
sleep 17 ...
sleep 18 ...
sleep 19 ...
sleep 20 ...
Hello

注意:alarm只设定一个闹钟,时间到达并执行其注册函数之后,闹钟便失效。如果想循环设置闹钟,需在其注册函数中在调用alarm函数。

linux C之alarm函数 转的更多相关文章

  1. 三十一、Linux 进程与信号——SIGCHLD 信号、kill和raise函数以及alarm函数

    31.1 SIGCHLD 信号 子进程状态发生变化(子进程结束)产生该信号,父进程需要使用 wait 调用来等待子进程结束并回收它. 避免僵尸进程 #include <stdio.h> # ...

  2. linux几种定时函数的使用

    Linux定时函数介绍: 在程序开发过程中,我们时不时要用到一些定时器,通常如果时间精度要求不高,可以使用sleep,uslepp函数让进程睡眠一段时间来实现定时, 前者单位为秒(s),后者为微妙(u ...

  3. (笔记)Linux延时及时间函数总结

    一. 基础知识1.时间类型.Linux下常用的时间类型有4个:time_t,struct timeval,struct timespec,struct tm.(1)time_t是一个长整型,一般用来表 ...

  4. Linux下利用ioctl函数获取网卡信息

    linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv ...

  5. module_init宏解析 linux驱动的入口函数module_init的加载和释放

    linux驱动的入口函数module_init的加载和释放 http://blog.csdn.net/zhandoushi1982/article/details/4927579 void free_ ...

  6. 【C/C++】Linux下使用system()函数一定要谨慎

    [C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...

  7. linux C之access函数 (20

    http://blog.sina.com.cn/s/blog_6a1837e90100uh5d.html linux C之access函数 (20access():判断是否具有存取文件的权限 相关函数 ...

  8. linux c语言 select函数用法

    linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...

  9. [Linux]不可重入函数

    一.概述 怎么会有可重入和不可重入. 在多任务系统下,中断可能在任务执行的任何时间发生:如果一个函数的执行期间被中断后,到重新恢复到断点进行执行的过程中,函数所依赖的环境没有发生改变,那么这个函数就是 ...

随机推荐

  1. cxf发布 webservice服务

    导包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar commons-lang-2.6.ja ...

  2. JS 校验,检测,验证,判断函数集合

    http://jc-dreaming.iteye.com/blog/754690 /**  *判断对象是否为空 *Check whether string s is empty.  */  funct ...

  3. android的NDK和java进行本地socket通信

    关于Android应用与Framework的socket通信,相信关心这个问题的朋友们已经看过<android使用socket使底层和framework通信>这篇文章,美中不足的是作者只贴 ...

  4. git配置用户名邮箱

    通常会配置全局的 用户名 邮箱  例如 姓名@公司邮箱  姓名 git config --global user.name "姓名" git config --global use ...

  5. Android成长之路-手势库的创建

      手势库的创建: 使用SDK自带的例子GestureBuilder建立手势库,这个文件夹存在于android\android-sdk-windows \samples\android-10\Gest ...

  6. sql中一列拆成两列

    declare @table table (name nvarchar(4))insert into @tableselect '张三' union allselect '李四' union alls ...

  7. 基于Bootstrap的Asp.net Mvc 分页的实现(转)

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...

  8. json-lib 的maven dependency 异常解决方案

    项目中要用到json-lib,mvnrepository.com查找它的dependency时结果如下: xml 代码   <dependency> <groupId>net. ...

  9. s2sh框架整合具体配置-xml方式

    s2sh整合之xml方式 说明:本文档所採用的框架版本号为:Struts 2.1.8, Sping2.5.5,  Hibernate 3.5.6 1.    须要的jar包: ------------ ...

  10. php图片采集后按原路径保存图片

    php图片采集后按原路径保存图片. 代码: <?php $domain ='http://www.jbxue.com'; $url = '/newskin/images/v4/logo.jpg' ...