0

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.

kobrien
  • 2,931
  • 2
  • 24
  • 33

2 Answers2

3

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(...);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • What action would the handler take in this approach? – kobrien Jul 25 '13 at 14:12
  • @kobrien Unless the child process can be stopped, the signal handled doesn't have to do anything at all. The `sleep` (and other similar functions) will return anyway when a signal is received. – Some programmer dude Jul 25 '13 at 14:18
0

I only find this need change:

setjmp(env); // env is a jum_buf value

Then

longjmp(env,1);// so if necessary, we can use :int flag = setjum(env); 

if flag == 1; I know code call sig_handler and back.

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31