Process Timers in Linux Operating System

Clocks and timers have been a part of UNIX systems from their very inception. Standard UNIX (POSIX.1) has three clocks. One tells the time and is directly accessible to the user. The other two have other purposes, which don’t often concern the application programmer. There are 2 types of timers: one shot timers and repeating ones. The former only expires once and is used to signal a special event that needs to occur only once. Repeating timers go off at regular intervals and are used to schedule periodic events.

There is, basically, only one clock which application programs use in Linux. The operating system keeps track of current date and time by storing the number of seconds that have elapsed since, midnight January 1, 1970 UTC. This period is also called an epoch. This date is considered as the informal -birthday’ of the UNIX operating system. The time is stored in a signed long integer. The time function can be used to know how much time in seconds has elapsed since the start of the epoch. The prototype for time call is: #include time_t time (time_t) *tloc);

In all versions of UNIX the time() system call may be used to obtain the time of the day. This call is peculiar in that if given the address of a long integer as an argument, it places the time in that integer and returns it. If, however, a null pointer is passed, the time of day is just returned. The time_t is convenient for calculating differences between times, but difficult to print dates. Several routines are available to convert the long integer returned by time() into an ASCII date string. With the UNIX operating system, an ASCII date string is a string as shown below: Day Mon dd hh: mm : ss yyyy

The ctime() library function can be used to do the above conversion. Its prototype is:

#include Char *ctime (const time_t *clock) Example: #include #include Int main () { long now, time (); struct tm *today, *localtime (); time ( today=localtime ( printf (-today is: %d/%d/%d \n-, today->tm_mon + 1, today->tm_mday, today->tm_year); exit(0); }

This time function measures real time or wall clock time. In a multiprogramming environment, we may be more interested in the amount of time an individual process uses. This is called virtual time. The system call tiems returns information about the execution times of a process and its children. One shot timers: the most common example of these kind of timers would be the sleep call. #include Unsigned sleep(unsigned seconds);

When a process executes sleep it blocks for the number of seconds specified. The sleep function can be used as a sort of crude interval timer. Standard UNIX timers: the standard UNIX interval timers defined in the Berkeley and SR4(1170) versions of UNIX are as follows – ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF

POSIX Interval timers: the 1170 specification timers do not provide sufficient flexibility to deal with real time tasks. To be useful in real-time environments, we need more than just three timers. There needs to a greater time resolution and allowance for determining timer overruns.

All in all, UNIX timers can be used to find out the CPU time requirements of a program for benchmarking. Interval timers are also extremely important to allow scheduled tasking.

To know more, click here