9.3 POSIX:XSI Interval TimersA timer generates a notification after a specified amount of time has elapsed. In contrast to a clock, which increments to track the passage of time, a timer usually decrements its value and generates a signal when the value becomes zero. A computer system typically has a small number of hardware interval timers, and the operating system implements multiple software timers by using these hardware timers.Operating systems use interval timers in many ways. An interval timer can cause a periodic interrupt, triggering the operating system to increment a counter. This counter can keep the time since the operating system was booted. UNIX systems traditionally keep the time of day as the number of seconds since January 1, 1970. If an underlying interval timer generates an interrupt after 100 microseconds and is restarted each time it expires, the timer interrupt service routine can keep a local counter to measure the number of seconds since January 1, 1970, by incrementing this local counter after each 10,000 expirations of the interval timer. Program 9.6 nanotest.cA function that tests the resolution of nanosleep.
Time-sharing operating systems can also use interval timers for process scheduling. When the operating system schedules a process, it starts an interval timer for a time interval called the scheduling quantum. If this timer expires and the process is still executing, the scheduler moves the process to a ready queue so that another process can execute. Multiprocessor systems need one of these interval timers for each processor.Most scheduling algorithms have a mechanism for raising the priority of processes that have been waiting a long time to execute. The scheduler might use an interval timer for priority management. Every time the timer expires, the scheduler raises the priority of the processes that have not executed.The interval timers of the POSIX:XSI Extension use a struct itimerval structure that contains the following members.
Here it_value holds the time remaining before the timer expires, and it_interval holds the time interval to be used for resetting the timer after it expires. Recall that a struct timeval structure has fields for seconds and microseconds.A conforming POSIX:XSI implementation must provide each process with the following three user interval timers.
If successful, these functions return 0. If unsuccessful, they return 1 and set errno. The setitimer function sets errno to EINVAL if the number of microseconds in value is not in the range [0, 106).If the it_interval member of *value is not 0, the timer restarts with this value when it expires. If the it_interval of *value is 0, the timer does not restart after it expires. If the it_value of *value is 0, setitimer stops the timer if it is running.Program 9.7 uses an ITIMER_PROF timer to print out an asterisk for each two seconds of CPU time used. The program first calls setupinterrupt to install myhandler as the signal handler for SIGPROF. Then, the program calls setupitimer to set up a periodic timer, using ITIMER_PROF, that expires every 2 seconds. The ITIMER_PROF timer generates a SIGPROF signal after every two seconds of CPU time used by the process. The process catches the SIGPROF signal and handles it with myhandler. This handler function outputs an asterisk to standard error. Program 9.7 periodicasterisk.cA program that prints an asterisk for each two seconds of CPU time used.
Exercise 9.11Write a program that sets ITIMER_REAL to expire in two seconds and then sleeps for ten seconds. How long does it take for the program to terminate? Why?Answer:POSIX states that the interaction between setitimer and any of alarm, sleep or usleep is unspecified, so we can't predict how long it will take. Avoid this combination in your programs by using nanosleep instead of sleep. Exercise 9.12What is wrong with the following code, which should print out the number of seconds remaining on the ITIMER_VIRTUAL interval timer?
Answer:Although the variable value is declared as a pointer to a struct itimerval structure, it does not point to anything. That is, there is no declaration of an actual struct itimerval structure that value represents.Program 9.1, uses virtual time. Remember that the value returned by getitimer is the time remaining, so the quantity is decreasing. Exercise 9.13How can you modify Program 9.8 to compensate for the overhead of calling setitimer and getitimer?Answer:Call the setitimer and getitimer pair with no intervening statements and use the time difference as an estimate of the timing overhead. Exercise 9.14What happens if we replace the final return in Program 9.8 with the infinite loop for( ; ; );?Answer:After using one million seconds of virtual time, the program receives a SIGVTALRM signal and terminates. One million seconds is approximately 12 days. Program 9.8 xsitimer.cA program that uses a POSIX:XSI interval timer to measure the execution time of a function.
|