Manual Pages for Linux CentOS command on man timer_create
MyWebUniversity

Manual Pages for Linux CentOS command on man timer_create

TIMERCREATE(2) Linux Programmer's Manual TIMERCREATE(2)

NAME

timercreate - create a POSIX per-process timer SYNOPSIS

#include

#include int timercreate(clockidt clockid, struct sigevent *sevp, timert *timerid);

Link with -lrt. Feature Test Macro Requirements for glibc (see featuretestmacros(7)): timercreate(): POSIXCSOURCE >= 199309L DESCRIPTION

timercreate() creates a new per-process interval timer. The ID of the new timer is returned in the buffer pointed to by timerid, which must

be a non-NULL pointer. This ID is unique within the process, until the timer is deleted. The new timer is initially disarmed. The clockid argument specifies the clock that the new timer uses to measure time. It can be specified as one of the following values: CLOCKREALTIME

A settable system-wide real-time clock. CLOCKMONOTONIC A nonsettable monotonically increasing clock that measures time from some unspecified point in the past that does not change after system startup. CLOCKPROCESSCPUTIMEID (since Linux 2.6.12) A clock that measures (user and system) CPU time consumed by (all of the threads in) the calling process. CLOCKTHREADCPUTIMEID (since Linux 2.6.12) A clock that measures (user and system) CPU time consumed by the calling thread. As well as the above values, clockid can be specified as the clockid returned by a call to clockgetcpuclockid(3) or pthreadgetcpu‐ clockid(3). The sevp argument points to a sigevent structure that specifies how the caller should be notified when the timer expires. For the definition and general details of this structure, see sigevent(7). The sevp.sigevnotify field can have the following values: SIGEVNONE Don't asynchronously notify when the timer expires. Progress of the timer can be monitored using timergettime(2). SIGEVSIGNAL Upon timer expiration, generate the signal sigevsigno for the process. See sigevent(7) for general details. The sicode field of the siginfot structure will be set to SITIMER. At any point in time, at most one signal is queued to the process for a given timer; see timergetoverrun(2) for more details. SIGEVTHREAD Upon timer expiration, invoke sigevnotifyfunction as if it were the start function of a new thread. See sigevent(7) for details.

SIGEVTHREADID (Linux-specific) As for SIGEVSIGNAL, but the signal is targeted at the thread whose ID is given in sigevnotifythreadid, which must be a thread in the same process as the caller. The sigevnotifythreadid field specifies a kernel thread ID, that is, the value returned by clone(2) or gettid(2). This flag is intended only for use by threading libraries. Specifying sevp as NULL is equivalent to specifying a pointer to a sigevent structure in which sigevnotify is SIGEVSIGNAL, sigevsigno is SIGALRM, and sigevvalue.sivalint is the timer ID. RETURN VALUE On success, timercreate() returns 0, and the ID of the new timer is

placed in *timerid. On failure, -1 is returned, and errno is set to indicate the error. ERRORS EAGAIN Temporary error during kernel allocation of timer structures. EINVAL Clock ID, sigevnotify, sigevsigno, or sigevnotifythreadid is invalid. ENOMEM Could not allocate memory. VERSIONS This system call is available since Linux 2.6. CONFORMING TO

POSIX.1-2001. NOTES A program may create multiple interval timers using timercreate(). Timers are not inherited by the child of a fork(2), and are disarmed and deleted during an execve(2).

The kernel preallocates a "queued real-time signal" for each timer cre‐ ated using timercreate(). Consequently, the number of timers is lim‐ ited by the RLIMITSIGPENDING resource limit (see setrlimit(2)). The timers created by timercreate() are commonly known as "POSIX (interval) timers". The POSIX timers API consists of the following interfaces: * timercreate(): Create a timer. * timersettime(2): Arm (start) or disarm (stop) a timer. * timergettime(2): Fetch the time remaining until the next expiration of a timer, along with the interval setting of the timer. * timergetoverrun(2): Return the overrun count for the last timer expiration. * timerdelete(2): Disarm and delete a timer. Part of the implementation of the POSIX timers API is provided by glibc. In particular: * The functionality for SIGEVTHREAD is implemented within glibc, rather than the kernel. * The timer IDs presented at user level are maintained by glibc, which maps these IDs to the timer IDs employed by the kernel. The POSIX timers system calls first appeared in Linux 2.6. Prior to

