linux c的程序
发布网友
发布时间:2022-11-15 17:22
我来回答
共2个回答
热心网友
时间:2024-12-02 17:41
#include <stdio.h>
#include <time.h>
timer_t g_stTimer ;
#define TIMER (10 ) //10 sec
timer_t start_timer(int tv_sec, int tv_usec, int Again)
{
timer_t tId = 0;
struct sigevent tSigEvent = {0};
struct itimerspec tItimerSpec = {0};
tSigEvent.sigev_value.sival_ptr = pTimerSignalInfo;
tSigEvent.sigev_notify = SIGEV_SIGNAL;
tSigEvent.sigev_signo = SIGUSR1;
if(timer_create(CLOCK_REALTIME, &tSigEvent, &tId) < 0)
{
printf("start_timer timer_create failed, errno = %d.\r\n", errno);
return 0;
}
if(nTvSec == 0 && nTvUsec == 0)
{
tItimerSpec.it_value.tv_sec = 1;
}
else
{
tItimerSpec.it_value.tv_sec = nTvSec;
}
tItimerSpec.it_value.tv_nsec = nTvUsec * 1000;
if(nAgain == 1)
{
tItimerSpec.it_interval.tv_sec = tItimerSpec.it_value.tv_sec;
tItimerSpec.it_interval.tv_nsec = tItimerSpec.it_value.tv_nsec;
}
else
{
tItimerSpec.it_interval.tv_sec = 0;
tItimerSpec.it_interval.tv_nsec = 0;
}
if(timer_settime(tId, 0, &tItimerSpec, NULL) < 0)
{
timer_delete(tId);
printf("start timer timer_settime failed, errno = %d.\r\n", errno);
return 0;
}
return tId;
}
void read_control()
{
//read control process
}
int main()
{
struct sigaction stTimer;
memset(&stTimer, 0, sizeof(stTimer));
stTimer.sa_flags = 0;
sigemptyset(&stTimer.sa_mask);
stTimer.sa_handler = read_control();
sigaction(SIGUSR1, &stTimer, NULL);
g_stTimer = start_timer( TIMER, 0, 1);
while(1)
{
printf("waiting .....");
}
return 0;
}
热心网友
时间:2024-12-02 17:41
楼主去看看unix环境高级编程里面的实例很多。