signal 信号处理例子
#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_handler\n");
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: %s\n", strerror(errno));
return 1;
}
cyclic_task();
/********************************************************************/
return 0;
}
本文地址:https://blog.csdn.net/tbadolph/article/details/109635529