If you're reading messages from network sockets, you can use select() to observe the sockets, and have a timeout on the amout of time you spend waiting for something to happen
The pseudocode would be something like this
Code:
timeout = 2_mins;
while ( 1 ) {
n = select ( ..., timeout );
if ( message_received ) {
do_work();
// work out how long we waited for a message
// and how long it took to process it.
timeout = ?
} else
if ( timed_out ) {
do_timeout_code();
timeout = 2_mins;
}
}
Another alternative is to perhaps use threads, but that depends on how much interaction there is between the normal program and the timed program. If there is shared data, then access to that data will need protecting with semaphores and mutexes.