一、采用新线程派驻的方式 (注: 编译时 需加上 -lrt)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <error.h>
#include <errno.h>
#include <string.h>
void timer_thread(union sigval v)
{
printf("timer_thread function! %d\n", v.sival_int);
}
int init_timer(timer_t *timerid, struct sigevent *evp, struct itimerspec *it)
{
if ( !evp || !it )
return -1;
memset(evp, 0, sizeof(struct sigevent)); //清零初始化
evp->sigev_value.sival_int = 111; //也是标识定时器的,这和timerid有什么区别?回调函数可以获得
evp->sigev_notify = SIGEV_THREAD; //线程通知的方式,派驻新线程
evp->sigev_notify_function = timer_thread; //线程函数地址
if (timer_create(CLOCK_REALTIME, evp, timerid) == -1)
{
perror("fail to timer_create");
return -1;;
}
printf("timer_create timerid = %d\n", *timerid);
it->it_interval.tv_sec = 1; // 后续按照该时间间隔
it->it_interval.tv_nsec = 0;
it->it_value.tv_sec = 3; // 最初开始时间间隔
it->it_value.tv_nsec = 0;
return 0;
}
int start_timer(timer_t *timerid, struct itimerspec *it)
{
if (it == NULL){
return -1;
}
if (timer_settime(*timerid, 0, it, NULL) == -1)
{
perror("fail to timer_settime");
return -1;
}
return 0;
}
void thread_func(void *param)
{
int *a = (int *)param;
while(1){
sleep(1);
printf("This is thread..\n");
}
*a = 100;
printf("param = %d\n", *a);
}
int main(int argc, const char *argv[])
{
pid_t pid = 0;
pthread_t thread;
timer_t timerid = 0;
int ret;
struct sigevent evp;
struct itimerspec it;
int a = 10;
#if 0
int ret = init_timer(&timerid, &evp, &it);
if (ret < 0){
printf("init_timer failed\n");
return -1;
}
#endif
if ((pid = fork()) < 0)
{
printf("fork failed.\n");
return -1;
}
else if ( pid == 0){
printf("child proc..\n");
ret = pthread_create(&thread, NULL, thread_func, &a);
int ret = init_timer(&timerid, &evp, &it);
if (ret < 0){
printf("init_timer failed\n");
return -1;
}
sleep(2);
printf("child timer_Id addr = %d\n", timerid);
start_timer(&timerid, &it);
sleep(10);
exit(0);
}
else{
printf("I'm parent proc..\n");
printf("parent timer_Id addr = %d\n", timerid);
printf("pthread_id = %d\n", thread);
do {
ret = waitpid(pid, NULL, WNOHANG);
if (ret == 0){
printf("No child exit\n");
sleep(2);
}
else if (ret == pid){
printf("Successly get child %d\n", pid);
}
else
printf("something error\n");
}while(ret == 0);
/*ret = waitpid(pid, NULL, 0);*/
/*if (ret == pid)*/
/*printf("successly get child %d\n", pid);*/
}
pause();
return 0;
}
二、采用通知方式为信号(signal)的处理方式
#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<string.h>
void handle()
{
time_t t;
char p[32];
time(&t);
strftime(p, sizeof(p), "%T", localtime(&t));
printf("time is %s\n", p);
}
int main()
{
struct sigevent evp;
struct itimerspec ts;
timer_t timer;
int ret;
memset(&evp, 0, sizeof(struct sigevent));
evp.sigev_value.sival_ptr = &timer;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGUSR1;
signal(SIGUSR1, handle);
ret = timer_create(CLOCK_REALTIME, &evp, &timer);
if( ret )
perror("timer_create");
ts.it_interval.tv_sec = 1;
ts.it_interval.tv_nsec = 0;
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ret = timer_settime(timer, 0, &ts, NULL);
if( ret )
perror("timer_settime");
while(1);
}
信号为signaction处理函数 :
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#define CLOCKID CLOCK_REALTIME
void sig_handler(int signo)
{
printf("timer_signal function! %d\n", signo);
}
int main()
{
//XXX int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
// signum--指定的信号编号,可以指定SIGKILL和SIGSTOP以外的所有信号编号
// act结构体--设置信号编号为signum的处理方式
// oldact结构体--保存上次的处理方式
//
// struct sigaction
// {
// void (*sa_handler)(int); //信号响应函数地址
// void (*sa_sigaction)(int, siginfo_t *, void *); //但sa_flags为SA——SIGINFO时才使用
// sigset_t sa_mask; //说明一个信号集在调用捕捉函数之前,会加入进程的屏蔽中,当捕捉函数返回时,还原
// int sa_flags;
// void (*sa_restorer)(void); //未用
// };
//
timer_t timerid;
struct sigevent evp;
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sig_handler;
act.sa_flags = 0;
// XXX int sigaddset(sigset_t *set, int signum); //将signum指定的信号加入set信号集
// XXX int sigemptyset(sigset_t *set); //初始化信号集
sigemptyset(&act.sa_mask);
if (sigaction(SIGUSR1, &act, NULL) == -1)
{
perror("fail to sigaction");
exit(-1);
}
memset(&evp, 0, sizeof(struct sigevent));
evp.sigev_signo = SIGUSR1;
evp.sigev_notify = SIGEV_SIGNAL;
if (timer_create(CLOCK_REALTIME, &evp, &timerid) == -1)
{
perror("fail to timer_create");
exit(-1);
}
struct itimerspec it;
it.it_interval.tv_sec = 2;
it.it_interval.tv_nsec = 0;
it.it_value.tv_sec = 1;
it.it_value.tv_nsec = 0;
if (timer_settime(timerid, 0, &it, 0) == -1)
{
perror("fail to timer_settime");
exit(-1);
}
pause();
return 0;
}