Time Management. Time management is concerned with OS facilities and services which measure real...

26
Time Management Time Management

Transcript of Time Management. Time management is concerned with OS facilities and services which measure real...

Page 1: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Time ManagementTime Management

Page 2: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Time management is concerned with OS facilities and Time management is concerned with OS facilities and services which measure services which measure real real time, and is essential to time, and is essential to the operation of timesharing systems.the operation of timesharing systems.

These services include:These services include: Keeping track of the time of day, and current dateKeeping track of the time of day, and current date

Measuring the time used by various activities and processes, Measuring the time used by various activities and processes, such as keeping one process from monopolizing the CPU.such as keeping one process from monopolizing the CPU.

Starting and stopping activities at specific timesStarting and stopping activities at specific times

Time management centers around processing interrupts Time management centers around processing interrupts generated by hardware clocks.generated by hardware clocks.

Page 3: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

A hardware clock is:A hardware clock is: Is a physical device which measures time.Is a physical device which measures time. Causes periodic interrupts either at specific Causes periodic interrupts either at specific

intervals, or upon the expiration of a preset intervals, or upon the expiration of a preset time interval, and may need to be resettime interval, and may need to be reset

Control of clocks can be provided via software Control of clocks can be provided via software using a clock driver, interrupt handlers, and using a clock driver, interrupt handlers, and timer management routines.timer management routines.

Since it is impossible to have a physical clock Since it is impossible to have a physical clock for each type of event that must be timed, for each type of event that must be timed, multiple software/virtual clocks can be based multiple software/virtual clocks can be based on the same physical clock.on the same physical clock.

Page 4: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Physical clocksPhysical clocks Physical clocks generate precisely timed Physical clocks generate precisely timed

electronic pulses that synchronize the other electronic pulses that synchronize the other components of the computer system, by components of the computer system, by periodically generating interrupts at a periodically generating interrupts at a KNOWN frequency.KNOWN frequency.

Line clocks:Line clocks: Simplest type of clock. They Simplest type of clock. They interrupt at a frequency that is based on that of interrupt at a frequency that is based on that of the electric power line voltage cycle on which the the electric power line voltage cycle on which the computer is operating. In the US this is 50 or 60 computer is operating. In the US this is 50 or 60 Hertz, and the line clock would generate an Hertz, and the line clock would generate an interrupt every 1/60th of a second. A clock interrupt every 1/60th of a second. A clock driver can then be used to interpret this interrupt driver can then be used to interpret this interrupt and update software clocks appropriately.and update software clocks appropriately.

Page 5: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Physical clocksPhysical clocks Programmable clocks:Programmable clocks: These clocks have 3 components: a crystal These clocks have 3 components: a crystal

oscillator, a counter and a holding register. The oscillator generates a 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. 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 When the value contained in this register reaches 0 then an interrupt is generated. generated.

Programmable clocks typically have multiple modes of operation and can Programmable clocks typically have multiple modes of operation and can either interrupt once (one-shot mode) and stop until explicitly started again 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 by software , or in (square wave mode) where after getting to zero and interrupting the contents of the holding register is automatically copied back interrupting the contents of the holding register is automatically copied back into the counter register. The frequency of the interrupts for this type of 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.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 Several OS services are based on the predictable beat of a clock tick, such as the beginning of the next fetch/execute cycle.as the beginning of the next fetch/execute cycle.

Multiple software clocks can be defined for a single hardware clock to Multiple software clocks can be defined for a single hardware clock to provide specific services: Time of Day, etc.provide specific services: Time of Day, etc.

Page 6: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Clock DriversClock Drivers

Clock interrupts are processed by Clock interrupts are processed by software not unlike device drivers.software not unlike device drivers.

Service routines to read and modify the value Service routines to read and modify the value or behavior of both hardware and or behavior of both hardware and virtual/software clocks. Synchronizing system virtual/software clocks. Synchronizing system and backup clocks.and backup clocks.

Deal with interrupts that represent clock ticks. Deal with interrupts that represent clock ticks. Interpret the clock ticks to update software Interpret the clock ticks to update software clocksclocks

Page 7: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Time of Day servicesTime of Day services

One of the responsibility of time One of the responsibility of time management is to provide date and time of management is to provide date and time of day services!day services!

