简体   繁体   中英

Why some system calls shield SIGPROF signal under strace

When I strace this code


void printMsg();

int main() {
  signal(SIGPROF, printMsg);

  struct itimerval tick;
  memset(&tick, 0, sizeof(tick));

  tick.it_value.tv_sec = 1;  // sec
  tick.it_value.tv_usec = 0; // micro sec.
  tick.it_interval.tv_sec = 0;
  tick.it_interval.tv_usec = 0;

  setitimer(ITIMER_PROF, &tick, NULL);

  while(1) {
    ;
  }

  return 0;
}

void printMsg() {
    printf("%s","Hello World!!\n");
}

I got the SIGPROF signal after 1 second as expected

...
05:54:10 setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={1, 0}}, NULL) = 0
05:54:11 --- SIGPROF {si_signo=SIGPROF, si_code=SI_KERNEL} ---
...

But when I add a system call like write(2, "", 0) or read(2, "", 0) in while(1) and strace again, it looks like SIGPROF can not be fired. However, time(0) in while(1) can trigger SIGPROF properly.

Btw, I use this code to emulate following PHP script, which ignored time limit under PHP-FPM SAPI,

<?php
set_time_limit(5);  // PHP uses setitimer(ITIMER_PROF) to implement this function
while (true) {
    flush();       // PHP uses write(<fd>, "", 0) to implement this function
}

The code trigger the SIGPROF using ITIMER_PROF, which is counting when the process is executing, or when the system is running on behalf of the process.

When the process goes into 'read' or 'write' calls, it is not 'running'. The process goes into IO Wait (and the system will allocate the CPU to other processes). When the process is in IOWait, the timer is not moving. When there is a system call that perform processing (mostly in system space) - eg time() is a tight loop, the timer will be moving.

See more about process states in https://www.tecmint.com/linux-process-management/

If you want to measure 'clock' time, consider using timer_create with CLOCK_REALTIME or CLOCK_MONOTONIC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM