static void /* Thread notification function */ threadFunc(union sigval sv) { timer_t *tidptr; int s; tidptr = sv.sival_ptr; printf("[%s] Thread notify\n", currTime("%T")); printf(" timer ID=%ld\n", (long) *tidptr); printf(" timer_getoverrun()=%d\n", timer_getoverrun(*tidptr)); /* Increment counter variable shared with main thread and signal condition variable to notify main thread of the change. */ s = pthread_mutex_lock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_lock"); expireCnt += 1 + timer_getoverrun(*tidptr); s = pthread_mutex_unlock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_unlock"); s = pthread_cond_signal(&cond); if (s != 0) errExitEN(s, "pthread_cond_signal"); }
static void luasandbox_timer_handle_profiler(luasandbox_timer * lt) { // It's necessary to leave the timer running while we're not actually in // Lua, and just ignore signals that occur outside it, because Linux timers // don't fire with any kind of precision. Timer delivery is routinely delayed // by milliseconds regardless of how short a time you ask for, and // timer_gettime() just returns 1ns after the timer should have been delivered. // So if a call takes 100us, there's no way to start a timer and have it be // reliably delivered to within the function body, regardless of what you set // it_value to. php_luasandbox_obj * sandbox = lt->sandbox; if (!sandbox || !sandbox->in_lua) { return; } // Set a hook which will record profiling data (but don't overwrite the timeout hook) if (!sandbox->timed_out) { int overrun; lua_State * L = sandbox->state; lua_sethook(L, luasandbox_timer_profiler_hook, LUA_MASKCOUNT | LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 1); overrun = timer_getoverrun(sandbox->timer.profiler_timer->timer); sandbox->timer.profiler_signal_count += overrun + 1; sandbox->timer.overrun_count += overrun; // Reset the hook if a timeout just occurred if (sandbox->timed_out) { lua_sethook(L, luasandbox_timer_timeout_hook, LUA_MASKCOUNT | LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 1); } } }
static void handle_profiler_signal (int signal) { if (EQ (backtrace_top_function (), Qautomatic_gc)) /* Special case the time-count inside GC because the hash-table code is not prepared to be used while the GC is running. More specifically it uses ASIZE at many places where it does not expect the ARRAY_MARK_FLAG to be set. We could try and harden the hash-table code, but it doesn't seem worth the effort. */ cpu_gc_count = saturated_add (cpu_gc_count, 1); else { EMACS_INT count = 1; #ifdef HAVE_ITIMERSPEC if (profiler_timer_ok) { int overruns = timer_getoverrun (profiler_timer); eassert (overruns >= 0); count += overruns; } #endif eassert (HASH_TABLE_P (cpu_log)); record_backtrace (XHASH_TABLE (cpu_log), count); } }
static void handler(int sig, siginfo_t *si, void *uc) { printf("[%s] Got signal %d\n", currTime("%T"), sig); printf(" sival_int = %d\n", si->si_value.sival_int); printf(" timer_getoverrun() = %d\n", timer_getoverrun((timer_t) si->si_value.sival_ptr)); }
int main(int argc, char *argv[]) { struct sigevent ev; timer_t tid; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGCONT; if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } if (timer_getoverrun(tid+1) == -1) { if (EINVAL == errno) { printf("fcn returned -1 and errno==EINVAL\n"); return PTS_PASS; } else { printf("fcn returned -1 but errno!=EINVAL\n"); printf("Test FAILED\n"); return PTS_FAIL; } } printf("fcn did not return -1\n"); return PTS_PASS; }
/* * stress_timer_handler() * catch timer signal and cancel if no more runs flagged */ static void MLOCKED_TEXT stress_timer_handler(int sig) { struct itimerspec timer; sigset_t mask; (void)sig; if (!stress_timer_keep_stressing()) goto cancel; timer_counter++; if (sigpending(&mask) == 0) if (sigismember(&mask, SIGINT)) goto cancel; /* High freq timer, check periodically for timeout */ if ((timer_counter & 65535) == 0) if ((time_now() - start) > (double)g_opt_timeout) goto cancel; if (g_keep_stressing_flag) { int ret = timer_getoverrun(timerid); if (ret > 0) overruns += ret; stress_timer_set(&timer); return; } cancel: g_keep_stressing_flag = false; /* Cancel timer if we detect no more runs */ (void)memset(&timer, 0, sizeof(timer)); (void)timer_settime(timerid, 0, &timer, NULL); }
static void handler(int sig, siginfo_t *si, void *uc) { printf("\n %s: \n",__func__); timer_t *tidptr; printf(" signal recvd %d\n", sig); tidptr = si->si_value.sival_ptr; printf(" timer_getoverrun() = %d\n", timer_getoverrun(*tidptr)); }
int test( void ) { timer_t timerid = 0; int result; result = timer_getoverrun( timerid ); return result; }
// use of printf() is UNSAFE // use of current_time() is UNSAFE: not reentrant (static variable) static void signal_handler (int sig, siginfo_t *sigInfo_p, UNUSED void *uc) { timer_t *tid_p; tid_p = sigInfo_p->si_value.sival_ptr; printf ("[%s] got signal %d\n", current_time ("%T"), sig); printf ("\t*sival_ptr = %ld\n", (long)*tid_p); printf ("\ttimer_getoverrun() = %d\n", timer_getoverrun (*tid_p)); }
// Note this function is not async-signal safe. If you need to call this from a // signal handler, you'll need to refactor the "Set a hook" part into a // separate function and call that from the signal handler instead. static void luasandbox_timer_handle_profiler(union sigval sv) { luasandbox_timer_callback_data * data = (luasandbox_timer_callback_data*)sv.sival_ptr; php_luasandbox_obj * sandbox; while (1) { if (!data->sandbox) { // data is invalid return; } if (!sem_wait(&data->semaphore)) { // Got the semaphore! break; } if (errno != EINTR) { // Unexpected error, abort return; } } // It's necessary to leave the timer running while we're not actually in // Lua, and just ignore signals that occur outside it, because Linux timers // don't fire with any kind of precision. Timer delivery is routinely delayed // by milliseconds regardless of how short a time you ask for, and // timer_gettime() just returns 1ns after the timer should have been delivered. // So if a call takes 100us, there's no way to start a timer and have it be // reliably delivered to within the function body, regardless of what you set // it_value to. sandbox = data->sandbox; if (!sandbox || !sandbox->in_lua) { sem_post(&data->semaphore); return; } // Set a hook which will record profiling data (but don't overwrite the timeout hook) if (!sandbox->timed_out) { int overrun; lua_State * L = sandbox->state; lua_sethook(L, luasandbox_timer_profiler_hook, LUA_MASKCOUNT | LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 1); overrun = timer_getoverrun(sandbox->timer.profiler_timer->timer); sandbox->timer.profiler_signal_count += overrun + 1; sandbox->timer.overrun_count += overrun; // Reset the hook if a timeout just occurred if (sandbox->timed_out) { lua_sethook(L, luasandbox_timer_timeout_hook, LUA_MASKCOUNT | LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 1); } } sem_post(&data->semaphore); }
static void handler(int sig, siginfo_t *si, void *uc) { timer_t *tidptr; tidptr = si->si_value.sival_ptr; /* UNSAFE: This handler uses non-async-signal-safe functions (printf(); see Section 21.1.2) */ printf("[%s] Got signal %d\n", currTime("%T"), sig); printf(" *sival_ptr = %ld\n", (long) *tidptr); printf(" timer_getoverrun() = %d\n", timer_getoverrun(*tidptr)); }
static void print_siginfo(siginfo_t *si) { timer_t *tidp; int or; tidp = si->si_value.sival_ptr; printf(" sival_ptr = %p; ", si->si_value.sival_ptr); printf(" *sival_ptr = 0x%lx\n", (long) *tidp); or = timer_getoverrun(*tidp); if (or == -1) errExit("timer_getoverrun"); else printf(" overrun count = %d\n", or); }
void Timer::HandleOverrun(siginfo_t *info) { // Process and log overruns. timer_t *p_timer_id; int overrun_count; p_timer_id = static_cast<timer_t *>(info->si_value.sival_ptr); overrun_count = timer_getoverrun(*p_timer_id); if (overrun_count == -1) { perror("timer_getoverrun"); printf("%s:%d\n", __FILE__, __LINE__); exit(1); } else { if (overrun_count > 0) { printf("overrun_count(%d)\n", overrun_count); } } }
int main(int argc, char *argv[]) { timer_t tid; int tval = BOGUSTID; tid = (timer_t) & tval; if (timer_getoverrun(tid) == -1) { if (EINVAL == errno) { printf("fcn returned -1 and errno=EINVAL\n"); return PTS_PASS; } else { printf("fcn returned -1, but errno!=EINVAL\n"); printf("Test FAILED\n"); return PTS_FAIL; } } printf("fcn did not return -1\n"); return PTS_PASS; }
/* * stress_timer_handler() * catch timer signal and cancel if no more runs flagged */ static void MLOCKED stress_timer_handler(int sig) { struct itimerspec timer; int ret; (void)sig; timer_counter++; ret = timer_getoverrun(timerid); if (ret > 0) overruns += ret; if (opt_do_run) { stress_timer_set(&timer); } else { /* Cancel timer if we detect no more runs */ timer.it_value.tv_sec = 0; timer.it_value.tv_nsec = 0; timer.it_interval.tv_sec = 0; timer.it_interval.tv_nsec = 0; } (void)timer_settime(timerid, 0, &timer, NULL); }
void Thread::waitPeriod() { int sig; sigwait(&(alarm_sig), &sig); mWakeupsMissed += timer_getoverrun(mTimerfd); }
void * task_c (void *arg) { int count; struct timespec my_period; int my_sig, received_sig; struct itimerspec timerdata; struct itimerspec timergetdata; timer_t timer_id; time_t clock; struct sigevent event; sigset_t set; struct periodic_params *params; params = arg; my_period = params->period; my_sig = params->signo; /* timer create */ event.sigev_notify = SIGEV_SIGNAL; event.sigev_signo = my_sig; if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { perror ("Error in timer creation\n"); rtems_test_exit(0); } /* block the timer signal */ sigemptyset (&set); sigaddset (&set,my_sig); pthread_sigmask(SIG_BLOCK,&set,NULL); /* set the timer in periodic mode */ timerdata.it_interval = my_period; timerdata.it_value = my_period; timerdata.it_value.tv_sec *= 2; if (timer_settime(timer_id,POSIX_TIMER_RELATIVE,&timerdata,NULL) == -1) { perror ("Error in timer setting\n"); rtems_test_exit(0); } /* periodic activity */ for (count=0 ; ; count++) { if (sigwait(&set,&received_sig) == -1) { perror ("Error in sigwait\n"); rtems_test_exit(0); } if (timer_gettime(timer_id, &timerdata) == -1) { perror ("Error in timer_gettime\n"); rtems_test_exit(0); } if (! _Timespec_Equal_to( &timerdata.it_value, &my_period) ){ perror ("Error in Task C timer_gettime\n"); } pthread_mutex_lock (&data.mutex); while (data.updated == FALSE) { pthread_cond_wait (&data.sync,&data.mutex); } clock = time(NULL); printf("Executing task C with count = %2i %s", params->count, ctime(&clock) ); if ( count && (count % 5) == 0 ) { int overruns = 0; sleep(1); overruns = timer_getoverrun( timer_id ); printf( "task C: timer_getoverrun - overruns=%d\n", overruns ); if (timer_gettime(timer_id, &timergetdata) == -1) { perror ("Error in timer setting\n"); rtems_test_exit(0); } printf( "task C: timer_gettime - %" PRIdtime_t ":%ld remaining from %" PRIdtime_t ":%ld\n", timergetdata.it_value.tv_sec, timergetdata.it_value.tv_nsec, timergetdata.it_interval.tv_sec, timergetdata.it_interval.tv_nsec ); } pthread_mutex_unlock (&data.mutex); params->count--; if (params->count == 0) StopTimer(timer_id, &timerdata); } return NULL; }
int main (int argc, char *argv[]) { struct itimerspec ts; struct sigevent sevent; timer_t *tidlist_p; int i; sigset_t sigSet; if (argc < 2) { printf ("usage: %s <secs>[/<nsecs>][:<interval secs>[/<interval nsecs>]]...\n", argv[0]); return 0; } tidlist_p = calloc (argc - 1, sizeof (timer_t)); if (tidlist_p == NULL) { perror ("malloc()"); return 1; } sevent.sigev_notify = SIGEV_SIGNAL; sevent.sigev_signo = TIMER_SIG; sigemptyset (&sigSet); sigaddset (&sigSet, TIMER_SIG); sigprocmask (SIG_BLOCK, &sigSet, NULL); for (i=0; i<argc-1; ++i) { itimerspec_from_str (argv[i+1], &ts); sevent.sigev_value.sival_ptr = &tidlist_p[i]; if (timer_create (CLOCK_REALTIME, &sevent, &tidlist_p[i]) == -1) { perror ("timer_create()"); return 1; } printf ("timerID: %ld (%s)\n", (long)tidlist_p[i], argv[i+1]); if (timer_settime (tidlist_p[i], 0, &ts, NULL) == -1) { perror ("timer_settime()"); return 1; } } while (1) { int sig; siginfo_t sigInfo; timer_t *tid_p; sig = sigwaitinfo (&sigSet, &sigInfo); if (sig == -1) { perror ("sigwaitinfo()"); return 1; } tid_p = sigInfo.si_value.sival_ptr; printf ("[%s] got signal %d\n", current_time ("%T"), sig); printf ("\t*sival_ptr = %ld\n", (long)*tid_p); printf ("\ttimer_getoverrun() = %d\n", timer_getoverrun (*tid_p)); } return 0; }
int main() { sigset_t set; struct sigevent ev; timer_t tid; struct itimerspec its; int overruns; if (sigemptyset(&set) != 0) { perror("sigemptyset() did not return success\n"); return PTS_UNRESOLVED; } if (sigaddset(&set, SIGCONT) != 0) { perror("sigaddset() did not return success\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGCONT; /* * create first timer */ if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } its.it_interval.tv_sec = INTERVALSEC; its.it_interval.tv_nsec = 0; its.it_value.tv_sec = VALUESEC; its.it_value.tv_nsec = 0; if (timer_settime(tid, 0, &its, NULL) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } sleep(VALUESEC+2*INTERVALSEC+1); if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } overruns = timer_getoverrun(tid); if (EXPECTEDOVERRUNS == overruns) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("FAIL: %d overruns sent; expected %d\n", overruns, EXPECTEDOVERRUNS); return PTS_FAIL; } printf("UNRESOLVED: This code should not be executed.\n"); return PTS_UNRESOLVED; }
void CUstpFtdcMduserApi::SendOneQuote(int sig, siginfo_t *si, void *uc) { int ret = 0; int iOldErr; int iOverRun; char sendBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; char * pSendBuf = NULL; CUstpFtdcDepthMarketDataField data; //在信号处理函数开始时保存errno iOldErr = errno; CUstpFtdcMduserApi* pObj = (CUstpFtdcMduserApi*)si->si_value.sival_ptr; iOverRun = timer_getoverrun(pObj->m_timerid); if (-1 == iOverRun) { printf_blue("timer_getoverrun() return %d.\n",iOverRun);//g_log.error } else { printf_blue("timer_getoverrun() return %d.\n",iOverRun); } //no instrument subscribed if(NULL == pObj->m_pInstrSubSucc) { errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 g_log.error("No instrument subscribed !\n"); return ; } if(ATF_SUCC == pObj->m_QuoteQueue.PopOneData(&data)) { AddQuoteCnt(); SubscribeInstrument *pTmp = pObj->m_pInstrSubSucc; while(pTmp != NULL) { printf_green("ready to send a quote\n"); strcpy(data.InstrumentID,pTmp->instrument); pObj->m_FtdcMdSpi_instance->OnRtnDepthMarketData(&data); pTmp = pTmp->pNext; } } else { //read quote over if((1 == pObj->m_quoteFetcher.GetFlagQuoteReadOver())) { if(true == pObj->m_bQuoteOver) { errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return; } printf_green("read quote over\n"); pObj->m_bQuoteOver = true; pObj->m_quoteFetcher.Terminate(); memset(sendBuf,0,sizeof(sendBuf)); memset(&stHead,0,sizeof(stHead)); //send msg to ATF ResultOfSendQuote result; result.isSucc = true; pSendBuf = sendBuf; stHead.iMsgID = htons(MSGID_C2S_QUOTE_RESULT_SEND_QUOTE); stHead.iMsgBodyLen = htons(sizeof(ResultOfSendQuote)); memcpy(pSendBuf , &stHead, sizeof(stHead)); memcpy(pSendBuf+sizeof(stHead),&result,sizeof(result)); iSendLen = sizeof(stHead) + sizeof(result); ret = TcpSendData(pObj->m_fdConnQuoteSvr,(char*)pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[MSGID_C2S_QUOTE_RESULT_SEND_QUOTE]TCP send error,expect %dB,actual %dB.\n",iSendLen,ret); errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return ; } g_log.debug("[MSGID_C2S_QUOTE_RESULT_SEND_QUOTE]TCP send %dB \n",ret); } else { g_log.error("quote pop error!\n"); } } errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return ; }
int main() { #ifndef _POSIX_REALTIME_SIGNALS printf("_POSIX_REALTIME_SIGNALS is not defined\n"); return PTS_UNTESTED; #endif sigset_t set; struct sigevent ev; timer_t tid; struct itimerspec its; struct timespec tssleep, tsres; int overruns; int valuensec, intervalnsec, expectedoverruns; int fudge; if (sigemptyset(&set) != 0) { perror("sigemptyset() did not return success\n"); return PTS_UNRESOLVED; } if (sigaddset(&set, SIGCONT) != 0) { perror("sigaddset() did not return success\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGCONT; /* * create first timer */ if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } if (clock_getres(CLOCK_REALTIME, &tsres) != 0) { perror("clock_gettime() did not return success\n"); return PTS_UNRESOLVED; } if (tsres.tv_sec != 0) { printf("Clock resolution in seconds, not nsecs. Exiting.\n"); return PTS_UNRESOLVED; } valuensec = tsres.tv_nsec; intervalnsec = 2 * valuensec; //expectedoverruns = (1000000000 - valuensec) / intervalnsec; expectedoverruns = 1000000000 / intervalnsec - 1; printf("value = %d sec, interval = %d nsec, " "expected overruns = %d\n", 1, intervalnsec, expectedoverruns); its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = intervalnsec; its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0; //its.it_value.tv_sec = 0; //its.it_value.tv_nsec = valuensec; if (timer_settime(tid, 0, &its, NULL) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } //tssleep.tv_nsec = valuensec + (expectedoverruns*intervalnsec); tssleep.tv_nsec = 0; tssleep.tv_sec = 2; if (nanosleep(&tssleep, NULL) != 0) { perror("nanosleep() did not return success\n"); return PTS_UNRESOLVED; } /* * Since the overrun count is only meaningful with respect * to a particular timer expiry disable the timer before * un-blocking the signal. This ensures that there is only * one expiry and it should have a meaningful overrun count. */ //its.it_interval.tv_sec = 0; //its.it_interval.tv_nsec = 0; //its.it_value.tv_sec = 0; //its.it_value.tv_nsec = 0; //if (timer_settime(tid, 0, &its, NULL) != 0) { // perror("timer_settime() did not return success\n"); // return PTS_UNRESOLVED; //} if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } overruns = timer_getoverrun(tid); printf("%d overruns occurred\n", overruns); /* * Depending on the clock resolution we may have a few * extra expiries after the nanosleep completes so do * a range check. */ fudge = expectedoverruns / 100; if (overruns >= expectedoverruns && overruns < expectedoverruns + fudge) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("FAIL: %d overruns sent; expected %d\n", overruns, expectedoverruns); return PTS_FAIL; } printf("UNRESOLVED: This code should not be executed.\n"); return PTS_UNRESOLVED; }
int main() { sigset_t set; struct sigevent ev; timer_t tid; struct itimerspec its; struct timespec ts, tsres; int overruns; if (sigemptyset(&set) != 0) { perror("sigemptyset() did not return success\n"); return PTS_UNRESOLVED; } if (sigaddset(&set, SIGCONT) != 0) { perror("sigaddset() did not return success\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGCONT; /* * create first timer */ if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } if (clock_getres(CLOCK_REALTIME, &tsres) != 0) { perror("clock_gettime() did not return success\n"); return PTS_UNRESOLVED; } if (tsres.tv_sec != 0) { printf("Clock resolution in seconds, not nsecs. Exiting.\n"); return PTS_UNRESOLVED; } its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = tsres.tv_nsec; its.it_value.tv_sec = 0; its.it_value.tv_nsec = VALUENSEC; if (timer_settime(tid, 0, &its, NULL) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } ts.tv_nsec = VALUENSEC + ((EXPECTEDOVERRUNS) * its.it_interval.tv_nsec); ts.tv_sec = 0; if (nanosleep(&ts, NULL) != 0) { perror("nanosleep() did not return success\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } overruns = timer_getoverrun(tid); if (overruns > EXPECTEDOVERRUNS - 5) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("FAIL: %d overruns sent; expected %d\n", overruns, EXPECTEDOVERRUNS); return PTS_FAIL; } }
int main() { struct sigevent ev; struct sigaction act; timer_t tid; struct itimerspec its; int overruns; sigset_t set; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGTOTEST; act.sa_handler=handler; act.sa_flags=0; /* * set up handler for SIGTOTEST */ if (sigemptyset(&act.sa_mask) != 0) { perror("sigemptyset() did not return success\n"); return PTS_UNRESOLVED; } if (sigaction(SIGTOTEST, &act, 0) != 0) { perror("sigaction() did not return success\n"); return PTS_UNRESOLVED; } /* * set up timer to send SIGTOTEST */ if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } its.it_interval.tv_sec = TIMERINTERVAL; its.it_interval.tv_nsec = 0; its.it_value.tv_sec = TIMERVAL; its.it_value.tv_nsec = 0; if (timer_settime(tid, 0, &its, NULL) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } /* * block signal SIGTOTEST */ if (sigemptyset(&set) != 0) { perror("sigemptyset() did not return success\n"); return PTS_UNRESOLVED; } if (sigaddset(&set, SIGTOTEST) != 0) { perror("sigaddset() did not return success\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } if (sleep(2*TIMERINTERVAL + TIMERVAL) != 0) { perror("Could not sleep for correct amount of time\n"); return PTS_UNRESOLVED; } if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { perror("sigprocmask() did not return success\n"); return PTS_UNRESOLVED; } overruns = timer_getoverrun(tid); printf("Total overruns: %d\n", overruns); if (1 == madeit) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("FAIL: %d signals sent\n", madeit); return PTS_FAIL; } printf("UNRESOLVED: This code should not be executed.\n"); return PTS_UNRESOLVED; }
int main(int argc, char **argv) { sigset_t la_de_tim; struct sigaction accion; int i; int over; struct timespec interv = { 1, 0}; /* Un segundo */ struct timespec parada = { 10, 500000000L}; /* 10.5 segundos */ struct timespec doscmseg = { 0, 200000000L}; /* 200 milisegundos */ struct timespec cero = { 0, 0}; struct timespec ahora; struct timespec antes; struct timespec inicial; struct timespec resolu; struct itimerspec tempo; int res; float media; float intrv; timer_t mitimer; struct sigevent evento; siginfo_t info; sigset_t launo; /* Observando caracteristicas del reloj */ clock_getres(CLOCK_REALTIME, &resolu); printf("El reloj tiene resolucion de %ld seg %ld nanoseg\n", (long)resolu.tv_sec, (long)resolu.tv_nsec); /* Probando el reloj con "sleep" */ media = 0.; res = clock_gettime(CLOCK_REALTIME, &antes); inicial = antes; for(i=0; i<NTST; i++) { res = sleep(1); clock_gettime(CLOCK_REALTIME, &ahora); intrv = lapso(antes, ahora); /* Perdida de tiempo intermedia */ nanosleep(&doscmseg, NULL); /* Trazadores */ /* Se ignoran los 2 primeros datos */ media += intrv; printf("Intervalo %f sg. Total %f sg. Desv. desde comienzo: %f sg.\n", intrv, lapso(inicial, ahora), lapso(inicial, ahora) - (double)(i+1)); antes = ahora; } printf("Media de intervalos %f\n", media/NTST); /* Probando temporizadores */ /* Primero, programados para un solo disparo */ printf("probando temporizadores de un solo disparo\n"); tempo.it_value = interv; tempo.it_interval = cero; /* Le asocio la segnal SIGRTMIN */ /* 1: La programo de tiempo real */ sigemptyset(&accion.sa_mask); accion.sa_flags = SA_SIGINFO; accion.sa_sigaction = manejador; sigaction(SIGRTMIN, &accion, NULL); /* 2: La bloqueo en la mascara de proceso */ sigemptyset(&la_de_tim); sigaddset(&la_de_tim, SIGRTMIN); sigprocmask(SIG_BLOCK, &la_de_tim, NULL); /* 3: Preparo sigevent para ponerla como evento de temporizador */ evento.sigev_signo = SIGRTMIN; evento.sigev_notify = SIGEV_SIGNAL; evento.sigev_value.sival_int = 77; /* Creo el temporizador */ i = timer_create(CLOCK_REALTIME, &evento, &mitimer); if(i==-1) printf("error en timer_create\n"); /* Lo programo de un disparo */ i = timer_settime(mitimer, 0, &tempo, NULL); /* Espero la segnal sincronamente */ sigemptyset(&launo); sigaddset(&launo, SIGRTMIN); sigwaitinfo(&launo, &info); printf("vencido temporizador de un disp.; valor de la segnal: %d\n", info.si_value.sival_int); timer_delete(mitimer); /* Ahora ciclico */ printf("probando temporizadores ciclicos con intervalo de 1 segundo\n"); tempo.it_interval = interv; /* Le cambio el valor de entrega, solo por probar */ evento.sigev_value.sival_int = 88; timer_create(CLOCK_REALTIME, &evento, &mitimer); timer_settime(mitimer, 0, &tempo, NULL); timer_gettime(mitimer, &tempo); printf("Valor programado: %ld sg % ld nsg\n", (long)tempo.it_interval.tv_sec, tempo.it_interval.tv_nsec); /* Espero la segnal */ /* Se activa varias veces sin que se detecte segnal -> overrun */ nanosleep(¶da, NULL); /* Ahora se esperan las senales generadas y se mide el intervalo */ media = 0.; clock_gettime(CLOCK_REALTIME, &antes); for(i=0; i<NTST1; i++) { sigwaitinfo(&launo, &info); clock_gettime(CLOCK_REALTIME, &ahora); over = timer_getoverrun(mitimer); intrv = lapso(antes, ahora); /* Perdida de tiempo intermedia */ nanosleep(&doscmseg, NULL); /* Trazadores */ if(i==2) inicial = ahora; if(i>1) /* Se ignoran los 2 primeros, que son anomalos */ { if(over > 0) printf("Overrun de %d!\n", over); media += intrv; printf("Intervalo %f seg. Total de %f seg. Desv. desde el com.: %f\n", intrv, lapso(inicial, ahora), lapso(inicial, ahora) - (i-2)); } else { printf("Intervalo %f, ovr.: %d, valor de segnal: %d\n", intrv, over, info.si_value.sival_int); } antes = ahora; } printf("Media de intervalos %f\n", media/(NTST1-2)); timer_delete(mitimer); }
int main(int argc, char *argv[]) { struct sigevent ev; struct sigaction act; struct timespec tpT0, tpclock, tpreset; struct itimerspec its; timer_t tid; int flags = 0, overruns; /* * set up sigevent for timer * set up sigaction to catch signal */ ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGTOTEST; act.sa_handler=handler; act.sa_flags=0; if (sigemptyset(&act.sa_mask) != 0) { perror("sigemptyset() was not successful\n"); return PTS_UNRESOLVED; } if (sigaction(SIGTOTEST, &act, 0) != 0) { perror("sigaction() was not successful\n"); return PTS_UNRESOLVED; } if (clock_gettime(CLOCK_REALTIME, &tpT0) != 0) { perror("clock_gettime() was not successful\n"); return PTS_UNRESOLVED; } if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } flags |= TIMER_ABSTIME; its.it_interval.tv_sec = TIMERINTERVAL; its.it_interval.tv_nsec = 0; its.it_value.tv_sec = tpT0.tv_sec + TIMEROFFSET; its.it_value.tv_nsec = tpT0.tv_nsec; if (timer_settime(tid, flags, &its, NULL) != 0) { perror("timer_settime() did not return success\n"); return PTS_UNRESOLVED; } tpclock.tv_sec = its.it_value.tv_sec + EXPECTEDOVERRUNS*TIMERINTERVAL; tpclock.tv_nsec = its.it_value.tv_nsec; getBeforeTime(&tpreset); if (clock_settime(CLOCK_REALTIME, &tpclock) != 0) { printf("clock_settime() was not successful\n"); return PTS_UNRESOLVED; } sleep(SHORTTIME); overruns = timer_getoverrun(tid); if (overruns == EXPECTEDOVERRUNS) { printf("Overrun count == # of repeating timer expirys\n"); } else { printf("Overrun count =%d, not # of repeating timer expirys\n", overruns); } tpreset.tv_sec += SHORTTIME; setBackTime(tpreset); if (caught == 1) { printf("Caught the correct number of signals\n"); } else { printf("FAIL: Caught %d signals, not 1\n", caught); printf("Test FAILED\n"); return PTS_FAIL; } printf("Test PASSED\n"); return PTS_PASS; }