An Application of Dynamic Timers

To show how the outcome of all the previous activities are actually used in the kernel, we'll show an example of the creation and use of a process time-out.

Let's assume that the kernel decides to suspend the current process for two seconds. It does this by executing the following code:

set_current_state(TASK_INTERRUPTIBLE); /* or TASK_UNINTERRUPTIBLE */ remaining = schedule timeout(timeout);

The kernel implements process time-outs by using dynamic timers. They appear in the schedule_timeout( ) function, which essentially executes the following statements:

struct timer list timer; expire = timeout + jiffies;

init timer(&timer);

timer.expires = expire;

timer.data = (unsigned long) current;

timer.function = process timeout;

add timer(&timer);

schedule( ) ; /* process suspended until timer expires */

del timer sync(&timer);

timeout = expire - jiffies;

When schedule( ) is invoked, another process is selected for execution; when the former process resumes its execution, the function removes the dynamic timer. In the last statement, the function either returns 0, if the time-out is expired, or it returns the number of ticks left to the time-out expiration if the process was awoken for some other reason.

When the time-out expires, the kernel executes the following function:

void process timeout(unsigned long data) {

struct task_struct * p = (struct task_struct *) data; wake_up_process(p);

The run_timer_list( ) function invokes process_timeout( ), passing as its parameter the process descriptor pointer stored in the data field of the timer object. As a result, the suspended process is woken up.

I [email protected] RuBoard

Was this article helpful?

0 0

Post a comment