To do this the system must record the To do this the system must record the current date and time and provide this current date and time and provide this information (in a variety of ways) upon information (in a variety of ways) upon request.request.

Page 8: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Clock_Interrupt: Clock_Interrupt:

save registers save registers 

increment Time_Of_Day_Clock increment Time_Of_Day_Clock 

IF IF Time_Of_Day_Clock > 24 hours Time_Of_Day_Clock > 24 hours THEN THEN 

set Time_of_Day_Clock to 0 set Time_of_Day_Clock to 0 

advance date advance date 

ENDIF ENDIF 

restore registers  restore registers  

RETURNRETURN from interrupt  from interrupt 

Based on the # of clock ticks in 1 dayBased on the # of clock ticks in 1 day

Page 9: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Clock_Interrupt: Clock_Interrupt: save registers save registers increment #_of_ticks_Counter increment #_of_ticks_Counter 

IF IF #_of_ticks_Counter  > 1 second #_of_ticks_Counter  > 1 second THEN THEN Set #_of_ticks_counter to 0Set #_of_ticks_counter to 0Advance second_counterAdvance second_counter

IfIf second_counter > 24 hours second_counter > 24 hours thenthenSet second_counter to 0Set second_counter to 0Advance dateAdvance date

EndifEndif

endifendifrestore registers  restore registers  RETURNRETURN from interrupt  from interrupt 

Based on the # of clock ticks in 1 secondBased on the # of clock ticks in 1 second

Page 10: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

When the current date or time is requested the When the current date or time is requested the time is translated into the number of seconds time is translated into the number of seconds since 12AM, on Jan 1, 1970 for Unix, or Jan since 12AM, on Jan 1, 1970 for Unix, or Jan 1980.1980.

This is translated into the current date and time.This is translated into the current date and time.

Usually a separate counter is maintained for the Usually a separate counter is maintained for the current date, which is then incremented when a current date, which is then incremented when a full 24 hours has past.full 24 hours has past.

Page 11: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Other types of servicesOther types of services providing "watchdog" or monitoring timers for systems processes providing "watchdog" or monitoring timers for systems processes

and expected events. and expected events. (such as a wakeup call that is set each time a (such as a wakeup call that is set each time a hard disk controller is sent a command. If the command fails, the hard disk controller is sent a command. If the command fails, the expiration of the timer will indicate the problem. )expiration of the timer will indicate the problem. )

providing suspension of a process for a specified interval providing suspension of a process for a specified interval (pause for (pause for 30 minutes)30 minutes)

providing suspension of a process until a specific time of day 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 the startup of a process at a specific time of day

providing for timing of expected events and asynchronous execution providing for timing of expected events and asynchronous execution of procedures upon expiration of the interval (timeouts) of procedures upon expiration of the interval (timeouts)

providing for cancellation of previous timer requests providing for cancellation of previous timer requests Accounting for CPU usageAccounting for CPU usage

Page 12: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Timer QueuesTimer Queues Like a process queue, a timer queue is Like a process queue, a timer queue is

implemented to keep track of timing requests implemented to keep track of timing requests generated either by individual processes or the generated either by individual processes or the OS itselfOS itself

This queue holds a set of logical timers This queue holds a set of logical timers (software clocks0) allocated to processes or (software clocks0) allocated to processes or system services. When the time in these logical system services. When the time in these logical timers expires an event is triggered.timers expires an event is triggered.

These system must keep track of these timers, These system must keep track of these timers, and one solution is to maintain an ordered linked and one solution is to maintain an ordered linked list of timing requestslist of timing requests

Page 13: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Contents of the timer queue element:Contents of the timer queue element: Pointers to the next (and previous Pointers to the next (and previous

element)element) Time quantumTime quantum Link to the PCB of requesting processLink to the PCB of requesting process Address of a procedure to call Address of a procedure to call

(representing the action to perform)(representing the action to perform) Type of request (user or OS)Type of request (user or OS)

Page 14: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

