290 likes | 422 Views
Time Management. Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing systems. These services include: Keeping track of the time of day, and current date
E N D
Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing systems. • These services include: • Keeping track of the time of day, and current date • Measuring the time used by various activities and processes, such as keeping one process from monopolizing the CPU. • Starting and stopping activities at specific times • Time management centers around processing interrupts generated by hardware clocks.
A hardware clock is: • Is a physical device which measures time. • Causes periodic interrupts either at specific intervals, or upon the expiration of a preset time interval, and may need to be reset • Control of clocks can be provided via software using a clock driver, interrupt handlers, and timer management routines. • Since it is impossible to have a physical clock for each type of event that must be timed, multiple software/virtual clocks can be based on the same physical clock.
Physical clocks • Physical clocks generate precisely timed electronic pulses that synchronize the other components of the computer system, by periodically generating interrupts at a KNOWN frequency. • Line clocks:Simplest type of clock. They interrupt at a frequency that is based on that of the electric power line voltage cycle on which the computer is operating. In the US this is 50 or 60 Hertz, and the line clock would generate an interrupt every 1/60th of a second. A clock driver can then be used to interpret this interrupt and update software clocks appropriately.
Physical clocks • Programmable clocks: These clocks have 3 components: a crystal oscillator, a counter and a holding register. The oscillator generates a periodic signal, which is fed to the counter to make it count down to zero. When the value contained in this register reaches 0 then an interrupt is generated. • Programmable clocks typically have multiple modes of operation and can either interrupt once (one-shot mode) and stop until explicitly started again by software , or in (square wave mode) where after getting to zero and interrupting the contents of the holding register is automatically copied back into the counter register. The frequency of the interrupts for this type of clock can be set by software by setting the contents of the counter. • Several OS services are based on the predictable beat of a clock tick, such as the beginning of the next fetch/execute cycle. • Multiple software clocks can be defined for a single hardware clock to provide specific services: Time of Day, etc.
Clock Drivers • Clock interrupts are processed by software not unlike device drivers. • Service routines to read and modify the value or behavior of both hardware and virtual/software clocks. Synchronizing system and backup clocks. • Deal with interrupts that represent clock ticks. Interpret the clock ticks to update software clocks
Time of Day services • One of the responsibility of time management is to provide date and time of day services! • To do this the system must record the current date and time and provide this information (in a variety of ways) upon request.
Clock_Interrupt: save registers increment Time_Of_Day_Clock IF Time_Of_Day_Clock > 24 hours THEN set Time_of_Day_Clock to 0 advance date ENDIF restore registers RETURN from interrupt Based on the # of clock ticks in 1 day
Clock_Interrupt: save registers increment #_of_ticks_Counter IF #_of_ticks_Counter > 1 second THEN Set #_of_ticks_counter to 0 Advance second_counter If second_counter > 24 hours then Set second_counter to 0 Advance date Endif endif restore registers RETURN from interrupt Based on the # of clock ticks in 1 second
When the current date or time is requested the time is translated into the number of seconds since 12AM, on Jan 1, 1970 for Unix, or Jan 1980. • This is translated into the current date and time. • Usually a separate counter is maintained for the current date, which is then incremented when a full 24 hours has past.
Other types of services • providing "watchdog" or monitoring timers for systems processes and expected events. (such as a wakeup call that is set each time a hard disk controller is sent a command. If the command fails, the expiration of the timer will indicate the problem. ) • providing suspension of a process for a specified interval (pause for 30 minutes) • providing suspension of a process until a specific time of day • providing for the startup of a process at a specific time of day • providing for timing of expected events and asynchronous execution of procedures upon expiration of the interval (timeouts) • providing for cancellation of previous timer requests • Accounting for CPU usage
Timer Queues • Like a process queue, a timer queue is implemented to keep track of timing requests generated either by individual processes or the OS itself • This queue holds a set of logical timers (software clocks0) allocated to processes or system services. When the time in these logical timers expires an event is triggered. • These system must keep track of these timers, and one solution is to maintain an ordered linked list of timing requests
Contents of the timer queue element: • Pointers to the next (and previous element) • Time quantum • Link to the PCB of requesting process • Address of a procedure to call (representing the action to perform) • Type of request (user or OS)
With each clock interrupt the clock driver has several things to do: incrementing the real time, decrementing the time quantum in timer requests, check for zero (any timer elements who’s counter has reached zero, and doing CPU accounting. • It is vital that each of these operations be arranged so that they are very fast, since they have to be repeated many times a second. • Let’s consider a timer queue….There are several ways the time stored in the first element can be manipulated….
F D A C E B * * * * * * 8450 8467 8453 8437 8457 8442 Time stored as a “real” time the event is to occur Front of queue Current time 8430 PCB When the current time matches the “time” stored in the first timer element, the event associated with that element is performed, a timer element will also contain a link to a PCB associated with the event, and an address of a routine that is to be executed to perform the event.
TIMER “Q” SEARCH: SET P TO THE TOP_TQE (timer queue element) DO WHILE TIME_OF_DAY_CLOCK >= P->TQE_TOD SCHEDULE ACTION OF P->TQE SET P TO NEXT TQE ENDWHILE SET TOP_TQE POINTER TO TQE POINTED TO BY P RETURN
Front of queue A * 7 B * 5 C * 8 D * 3 E * 4 F * 10 Storing Intervals in elements Current time 8430 Request for 8437, 8430+7 Request for 8442, 8437+5 The event associated with the first TQE is to occur in 7 ticks from the current time. Time stored in later elements as based on the elements in front of it. Request for 8450
Front of queue Current time 8430 Request for 8437, 8430+7 A * 7 B * 0 If multiple events are to occur at the same time, the time value of subsequent elements is zero. C * 0 Request for 8440, 8437+3 D * 3 E * 4 F * 10
Programmable clock Programmable clock Timer Queue Head Timer Queue Head 685 730 A A * * 730 730 B B * * 405 405 Time can be stored in a programmable clock # of clock ticks until the event is to be triggered The time stored in the programmable clock would be decremented based on the rate of that clock.
Adding new elements into the timer queue can require adjustments to other TQEs …
Programmable clock Programmable clock Timer Queue Head 457 685 A * 730 B * 405 A C * * 457 228 A new request arrives for a time interval of 457 Timer Queue Head Timer interval in A reset to the time remaining in the programmable clock, minus the time interval of the new event. B * 405
Adding new timing requests • Determining the time interval for the new element • Searching the queue until an element is found whose event should occur after the time interval for the new element. • Inserting the new element into the queue • Adjusting the time interval of the element, in front of which the new element has been inserted.
8 18 7 5 10 3 4 A B G C D E F Current time= 8430 Timerq_head For a queue where each element contains a time that is an interval from the current time. Element C is to occur in 20 ticks…
Current time= 8430 Timerq_ head 5 2 (8) 6 7 4 10 3 A B G C D E F We insert G between elements b and c and adjust times in later elements to reflect the addition
3 5 10 4 6 2 7 10 5 4 A B G C D D F E E F Deleting element C Current time= 8430 Timerq_ head Deleting elements also require adjustments. When an element is deleted the times in later elements must be increased.
Timing issues: • The time spent processing a clock interrupt is critical to the accuracy of the timing facility within an OS. • We must update the hardware clock as quickly as possible • Must be the highest priority interrupt if clock ticks are to be counted accurately. • Ability to block all but clock interrupts will allow critical work to be done, but still maintaining the accuracy of the clock