April 24, 2006
Coverage: [USP] Chapter 9
Quicktime Warm-Up :-)
- review of cpufraction.c
- review of timechild.c
- why don't we get the same result
when we pass it ls as we do with the time shell command?
- here we always get 0 clock ticks
Don't Get Caught sleeping
- sleep function
- this is old hat by now
- takes seconds and blocks the calling thread
until time up or signal caught
- interacts with SIGALRM
- therefore, avoid using them in the same process
- beeper.c: beeps every n seconds
- nanosleep function
- causes calling thread to block until time
interval in rqtp (struct timespec) expired or until
the thread receives a signal
- allows nanosecond resolution
- not affected by SIGALRM
- nanotest.c: tests the resolution of nanosleep
POSIX:XSI Interval Timers
- a timer is the complement of a clock
- while a clock increments time, a timer decrements
its value and generates a signal when that value reaches
zero
- computers have a few hardware interval timers
- the operating system implements multiple software
timers using those hardware timers
- timers have a variety of uses, e.g,
- to keep track of time since last reboot
- for process scheduling (quantum)
- for increasing priority of a ready process
- struct itimerval (we have seen this before)
- struct timeval it_value: time remaining before expiration
- struct timeval it_interval: value to reload into timer
- POSIX:XSI provides each process the following three timers
- ITIMER_REAL: generates a SIGALRM
- ITIMER_VIRTUAL: generates a SIGVTALRM
- ITIMER_PROF: decrements in virtual time and
system time for the process and generates a SIGPROF
- functions for manipulating these interval timers
- getitimer: retrieves the current time interval from
a user interval timer
- setitimer: starts and stops a user interval timer
- periodicasterisk.c: prints an asterisk for each
two seconds of CPU time used
- uses an ITIMER_PROF
- uses a signal handler for SIGPROF to print an asterisk
- xsitimer.c: uses a POSIX:XSI interval timer
to measure the execution time of a function
Realtime Signals
- remember the limitations of signal handling?
- blocked signals of the same type are not queued
- handlers can only take the signal number as a parameter
- POSIX:XSI and POSIX:RTS Realtime Signal Extensions address
these limitations
- sa_sigaction member of sigaction
- alternative type of signal handler
- sa_flags must be nonzero
- handler must have the form: void func (int
signo, siginfo_t *info, void *context);
- siginfo_t is a struct containing
fields (e.g., int si_code) to store information
such as the cause of the signal
- SI_USER: kill, raise, abort
- SI_QUEUE: sigqueue
- SI_TIMER: timer expired
- SI_ASYNCIO: asynchronous I/O completed
- SI_MESGQ: arrival of a message
- sigqueue function
- extension to kill which permits signals to be queued
- examples:
- sendsigqueue.c: program which behaves like
kill, but sends a queued signal to a process
- sigqueuehandler.c
- program which receives SIGUSR1 signals and displays
their values
- notice the use of fprintf in handler
References
[USP] K. A. Robbins and S. Robbins. UNIX Systems Programming: Concurrency,
Communication, and Threads. Prentice Hall, 2003