I am using the Linux ptrace API in a profiler I am writing.
My pseudo C code looks like this:
setjmp();
measure();
alarm(N);
while(1) {
waitpid(child, &status, WNOHANG);
if(child_process_exiting) {
measure();
break;
}
}
The alarm signal handler is as follows:
void sig_handler(int sig) {
signal(SIGALRM, sig_handler);
longjmp(env, 0);
}
I want to repeatedly return to the setjmp call until the child process exits and breaks the loop. The goal is to run the measure function every N seconds until the child process exits.
Why not simple just sleep for N
seconds, then do the measure
call? If you install a SIGCHLD
handler to catch when the child process exits, then e.g. sleep
will be interrupted and return the number of seconds left.
Something like
signal(SIGCHLD, sigchld_handler);
do
{
measure();
} while (sleep(N) == 0);
waitpid(...);