Search code examples
c++timerrestartwatchdog

I want to restart my program after the few seconds what I set


In present, I try to make a watchdog for my project.

Also, I want to make a restart timer.

I mean if the few seconds pass, the program will start from first.

Surely, I can use while loop in main function. I don't want this.

I just want to make some class such as a timer or watchdog,

After the main function passes the time I set, I want to let my program start again.

Is there any good idea?

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;
}

Solution

  • If you can get your code to keep an eye on the clock and voluntarily return after so-many-seconds have elapsed, that's usually the best way; however, since you mentioned a watchdog, it sounds like you don't want to trust your code to do that, so (assuming you have an OS that supports fork()) you can spawn a child process to run the code, and then the parent process can unilaterally kill() the child process after 5 seconds and then launch a new one. Here's an example, with a child process counting a random number of potatoes, one per second; if it tries to count more than 5 of them, it will be killed by the parent process.

    #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");
          }
       }
    }
    

    Note: If your OS doesn't support fork() (i.e. your OS is Windows), this technique is still possible, but it requires the use of Windows-specific APIs and is significantly more work to implement.