One way is to schedule an alarm signal (SIGALRM) to be sent to the process after a number of seconds have elapsed, given as an argument to alarm().
Write a signal handler that sets a flag upon receipt of SIGALRM; check the value of flag in the caller to decide whether to continue or exit the program.