Sunday, October 19, 2008

Role of Timer in Operating System

Timer in Operating System:As all we know that it is only the operating system that has full control over the CPU,and it is the primary duty of operating system to prevent user programs from getting stuck into an infinite loop or not calling to system services and never returning control back to the operating system.To accomplish all those services an operating system uses a device known as Timer.The main task of a timer is to interrupt the CPU after a specific period of time.This specific period of time is set by operating system and its value may very from 1 ms to 1 second.If the operating system is interested in using variable timer then it is implemented with the help of fixed rate clock or counter and in this case operating system sets the counter.

Working of Timer:Whenever the clock ticks,the counter is decremented.When the value of this counter reaches zero an interrupt occurs and control is transferred back to the operating system.Let's understand this with the help of example,A 10 bit counter with a 1 Millisecond clock allows interrupts at interval from 1 millisecond to 1024 milliseconds,in steps of 1 millisecond.Before turning control over to the user process an operating system ensures that timer is set to interrupt.Hence a timer can be used to prevent a user program from running too long.We can also initialize the value of counter with the amount of time that a process requires to complete its execution.For example,a program with 7 minutes time limit has its counter initialized to 420,and for every clock tick the counter is decremented by 1.Hence after 420 ticks the control is automatically transferred back to the operating system.