/*-------------------------------------------------------------------*/ /* testitimersigaction.c */ /* The setitimer and sigaction system calls. */ /*-------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include #include /*-------------------------------------------------------------------*/ static void mySigAction(int iSignal, siginfo_t *psSigInfo, void *pvContext) /* This function is intended to be a handler for signals of type SIGPROF. Print to stdout the value of the EIP register at the time the signal occurred. */ { ucontext_t *psContext = (ucontext_t*)pvContext; int iEip; /* Print the contents of the eip register at the time the signal occurred. */ iEip = psContext->uc_mcontext.gregs[REG_EIP]; printf("%x\n", iEip); } /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* Illustrate the setitimer and sigaction system calls. */ { struct sigaction sSigAction; struct sigaction sOldSigAction; struct itimerval sTimerValue; struct itimerval sOldTimerValue; /* Register a signal handler to handle SIGPROF signals. */ sSigAction.sa_flags = SA_SIGINFO; sSigAction.sa_sigaction = mySigAction; sigemptyset(&sSigAction.sa_mask); sigaction(SIGPROF, &sSigAction, &sOldSigAction); /* Create an interval timer to generate a SIGPROF signal after A seconds and B microseconds of virtual time. Thereafter repeatedly generate a SIGPROF signal at C second, D microsecond intervals of virtual time. */ sTimerValue.it_value.tv_sec = 1; /* A */ sTimerValue.it_value.tv_usec = 0; /* B */ sTimerValue.it_interval.tv_sec = 1; /* C */ sTimerValue.it_interval.tv_usec = 0; /* D */ setitimer(ITIMER_PROF, &sTimerValue, &sOldTimerValue); /* Do some nonsensical computation, forever. */ { int i; i = 1; for (;;) { i += 1; i += i * i; } } } /* Sample execution: $ gcc -Wall -ansi -pedantic -o testitimersigaction testitimersigaction.c $ testitimersigaction 804850a 80484f3 80484f3 8048502 80484eb 80484fb 80484f1 80484fb 8048502 8048502 804850a 8048502 8048502 8048502 80484fb ^C */