在这里,我有一个用于信号处理和复制过程的示例代码。在这里,我想使用信号动作调用一个计时器,每一秒。它在这里工作正常,但是当我在启动和停止计时器函数之间添加我的复制过程代码时,当第一次信号发出意味着 1 秒后,我的复制过程将被终止。
在这里,我尝试了这些SIGRTMAX ,SIGUSR1, SIGALRM
信号,但它们都给出了相同的结果。
为什么我的复制过程在信号发出时停止。?
代码 :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#define SIGTIMER SIGRTMAX
timer_t KeepAliveTimerId;
void stopKeepAlive()
{
if(KeepAliveTimerId != NULL)
{
timer_delete(KeepAliveTimerId);
printf("timer delete\n");
}
}
void signalHandler(int signo, siginfo_t* info, void* context)
{
if (signo == SIGTIMER)
{
printf("Signal Raised\n");
}
}
int startKeepAlive()
{
struct sigevent sigev; //signal event struct
struct itimerspec itval;
struct itimerspec oitval;
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO;
sigact.sa_sigaction = signalHandler;
// set up sigaction to catch signal
if (sigaction(SIGTIMER, &sigact, NULL) == -1)
{
printf("time_settime error \n");
return -1;
}
//Create the POSIX timer to generate signo
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGTIMER;
sigev.sigev_value.sival_int = 2;
if (timer_create(CLOCK_REALTIME, &sigev, &KeepAliveTimerId) == 0)
{
itval.it_value.tv_sec = 1;
itval.it_value.tv_nsec = 0L;
itval.it_interval.tv_sec = itval.it_value.tv_sec;
itval.it_interval.tv_nsec = itval.it_value.tv_nsec;
if (timer_settime(KeepAliveTimerId, 0, &itval, &oitval) != 0)
{
printf("Error in set time \n");
return -2;
}
}
else
{
printf("Error in creating timer \n");
return -3;
}
return 0;
}
int main()
{
int result;
// Start Timer
startKeepAlive();
result = system("cp /mnt/bct/package.QuipC /Download/ 2>&1");
if (result == 0)
{
printf("result is %d\n",result);
//stop timer
stopKeepAlive();
return EXIT_SUCCESS;
}
printf("result is %d\n",result);
// Stop Timer
stopKeepAlive();
return EXIT_FAILURE;
}