我想在我设置的几秒钟后重新启动我的程序
I want to restart my program after the few seconds what I set
目前,我尝试为我的项目做一个看门狗。
另外,我想做一个重启定时器。
我的意思是如果几秒钟过去了,程序将从第一个开始。
当然,我可以在 main 函数中使用 while 循环。我不要这个。
我只想做一些class比如定时器或者看门狗,
主函数过了我设置的时间后,我想让我的程序重新开始。
有什么好办法吗?
int main(void)
{
Timer timer(5) // setting my timer to 5 secs
//If time takes over the 5 secs in this loop,
//I want to restart the main loop.
while(1)
{
//Do Something...
}
return 0;
}
如果你能让你的代码关注时钟并在这么多秒过去后自愿return,那通常是最好的方法;但是,由于您提到了一个看门狗,听起来您不想相信您的代码会这样做,所以(假设您有一个 OS 在 5 秒后支持 fork()) you can spawn a child process to run the code, and then the parent process can unilaterally kill() 子进程,然后启动一个新的。这是一个例子,子进程计算随机数量的土豆,每秒一个;如果它试图计算超过 5 个,它将被父进程杀死。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
// The code you want to be able to abort and restart would go in here
static void FunctionThatMightTakeALongTime()
{
srand(time(NULL)); // just so we get different random values each time
const int countTo = (rand()%12)+1;
for (int i=0; i<countTo; i++)
{
printf("%i potato... (out of %i)\n", i+1, countTo);
sleep(1);
}
}
int main(int argc, char ** argv)
{
while(1)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork"); // fork() failed!?
return 10;
}
else if (pid == 0)
{
// We're in the child process -- do the thing
printf("Starting child process...\n");
FunctionThatMightTakeALongTime();
printf("Child process completed!\n");
return 0;
}
else
{
// We're in the parent/watchdog process -- wait
// 5 seconds, and then if the child process is
// still running, send it a SIGKILL signal to kill it.
// (if OTOH it has already exited, the SIGKILL isn't
// required but it won't do any harm either)
sleep(5);
printf("Watchdog: killing child process now\n");
if (kill(pid, SIGKILL) != 0) perror("kill");
// Now call waitpid() to pick up the child process's
// return code (otherwise he'll stick around as a zombie process)
if (waitpid(pid, NULL, 0) == -1) perror("waitpid");
}
}
}
注意:如果你的OS不支持fork()
(即你的OS是Windows),这个技巧仍然可行,但需要使用Windows 特定 API 的数量,并且要实施的工作要多得多。
目前,我尝试为我的项目做一个看门狗。
另外,我想做一个重启定时器。
我的意思是如果几秒钟过去了,程序将从第一个开始。
当然,我可以在 main 函数中使用 while 循环。我不要这个。
我只想做一些class比如定时器或者看门狗,
主函数过了我设置的时间后,我想让我的程序重新开始。
有什么好办法吗?
int main(void)
{
Timer timer(5) // setting my timer to 5 secs
//If time takes over the 5 secs in this loop,
//I want to restart the main loop.
while(1)
{
//Do Something...
}
return 0;
}
如果你能让你的代码关注时钟并在这么多秒过去后自愿return,那通常是最好的方法;但是,由于您提到了一个看门狗,听起来您不想相信您的代码会这样做,所以(假设您有一个 OS 在 5 秒后支持 fork()) you can spawn a child process to run the code, and then the parent process can unilaterally kill() 子进程,然后启动一个新的。这是一个例子,子进程计算随机数量的土豆,每秒一个;如果它试图计算超过 5 个,它将被父进程杀死。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
// The code you want to be able to abort and restart would go in here
static void FunctionThatMightTakeALongTime()
{
srand(time(NULL)); // just so we get different random values each time
const int countTo = (rand()%12)+1;
for (int i=0; i<countTo; i++)
{
printf("%i potato... (out of %i)\n", i+1, countTo);
sleep(1);
}
}
int main(int argc, char ** argv)
{
while(1)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork"); // fork() failed!?
return 10;
}
else if (pid == 0)
{
// We're in the child process -- do the thing
printf("Starting child process...\n");
FunctionThatMightTakeALongTime();
printf("Child process completed!\n");
return 0;
}
else
{
// We're in the parent/watchdog process -- wait
// 5 seconds, and then if the child process is
// still running, send it a SIGKILL signal to kill it.
// (if OTOH it has already exited, the SIGKILL isn't
// required but it won't do any harm either)
sleep(5);
printf("Watchdog: killing child process now\n");
if (kill(pid, SIGKILL) != 0) perror("kill");
// Now call waitpid() to pick up the child process's
// return code (otherwise he'll stick around as a zombie process)
if (waitpid(pid, NULL, 0) == -1) perror("waitpid");
}
}
}
注意:如果你的OS不支持fork()
(即你的OS是Windows),这个技巧仍然可行,但需要使用Windows 特定 API 的数量,并且要实施的工作要多得多。