<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[【转】Linux下c语言写的定时器（计时器）]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Mon, 25 Nov 2013 05:45:30 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	【转】Linux下c语言写的定时器（计时器）<br/>【实现功能】：下的C编程：编写一个程序（库），实现定时器（计时器）的功能，它能为用户提供在同一进程中多次使用的定时器。这里要求用信号来实现。<br/>【解题思路】：编写一个结构体Timer代表一个计时器，然后再定义Timer类型的数组myTimer[N]，用来保存我们设置的定时器；再定义函数setTimer（）生成计时器，并将生成的计时器保存到myTimer中，这样通过多次调用，就可以在同一个进程中生成多个计时器；定义timeout()信号处理函数，每隔一秒产生一个信号，通过调用timeout()对所有的定时器扫描一遍，检查哪些计时器超时。<br/>【程序代码】：如下<br/><textarea name="code" class="C" rows="15" cols="100">
#include&lt;stdlib.h&gt;
#include&lt;unistd.h&gt;
#include&lt;signal.h&gt;
#include&lt;time.h&gt;
#include&lt;sys/time.h&gt;
#define N 100 //设置最大的定时器个数
int i=0,t=1; //i代表定时器的个数；t表示时间，逐秒递增
struct Timer //Timer结构体，用来保存一个定时器的信息
&#123; 
int total_time; //每隔total_time秒
int left_time; //还剩left_time秒
int func; //该定时器超时，要执行的代码的标志
&#125;myTimer[N]; //定义Timer类型的数组，用来保存所有的定时器

void setTimer(int t,int f) //新建一个计时器
&#123; 
struct Timer a;
a.total_time=t;
a.left_time=t;
a.func=f;
myTimer[i++]=a;
&#125;

void timeout() //判断定时器是否超时，以及超时时所要执行的动作
&#123; 
printf(&quot;Time: %d&#92;n&quot;,t++);
int j;
for(j=0;j&lt;i;j++)
&#123; 
if(myTimer[j].left_time!=0)
myTimer[j].left_time--;
else
&#123; 
switch(myTimer[j].func)&#123; //通过匹配myTimer[j].func，判断下一步选择哪种操作
case 1:
printf(&quot;------Timer 1: --Hello Aillo!&#92;n&quot;);break;
case 2:
printf(&quot;------Timer 2: --Hello Jackie!&#92;n&quot;);break;
case 3:
printf(&quot;------Timer 3: --Hello PiPi!&#92;n&quot;);break;
&#125;
myTimer[j].left_time=myTimer[j].total_time; //循环计时
&#125;
&#125;
&#125;

int main() //测试函数，定义三个定时器
&#123; 
setTimer(3,1);
setTimer(4,2);
setTimer(5,3);
signal(SIGALRM,timeout); //接到SIGALRM信号，则执行timeout函数
while(1)
&#123; 
sleep(1); //每隔一秒发送一个SIGALRM
kill(getpid(),SIGALRM);
&#125;
exit(0);
&#125;
</textarea><br/><br/>执行情况如下：<br/>[root@test timer4linux]# make timer4linux<br/>cc&nbsp;&nbsp;&nbsp;&nbsp; timer4linux.c&nbsp;&nbsp; -o timer4linux<br/>timer4linux.c: In function ‘timeout’:<br/>timer4linux.c:26: warning: incompatible implicit declaration of built-in function ‘printf’<br/>[root@test timer4linux]# ls<br/>timer4linux&nbsp;&nbsp;timer4linux.c<br/>[root@test timer4linux]# ./timer4linux <br/>Time: 1<br/>Time: 2<br/>Time: 3<br/>Time: 4<br/>------Timer 1: --Hello Aillo!<br/>Time: 5<br/>------Timer 2: --Hello Jackie!<br/>Time: 6<br/>------Timer 3: --Hello PiPi!<br/>Time: 7<br/>Time: 8<br/><br/>来自：http://hi.baidu.com/symdfbb/item/a7024e115cae7302b98a1a0b
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 【转】Linux下c语言写的定时器（计时器）]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>