this, glibc provided an incomplete user-space implementation (CLOCKREALTIME timers only) using POSIX threads, and current glibc

falls back to this implementation on systems running pre-2.6 Linux ker‐ nels. EXAMPLE The program below takes two arguments: a sleep period in seconds, and a timer frequency in nanoseconds. The program establishes a handler for the signal it uses for the timer, blocks that signal, creates and arms a timer that expires with the given frequency, sleeps for the specified number of seconds, and then unblocks the timer signal. Assuming that the timer expired at least once while the program slept, the signal handler will be invoked, and the handler displays some information about the timer notification. The program terminates after one invoca‐ tion of the signal handler. In the following example run, the program sleeps for 1 second, after creating a timer that has a frequency of 100 nanoseconds. By the time the signal is unblocked and delivered, there have been around ten mil‐ lion overruns.

$ ./a.out 1 100 Establishing handler for signal 34 Blocking signal 34 timer ID is 0x804c008 Sleeping for 1 seconds Unblocking signal 34 Caught signal 34 sivalptr = 0xbfb174f4; *sivalptr = 0x804c008 overrun count = 10004886 Program source

#include

#include

#include

#include

#include

#define CLOCKID CLOCKREALTIME

#define SIG SIGRTMIN

#define errExit(msg) do { perror(msg); exit(EXITFAILURE); \ } while (0) static void printsiginfo(siginfot *si) { timert *tidp; int or;

tidp = si->sivalue.sivalptr;

printf(" sivalptr = %p; ", si->sivalue.sivalptr);

printf(" *sivalptr = 0x%lx\n", (long) *tidp); or = timergetoverrun(*tidp);

if (or == -1) errExit("timergetoverrun"); else

printf(" overrun count = %d\n", or); } static void handler(int sig, siginfot *si, void *uc) { /* Note: calling printf() from a signal handler is not

strictly correct, since printf() is not async-signal-safe; see signal(7) */

printf("Caught signal %d\n", sig); printsiginfo(si); signal(sig, SIGIGN); } int main(int argc, char *argv[]) { timert timerid; struct sigevent sev; struct itimerspec its; long long freqnanosecs; sigsett mask; struct sigaction sa; if (argc != 3) {

fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXITFAILURE); } /* Establish handler for timer signal */

printf("Establishing handler for signal %d\n", SIG); sa.saflags = SASIGINFO; sa.sasigaction = handler; sigemptyset(&sa.samask);

if (sigaction(SIG, &sa, NULL) == -1) errExit("sigaction"); /* Block timer signal temporarily */

printf("Blocking signal %d\n", SIG); sigemptyset(&mask); sigaddset(&mask, SIG);

if (sigprocmask(SIGSETMASK, &mask, NULL) == -1) errExit("sigprocmask"); /* Create the timer */ sev.sigevnotify = SIGEVSIGNAL; sev.sigevsigno = SIG; sev.sigevvalue.sivalptr = &timerid;

if (timercreate(CLOCKID, &sev, &timerid) == -1) errExit("timercreate");

printf("timer ID is 0x%lx\n", (long) timerid); /* Start the timer */ freqnanosecs = atoll(argv[2]); its.itvalue.tvsec = freqnanosecs / 1000000000;

its.itvalue.tvnsec = freqnanosecs % 1000000000; its.itinterval.tvsec = its.itvalue.tvsec; its.itinterval.tvnsec = its.itvalue.tvnsec;

if (timersettime(timerid, 0, &its, NULL) == -1) errExit("timersettime"); /* Sleep for a while; meanwhile, the timer may expire multiple times */

printf("Sleeping for %d seconds\n", atoi(argv[1])); sleep(atoi(argv[1])); /* Unlock the timer signal, so that timer notification can be delivered */

printf("Unblocking signal %d\n", SIG);

if (sigprocmask(SIGUNBLOCK, &mask, NULL) == -1) errExit("sigprocmask"); exit(EXITSUCCESS); } SEE ALSO clockgettime(2), setitimer(2), timerdelete(2), timergetoverrun(2), timersettime(2), timerfdcreate(2), clockgetcpuclockid(3), pthreadgetcpuclockid(3), pthreads(7), sigevent(7), signal(7), time(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2010-09-27 TIMERCREATE(2)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™