Showing posts with label Linux Commands. Show all posts
Showing posts with label Linux Commands. Show all posts

Friday, November 14, 2014

Volatile : Demistyfied


  

Introduction
"Volatile" is a qualifier in 'C' which is applied to a variable when it is declared. So, what does it tells to the compiler? It gives the information to the compiler that the value of the variable may change at any time during the execution of the code without the knowledge of the compiler. If proper precautions are not taken, we might not get the desired results. A variable should be declared volatile whenever its value could change unexpectedly.


The syntax for declaring the variable as volatile is given below,
volatile dataTpye variable;  

Let us understand the “Volatile” keyword in deep through the following examples.


Example 1:
Let us consider small and simple example as shown in the Fig:1 to study the behavior of the 'volatile' keyword in C.

Fig 1: Code without the use of volatile.
In the above example the intention of the programmer is to keep polling inside the while loop until 'flag' value becomes 1(one).But the compiler, while compiling the code applies the optimization techniques and compiler will notice that no other code can possibly change the value stored in 'flag', and therefore assume that it will remain equal to 0(Zero) all times. The compiler will then replace the function body with an infinite loop as shown in the below Fig 2

                Fig 2: Optimization applied by the compiler to the code shown in fig1.

Let we check the size of the assembly code generated by the compiler as shown in the below Fig 3.

Fig 3: Size of assembly code generated by the compiler.


Now, if you observe Fig 3, the size can be found as 482 bytes in the 5th column. Now, we will apply the volatile keyword to the flag variable to the code shown in Fig 1, as shown in the below Fig 4,

                 Fig 4: Code with volatile  
Let we check the size of the assembly code generated by the compiler as shown in the below Fig 5.
Fig5: Size of assembly code generated by the compiler after applying 'volatile' keyword.


Now, if you observe Fig 5, the size can be found as 501 bytes in the 5th column. So, when we compare the sizes of both the codes with & without volatile keyword, obviously one can observe that the compiler is not optimizing the variable flag when it is qualified as “Volatile”.

Let we still experiment further to explore where the compiler is optimizing the code, to do this apply the vimdiff command to the assembly codes generated earlier, the difference is shown in the below fig 6:


Fig 6: Difference between the assemblies codes generated without & with volatile keyword.
From the above figure, we can conclude that volatile keyword prevents the application of optimization techniques by the compiler.

Example 2
Let us consider another example, where “for” loops are used commonly in the Embedded C code for the generation of the delays. Let us see how the compiler will optimize the code containing the “for” loops in the embedded C code without the use of the qualifier “Volatile” as shown in the Fig 7 below,

Fig7: For loop without volatile qualifier.
Let us generate the assembly code for the above given example, using the command given in the note 2, and getting the size of the assembly code using the “ls” command is given below in Fig 8,

Fig8: Size of assembly code generated by the compiler without volatile qualifier.
Now, we will apply the volatile keyword to the “i” variable in the code shown in Fig 7, as shown in the below Fig 9,

Fig9: Code with volatile keyword.
Let us generate the assembly code for the above given example, using the command given in the note 2, and getting the size of the assembly code using the “ls” command is given below in Fig 10,

Fig10: Size of assembly code generated by the compiler with volatile qualifier.
Comparing the sizes in the Fig 8 & 10, one can identify the compiler is applying the optimization techniques without the volatile qualifier. The dis-assembly code for both with & without volatile keyword is shown below in Fig  11,

Fig 11: Difference between the assemblies codes generated without & with volatile keyword.
Example 3 : Global variables accessed by multiple tasks within a multi-threaded application
Let us consider one more example to show how the global variable will be affected by the compiler optimization in the multi-threaded application. The example code snippet is shown as below in Fig 12,

Fig 12: Demo code to show how global variable will be affected in multi threaded program.
In the above demo program, the compiler doesn't have any knowledge of context switching between the two threads. If the compiler optimizations are turned “ON” then the compiler will assume that global_item_count variable is always “ZERO” and no other part of the thread is attempting to modify it. So, the compiler may replace the line no. 11 in the demo code like this

 
Which is nothing but the infinite loop, so in-order to avoid such optimizations by the compiler, it is safe to declare the variable global_item_count as “volatile”.

Similarly, one can realize the effect of producer-consumer problem accessing the global variable without declaring it as “Volatile”. Refer the link below


Example 4: Interrupt service routines
Let us consider another example given in the fig 13, where “volatile” plays a very important role in the ISR.
Fig 13: Volatile keyword used in ISRs


In the above example, if the flag is not declared as “Volatile” , then the compiler may optimize the code assuming always the flag is ZERO and replace the while(!flag) to while(TRUE) in line no.11, which is nothing but infinite loop. But the flag value will change when the interrupt occurs.

Whether to declare the variable as 'Volatile' or not is cross compiler dependent, anyhow it is good practice to declare the variable as 'Volatile' to achieve the portability of the code.

Conclusion:
The main use of volatile keyword is to prevent compiler from optimizing the code in terms of time complexity by generating a code that uses CPU registers as faster ways to represent variables. By declaring the variable as “Volatile” forces compiled code to access the exact memory location in RAM on every access to the variable to get the latest value of it which may have been changed by another entity.

A variable should be declared volatile whenever its value could change unexpectedly. In real time, three types of variables could change,

1. Memory-mapped peripheral registers

2. Global variables modified by an interrupt service routine

3. Global variables accessed by multiple tasks within a multi-threaded application  


Tuesday, May 27, 2014

Split command : Organize your C files better

Often when we walk into car garage, one can observe that the mechanic having different type of spanners, screw drivers for fixing different set of problems. These are nothing but tools that makes life of the mechanic easier. In the similar lines knowing different tools (ex: commands) and using it for their advantage can make life of a developer much easier. In this post let us have a look into split command.

Let us take example of the given file (Fig1, split_file.c), which has the obvious main function and two other functions i.e. function1 and function2. As the size of the file grows, file becomes bigger in size and the development may want to move some of the function implementation into other files and eventually compile them using make utility.

Fig1: split_file.c
One way to split function1 and function2 into two different file is by using the split command. Here is a simple way to understand the split command:
                                                split –b <file> <targetfile>
                                                split –l  <file> <targetfile>

where
                                                file: Input file which you need to get it splitted
                                               targetfile: Instance of output where it needs to be spitted
After executing the split command the command itself will split the output into targetfileaa, targetfileab etc, depending on the type of split. The “-b” or “-l” option talks about if the split to be based on bytes of lines of the file.

In our case since the main, function1 and function2 are having 10 lines each, we can apply a split based on the split as follows (Fig2: Applying split command)

Fig2: Applying split command
After the command you can do ls –al and see there are split_outputaa, split_outputab, split_outputac etc,which will have main, function1 and function2 split into it. Let us see the file contents by executing the cat (Fig3: Cat command for viewing contents) command as follows.
Fig3: Output of split command
In this way larger files can be split into smaller set of files, which come in handy during development. Such tools also help a developer to keep source files compact and split across, thereby helping in maintaining and enhancing them without much problem. For example if you have 5000+ lines of code in a single file, its high time to start dividing them into multiple times. Its time to start splitting!