Tuesday 6 August 2013

[Kernel Programming#6] Busy waiting & Delay of execution in Kernel

Busy waiting

The current execution of process can wait for some events should occur (like availability of required resources/data, passage of time or release of a lock). Here, the passage of times means delaying the current execution.
This waiting can be done in two ways
1.       Busy waiting
2.       Sleeping

Busy waiting

Here, the process/thread run in a loop by constantly checking the event has occurred condition. For example, if you want to wait for 5millisec then,
wait = getCurrentTime() + 5;
while(wait > getCurrentTime())
cpu_relax(); // do nothing
cpu_relax() is an architecture way of saying that there is no much work with CPU at this time.
This function does nothing.
The CPU cycles are being wasted for this amount of time. If the wait time is small then it is advisable to use the busy waiting concepts. Since, there is no overhead of context switching and performance is good.
But, if the wait time is more then it will be better to use sleeping concepts instead of busy waiting.

Sleeping

Here, the process/thread will be put into wait-queue so that CPU can start execution of other process/thread (using context switching). Kernel will wake up the process when the required condition for that process is met.
Advantage: There is no wastage of CPU cycles.
Overhead: context switching

Delaying the execution:

There are few scenarios where the current process has to wait for some period of time like for example h/w needs to accomplish few tasks. There are different techniques to achieve this delay.
We can divide delays into two types:
Long delays: Delays those are reliably longer than clock tick. It can use system clock for implementation.
Short delays: Implemented with s/w loops.
Long delays: below techniques can be used
è Busy waiting: cpu_relax()
è Release processor: schedule(). Releasing the CPU when the process doesn’t require it. There is a possibility of going to infinite sleep since CPU starts serving other process, and current sleep process may not get CPU in worst case. The above two approaches uses jiffi counter.
To avoid infinite sleep, another way is to ask the kernel to do this task like below.
è wait_event_timeout(): using this technique, the process can be wake up when somebody calls wake_up() and when the timeout expires.
But, If process specifically require to wake up with timeout value only then below technique will be used.
è Schedule_timeout():
Short Delays: techniques (Busy waiting)
ndelay(): set nano seconds delay. Optional.
udelay(): set micro seconds delay. Every architecture implements it.
mdelay(): set milli seconds delay. Optional.
Delay achieved and it is at least the value of delay and it could be more.
There are other techniques which doesn’t involve in busy waiting.
msleep(): puts the calling process in sleep.
msleep_interruptible(): puts the calling process in sleep.
ssleep(): --same—but in seconds.

You might also like

Related Posts Plugin for WordPress, Blogger...