标签:style http color io os ar 使用 sp div
作者:zhanhailiang 日期:2014-10-25
Linux为每个进程提供3种定时器:
其通过setitimer来初始化:
int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
设置定时器后在当前进程终止前每隔固定时间都会发送相应的信号。
此时我们通过sigaction来接收相应信号并处理相应逻辑:
int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
如下举例说明如何使用Linux定时器定时输出一段文本:
#include <sys/time.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <string.h> #define PROMPT "2 seconds over\n\a" char *prompt = PROMPT; unsigned int len; void prompt_info (int signo) { write (STDERR_FILENO, prompt, len); } /* * 设置收到SIGPROF信号的处理方式为prompt_info,打印一段文本 */ void init_sigaction (void) { struct sigaction act; act.sa_handler = prompt_info; act.sa_flags = 0; sigemptyset (&act.sa_mask); sigaction (SIGPROF, &act, NULL); } /* * 设置ITIMER_PROF类型的定时器, 每隔2秒发送一次SIGPROF信号 */ void init_time () { struct itimerval value; value.it_value.tv_sec = 2; value.it_value.tv_usec = 0; value.it_interval = value.it_value; setitimer (ITIMER_PROF, &value, NULL); } int main () { len = strlen (prompt); init_sigaction (); init_time (); while (1); exit (0); }
编译执行如下:
[root@~/wade/codeReview/learningc/10]# gcc interval.c -o interval [root@~/wade/codeReview/learningc/10]# ./interval 2 seconds over 2 seconds over 2 seconds over 2 seconds over 2 seconds over ^C [root@~/wade/codeReview/learningc/10]#
Linux操作系统下C语言编程入门:http://wenku.baidu.com/link?url=pqkDmO8ibRGlaTVfHLe-CjBv4eOglm_9mgaKfuux5S2LHk_80EO5ZZpBUeTibs4Eo1-6_rXSmcUSo1qQ8XkExR9kX6ulqf7h1yTSfmV4le_
标签:style http color io os ar 使用 sp div
原文地址:http://blog.csdn.net/billfeller/article/details/40442365