Search This Blog

Saturday, April 19, 2014

Volatile keyword in C

Volatile is a type qualifier. It stop compiler to optimize certain area of code and prevent compiler to assume value of a variable hasn't changed. so compiler leave that segment code without optimized.
     here is one example from #http://en.wikipedia.org/wiki/Volatile_variable

Without Volatile

Before compilation

static int foo;
 
void bar(void) {
    foo = 0;
 
    while (foo != 255)
         ;
}

Optimizing compiler observer that there is no other possible to change the value stored in foo. so compiler take the default value 0 and optimize the loop in to infinite .
After compilation
void bar_optimized(void) {
    foo = 0;
 
    while (true)
         ;
}

With Volatile
static volatile int foo;
 
void bar (void) {
    foo = 0;
 
    while (foo != 255)
        ;
}

Here compiler will not assume any value and it will let the foo variable for the system to change the value.

Volatile is a type qualifier not a storage class so it does not change the area of storage

In kernel
Volatile is used in optimization barrier primitives. In Linux barrier() macro is used, which acts as an optimization barrier. barrier() macro defined as-
 
/* Optimization barrier */
/* The "volatile" is due to gcc bugs */
#define barrier() __asm__ __volatile__("": : :"memory")

The volatile keyword disallow the compiler to reshuffle the asm instruction with the other instructions of the program.
Above example is taken from one of the kernel synchronization so called memory barrier.

Volatile keyword also allow access to memory mapped devices, allow usage of variable between set jump and long jump

Why Virtual Memory ?

Whenever we run several processes simultaneously, they run successfully as long as physical memory addressing all the processes. sometime process size may exceed to the available physical memory limits , then the corresponding process is thrown out from the process queue, that should not happen in real.
       Our physical memory is very limited and of course expensive. so Virtual provides that provision in which we can run multiple process at the same time with being worried about the physical memory size. virtual memory provides a logical layer between memory requested by the Process and Memory Management Unit(MMU). Virtual memory allows each process to use subset of available physical memory.
     This all possible because of paging technique. Paging provides a memory translation between virtual address to physical address.        

Synchronization Techniques Used in kernel

Here are all the synchronization technique used by kernel programmers 
  1. Per CPU Variable
  2. Atomic Operation
  3. Memory Barrier
  4. Spin Lock
  5. Semaphore
  6. Seqlocks
  7. Local Interrupt disabling
  8. Local Softirq disabling
  9. Read-Copy-Update (RCU)

Friday, April 18, 2014

What is dereferencing of a pointer?

Dereference operation means indirect operation. In pointers, pointer variable holds the address of similar kind of variable.
      Ex- int *p;
           'p' is a pointer to integer which holding the address of variable
     int i=10;
         when we want to retrieve value pointed by pointer variable 'p', We use *p to get the value so in this case we are not directly accessing the value of i, but we are indirectly accessing through pointer means dereferencing the pointer.  
      

Thursday, April 17, 2014

THREAD PRIORITY

Priority Range in threads

0 to 63 consider as thread priority range. which is used by the system and users.

0- This is reserved for null threads for the idle mode.

1-23 - This range of priority used by kernel thread, user thread and servers.

24-32 - Used by kernel thread and system server. i.e server with portserv capability

32-63 -  Reserved for real-time threads running on the kernel side.

Deadline Range 

Deadline (ms)                                                      Thread Priority

>100                                                                          24 - 26

>20                                                                            27

2 - 20                                                                          28 - 31

KERNEL MEMORY ALLOCATOR (KMA) ALGORITHMS

1) Resource map allocator
2) Power-of-two free list
3) McKusick-Kernel allocator
4) Buddy System
5) Mach's zone allocator
6) Dynix Allocator
7) Solaris's Slab allocator

Softirqs, Tasklets and Work Queue

Both are diferrable functions and they also called software interrupt. softirqs and tasklets are strictly correlated. tasklets are implemented on top of softirqs.

Softirqs are defined at compile time whereas tasklets are initialized and allocated at run time while loading the kernel module.

Softirqs are reentrant functions so it can run concurrently on the several CPUs. Spin loack techniques generally used to protect softirqs data structures. whereas tasklets run under restriction of kernel.

Tasklets schedule task for future purpose

Work Queue is similar to Tasklets, It also schedule task for future purpose. Both are use for the similar purposes, but tasklets are excute in interrupt context whereas work queue executes in process context.
    Work queue allow kernel function to be activated and later executed by the kernel thread(worker thread).      

  

Wednesday, April 16, 2014

Deferrable Functions

Defferrable function is non-urgent interruptible kernel functions. Softirqs and Taskets are diferrable functions.