Wednesday, January 1, 2014

Difference between priority inversion and priority inheritance

Consider two processes A and B which use a common lock. Process A has a much higher priority than process B. In this case if process B holds the common lock, then process A will not have a chance to run until process B releases the lock. In the mean time, if the scheduler runs it could always pick a process C which has priority in between A and B to run.

Hence, now process C which has priority less than A runs before A thereby making the priorities assigned to the processes useless. Also, since C has a higher priority than B it runs longer further preventing B from releasing the common lock for A. In the worst case we can imagine a lot of other processes like B which have priorities between A and B.

Why Pre-emption does not work here:
In this case even if we enable kernel pre-emption, all it can ensure is making the process B relinquish control. However, process A still can't run until the common lock is released by B.


1. Careful and minimal use of locks: Works best in most cases.

2. Priority inheritance: In this mechanism the process holding the lock inherits the priority of the highest priority process holding the lock. In our example, this will ensure that B has the same priority as A and runs often enough to release the resource soon enough for A to use it. This has been an often contended approach to handling priority inversion and is not the best way out of the situation.

Tuesday, December 31, 2013

What is the first function loaded from the kernel?

The first function loaded in the kernel is start_kernel()

Find a value in an array in octave

As intuitive as octave is, the command is find. However, through the standard help spit out by octave it is not very apparent how we can search for a value.

Say we create an array of 10 values rounded off to have numbers between 1 to 10:
octave:27> x = floor(rand(1,10)*10)+1
x =
   7   3   3   4   6   7   6   4   2   6
Now, say we want to find if the array contains the value 3, we would do something like:
octave:28> m = find(x==3, 1)
m =  2
The output m = 2 tells us the index of the location and also the fact that this value is present. If we test for a non-existing value for something like say 10,
octave:29> m = find(x==10, 1)
m = [](1x0)

Sunday, December 29, 2013

Difference between interrupt context and process context

Process context: When the kernel is running code on behalf of some process, we refer to the kernel running in process context. The kernel could be running in process context because of executing a system call like read() or write().

Interrupt context: When the kernel is not running in process context, we say that it is running in interrupt context. The kernel runs in interrupt context because of either of these conditions:

  1. Actually receiving an interrupt on the IRQ line.
  2. Soft-IRQ
  3. Timer firing
Hence, the kernel can be running only in either one of the two.