Similarly, settimer sets the timer with the parameter new_value. The call gettimer gets the value of the timer in the parameter curr_value. The first parameter, which, is one of the timers, ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF. If both, it_value and it_interval are zero, the timer stops. After generating the alarm, the it_value is set to the timer reset value, it_interval. When it_value, that is, both the members tv_sec and tv_usec, become zero, the relevant alarm is generated. Int setitimer (int which, const struct itimerval *new_value, Int getitimer (int which, struct itimerval *curr_value) Struct timeval it_value /* current value */ Struct timeval it_interval /* next value */ When it becomes zero, signal SIGPROF is sent to the process. ITIMER_PROF - ITIMER_PROF decrements when the process is executing in the user mode and also when the system is running on behalf of the process.When it becomes zero, the SIGVTALRM signal is sent to the process. That is, it decrements when the process is executing in the user mode. ITIMER_VIRTUAL - ITIMER_VIRTUAL decrements in the process's virtual time.When it becomes zero, the SIGALRM signal is sent to the process. ITIMER_REAL - ITIMER_REAL decrements in real (calendar) time.
There are three interval timers for a process, Once set, the timers decrement and when a timer becomes zero, a specific alarm is sent to the process. There are two high resolution timer interfaces in Linux, the Interval Timer (itimer) interface and the POSIX timers interface. Get the clock resolution using the clock_getres system call.
resolution entry showing 1 nanosecond and event_handler as hrtimer_interrupt in /proc/timer_list indicate that high resolution timers are available.
The number of jiffies in a second, or the frequency of the system tick, is defined by the HZ constant. In the Main Timing subsystem, the smallest time maintained is denoted by a jiffy, which is the duration of a tick of the system timer interrupt. There are two timing sub-systems in Linux, the Main Timing subsystem and the High Resolution Timing subsystem. In nanosleep, there is no restriction on the amount of time a thread goes on to sleep. The unslept time is returned in the timespec struct pointed by rem. The thread wakes up when the time is over or the sleep is interrupted by a signal which has not been ignored. With the nanosleep call, the calling thread goes to sleep as per the data pointed by req. Int nanosleep (const struct timespec *req, struct timespec *rem) nanosleep uses the struct timespec, which is, That is, the calling process can only sleep for less than a second.įinally, there is the nanosleep system call, which provides the high resolution sleep. The usleep function is similar to sleep, except that the argument usec is in microseconds. sleep can easily be implemented as a function using SIGALRM. So sleep and SIGALRM should not be used in the same process. Sleep might have been implemented using SIGALRM.
The returned value is the unslept seconds. The process wakes up when s seconds are over or a signal, which is not ignored, is received. With the sleep function, a process can sleep for s seconds. Alarm returns the time remaining in seconds of any previously set alarm, or 0 if there was no alarm set previously. So, passing 0 as argument to alarm clears any previously set alarm. The alarm system call arranges for the SIGALRM signal to be sent to the calling process after s seconds.Īny previously set alarm is cleared.