A word on using the profiling timers

We have explored, in some detail, the usage of the ITIMER_REAL timer type—which counts down in real-time. What about using the other two—the ITIMER_VIRTUAL and ITIMER_PROF—timers? Well, the code styling is very similar; there's nothing new there. The catch that a developer who is new to this faces is this: the signal(s) may never seem to arrive at all!

Let's take a simple code snippet using the ITIMER_VIRTUAL timer:

static void profalrm(int signum){    /* In production, do Not use signal-unsafe APIs like this! */    printf("In %s:%d sig=%d\n", __func__, __LINE__, signum);}[...]// in main() ...struct sigaction act;struct itimerval t1;memset(&act, 0, sizeof(act));act.sa_handler = profalrm;sigfillset(&act.sa_mask); ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.