【发布时间】:2015-01-03 11:07:22
【问题描述】:
我正在尝试比较给定日期何时对应于当前时间,何时发生它应该执行程序。我使用了一个无限循环,以便它等待给定时间与当前时间相对应,问题是当发生这种情况时,它会多次执行程序,我不知道如何解决这个问题......
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int taskexecution()
{
char * path;
path = "/home/soraia/mieti/Proj/makefile";
pid_t fk = fork();
if (!fk) { /* in child */
chdir("/home/soraia/mieti/Proj");
execlp ("make", "make", "-f", path , NULL);
_exit(127);
}
else if (fk == -1)
{
perror("fork"); /* print an error message */
}
return 0;
}
void time()
{
struct tm data;
data.tm_year=2015-1900;
data.tm_mon=1-1;
data.tm_mday=03;
data.tm_hour=10;
data.tm_min=49;
data.tm_sec=10;
data.tm_isdst = -1;
if(mktime(&data) == time(NULL))
{
taskexecution();
}
}
int main ()
{
while(1)
{
time();
}
return 0;
}
【问题讨论】:
-
我使用了无限循环,那么不应该期望它在无限时间内运行吗?
-
是的,但是当 if(mktime(&data) == time(NULL)) 到来时,它应该只执行一次程序,因为该条件只为真一次
标签: c date unix infinite-loop time.h