#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void cyclic_task()
{
static long int count = 0;
while (1)
{
sleep(1);
printf("cyclic_task....n");
}
}
static unsigned int sig_alarms = 0;
/****************************************************************************/
void signal_handler(int signum)
{
switch (signum)
{
case SIGALRM: //收到信号SIGALRM之后,处理
printf("signal_handlern");
break;
}
}
int main(int argc, char **argv)
{
struct sigaction sa;
struct itimerval tv;
int err;
/********************************************************************/
sa.sa_handler = signal_handler; //信号处理句柄
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGALRM, &sa, 0)) { //安装信号处理
fprintf(stderr, "Failed to install signal handler!n");
return -1;
}
printf("Starting timer...n");
printf("**************Start test************n");
tv.it_value.tv_sec = 1; //定时器
tv.it_value.tv_usec = 0 ;
tv.it_interval = tv.it_value;
if (setitimer(ITIMER_REAL, &tv, NULL)) {
fprintf(stderr, "Failed to start timer: %sn", strerror(errno));
return 1;
}
cyclic_task();
/********************************************************************/
return 0;
}
最后
以上就是坚强香氛最近收集整理的关于signal 信号处理例子的全部内容,更多相关signal内容请搜索靠谱客的其他文章。
发表评论 取消回复