没测试过 仅保存备份
#if 0
//通过自己计算时间差的方法来定时 秒#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
static char msg[] = "I received a msg.\n";
int len;
static time_t lasttime;
void show_msg(int signo)
{
write(STDERR_FILENO, msg, len);
}
int main()
{
struct sigaction act;
union sigval tsval;
act.sa_handler = show_msg;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(50, &act, NULL);
len = strlen(msg);
time(&lasttime);
while ( 1 )
{
time_t nowtime;
/*获取当前时间*/
time(&nowtime);
/*和上一次的时间做比较,如果大于等于2秒,则立刻发送信号*/
if (nowtime - lasttime >= 20)
{
/*向主进程发送信号,实际上是自己给自己发信号*/
sigqueue(getpid(), 50, tsval);
lasttime = nowtime;
}
}
return 0;
}
#endif
//通过自己计算时间差的方法来定时 毫秒
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
static char msg[] = "I received a msg.\n";
int len;
//static time_t lasttime;
struct timeval start;
void show_msg(int signo)
{
write(STDERR_FILENO, msg, len);
}
int main()
{
struct sigaction act;
union sigval tsval;
act.sa_handler = show_msg;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(50, &act, NULL);
len = strlen(msg);
// time(&lasttime);
gettimeofday(&start, NULL);
while ( 1 )
{
// time_t nowtime;
struct timeval end;
/*获取当前时间*/
// time(&nowtime);
gettimeofday(&end, NULL);
/*和上一次的时间做比较,如果大于等于2秒,则立刻发送信号*/
if (end.tv_usec - start.tv_usec >= 20)
{
/*向主进程发送信号,实际上是自己给自己发信号*/
sigqueue(getpid(), 50, tsval);
start.tv_usec = end.tv_usec;
}
}
return 0;
}