I all goes well, my program finishes in about N seconds:
./maybe_deadlock
But sometimes a deadlock happens randomly, and the program hangs forever.
To debug this, I want to run the program along these lines: How to debug a rare deadlock?
while true; do
date
gdb -ex 'set confirm on' -ex run -ex quit --args ./maybe_crash.out;
done
But to make things more convenient, I want to get a notification when the N seconds have elapsed, so I don't have to keep checking the terminal to see if stdout looks stuck.
So in other words, I want to run an arbitrary command from: https://unix.stackexchange.com/questions/144924/how-to-create-a-message-box-from-the-command-line if N seconds passed and the program didn't finish.
I know about the timeout
command from How to kill a child process after a given timeout in Bash?, but I don't want to kill my process since I want to examine it on GDB afterwards.
I think I can come up with an ugly solution that puts the process on the background, sleeps, waits and touches a sentinel file, but let's see if there is a more elegant method.
This program can be test the solutions:
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main() {
if (time(NULL) % 2) {
sleep(3);
} else {
while (1) {
sleep(0x7FFFFFFF);
}
}
return EXIT_SUCCESS;
}