How to stop a process over a consistent time interval in order to execute a measurement function?

StackOverflow https://stackoverflow.com/questions/17859565

  •  04-06-2022
  •  | 
  •  

Вопрос

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(...);

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top