You need to use a timer. There are several APIs to do this.
Just using alarm
is the simpliest.
There is setitimer/getitimer
.
There is the timer_create/timer_destroy/timer_settime
family.
Each has their own strengths offset by their own complexities.