With each clock interrupt the clock driver has With each clock interrupt the clock driver has several things to do: incrementing the real time, several things to do: incrementing the real time, decrementing the time quantum in timer decrementing the time quantum in timer requests, check for zero (any timer elements requests, check for zero (any timer elements who’s counter has reached zero, and doing CPU who’s counter has reached zero, and doing CPU accounting.accounting.

It is vital that each of these operations be It is vital that each of these operations be arranged so that they are very fast, since they arranged so that they are very fast, since they have to be repeated many times a second.have to be repeated many times a second.

Let’s consider a timer queue….There are Let’s consider a timer queue….There are several ways the time stored in the first element several ways the time stored in the first element can be manipulated….can be manipulated….

Page 15: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

8430

Current time

* 8437A

* 8442B

* 8450C

* 8453D

* 8457E

* 8467F

PCB

Front of queue

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.

Time stored as a “real” time the event is to occur

Page 16: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

TIMER “Q” SEARCHTIMER “Q” SEARCH: :

SET P TO THE TOP_TQESET P TO THE TOP_TQE (timer queue element)(timer queue element)

DO WHILE TIME_OF_DAY_CLOCK >= P->TQE_TODDO WHILE TIME_OF_DAY_CLOCK >= P->TQE_TODSCHEDULE ACTION OF P->TQESCHEDULE ACTION OF P->TQESET P TO NEXT TQESET P TO NEXT TQE

ENDWHILEENDWHILE

SET TOP_TQE POINTER TO TQE POINTED TO BY PSET TOP_TQE POINTER TO TQE POINTED TO BY P

RETURNRETURN

Page 17: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Storing Intervals in elementsStoring Intervals in elements

8430

Current time

* 7A

* 5B

* 8C

* 3D

* 4E

* 10F

Front of queue

Request for 8437, 8430+7

Request for 8442, 8437+5

Request for 8450The 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.

Page 18: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

* 7A

* 0B

* 0C

* 3D

* 4E

* 10F

Front of queue

8430

Current time

Request for 8437, 8430+7

Request for 8440, 8437+3

If multiple events are to occur at the same time, the time value of subsequent elements is zero.

Page 19: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Programmable clock

730

# of clock ticks until the event is to be triggered

Timer Queue Head

* 730A

* 405B

Programmable clock

685

Timer Queue Head

* 730A

* 405B

Time can be stored in a programmable clock

The time stored in the programmable clock would be decremented based on the rate of that clock.

Page 20: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Adding new elements into the timer queue Adding new elements into the timer queue can require adjustments to other TQEs …can require adjustments to other TQEs …

Page 21: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Programmable clock

685

Timer Queue Head

* 730A

* 405B

A new request arrives for a time interval of 457

Programmable clock

457

Timer Queue Head

* 405B

* 228A

* 457C

Timer interval in A reset to the time remaining in the programmable clock, minus the time interval of the new event.

Page 22: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Adding new timing requestsAdding new timing requests Determining the time interval for the new Determining the time interval for the new

elementelement Searching the queue until an element is Searching the queue until an element is

found whose event should occur after the found whose event should occur after the time interval for the new element.time interval for the new element.

Inserting the new element into the queueInserting the new element into the queue Adjusting the time interval of the Adjusting the time interval of the

element, in front of which the new element, in front of which the new element has been inserted.element has been inserted.

Page 23: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Current time= 8430

Timerq_head

18 G

7 A

5 B

8 C

3 D

4 E

10 F

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…

Page 24: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Current time= 8430

Timerq_ head 7 A

5 B

6 G

2 (8) C

3 D

4 E

10 F

We insert G between elements b and c and adjust times in later elements to reflect the addition

Page 25: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Deleting element C

Current time= 8430

Timerq_ head 7 A

5 B

6 G

2 C

3 D

4 E

10 F

5 D

4 E

10 F

Deleting elements also require adjustments. When an element is deleted the times in later elements must be increased.

Page 26: Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.

Timing issues:Timing issues: The time spent processing a clock The time spent processing a clock

interrupt is critical to the accuracy of the interrupt is critical to the accuracy of the timing facility within an OS.timing facility within an OS.

We must update the hardware clock as We must update the hardware clock as quickly as possiblequickly as possible

Must be the highest priority interrupt if Must be the highest priority interrupt if clock ticks are to be counted accurately.clock ticks are to be counted accurately.

Ability to block all but clock interrupts will Ability to block all but clock interrupts will allow critical work to be done, but still allow critical work to be done, but still maintaining the accuracy of the clockmaintaining the accuracy of the clock