Wednesday, August 25, 2010

Late/Dynamic Binding in C++






Late Binding using Function Pointers (C)

Tuesday, August 24, 2010

Locks in Multi-processing/multi-thread programming

DeadLock:
deadlock is a situation wherein two or more competing actions are each waiting for the other to finish, and thus neither ever does.


LiveLock:

livelock is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing. Livelock is a special case of resource starvation; the general definition only states that a specific process is not progressing.
A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.
Livelock is a risk with some algorithms that detect and recover from deadlock. If more than one process takes action, the deadlock detection algorithm can repeatedly trigger. This can be avoided by ensuring that only one process (chosen randomly or by priority) takes action.

I2C protocol

http://www.i2cbus.com/theprotocol.html

new/delete operator

1. Return type of delete operator is void (not void*)




2. Exception and valid cases


char *p;
delete p; ==> exception (shown in picture)




Where as
char *p = 0;
delete p; .==> You can, however, use delete on a pointer with the value 0. This provision means that, when newreturns 0 on failure, deleting the result of a failed new operation is harmless.




2. Mixing both new/malloc and delete/free


class A {
public:
   A() { cout << "Constructor" << endl;
   ~A() { cout << "Destructor" << endl;
};


int main() {
  A *a = new A;
  free(a);                 ==> Calls only constructor; not destructor


 A *b = (A*)malloc(sizeof(A));
 delete b;               ==> Calls only destructor; not constructor


 A *c = new A;
 delete c;                ==> Calls both constructor and destructor
}                          



Monday, June 21, 2010

GDB unknown commands

All in gdb> prompt

1. Ctrl + p => To get the previous command
2. p array@sizeof(array) => To print all the array elements
3. set print pretty on => To print the structure elements' values in a neat way
4. p /x => prints the value in hexa decimal format

Tuesday, March 30, 2010

VI keyboard Shortcuts

:set hlsearch (:nohl)

:set mouse=a

:set nocompatible

:set smartindent
:set nowrapscan (to disable the wrap search)

:highlight String ctermfg=red

:highlight search ctermfg=black


  • To indent a block of code, keep the cursor in any of the {} braces, and do =%
  • To comment a block of line, keep the cursor in the first line (from where you want to comment), 
          - press CTRL + v (to enter visual mode)
          - use UP DOWN arrows (or j/k keys) to select the lines
          - press I (shift + i) (this enters into visual mode)
          - type "//" 
          - Once you press ESC, the // will be applied to all the lines you selected.
  • To uncomment a block of code, keep the cursor in the first line (from where you want to uncomment)
          - press CTRL + v (to enter visual mode)
          - select RIGHT LEFT arrows to select only the "//" characters
          - press "d" (to delete all the // characters)
  • To search and replace a text ("src" to "dest"), use
          :%s/src/dest/ ==> Replaces first occurence of "src" in each line
          :%s/src/dest/g ==> Replaces all the occurences of "src" with "dest" in all the lines
          :%s/src/dest/gc ==> Asks for confirmation to replace or not for each occurence


  • Vimdiff commands:
    • ]c - to go to the next change
    • [c - to go to the previous change
    • do - diff obtain; means get the changes from other file
    • dp - diff put; means put the changes into other file

Friday, March 26, 2010

Emacs Keyboard shortcuts

Movements:

M - f  <==> Move forward by one word
M - b <==> Move backward by one word

C - f   <==> Move forward by one letter
C - b  <==> Move backward by one letter
C - p  <==> Move up by one line
C - n  <==> Move down by one line

M - v  <==> PAGE UP
C - v  <==> PAGE DOWN

M - >  <==> Goto end of the file (U have to press "SHIFT" here to enter "<" )
M - <  <==> Goto the beginning of the file (same as above)

File operations:









C - x C- f  <==> To open a file
C - x k  <==> Close the current buffer (kill buffer)


M - d  <==> Delete next word
C - k   <==> Delete current line
C - y   <==> Paste (watever copied)

C - [space] <==> Start marking the portion
C - w  <==> Cut the portion till the cursor is in currently
M - w  <==> Copy the portion till the cursor is in currently

C - x u  <==> Undo last operation

M - g g  <==> Goto the line
M - x replace-string  <==> Search and replace

C - s [string]  <==> To search a string forward
C - r [string]  <==> To search a string backward

Windows:


C - x 0   <==> Close the current window
C - x 1   <==> Close all the windows except current window
C - x 2   <==> Split the current window vertically (means top and bottom)
C - x 3   <==> Split the current window horizontally (means left and right)
C - x o ('o' for 'others')   <==> Switch between windows

Shell:

M -x shell  <==> Open shell (terminal)

Setting Tab Size:


M - x set-variable [ENTER] c-basic-offset [ENTER]  8 <==> Setting tab size to 8


Auto-Indentation:

M -x indented-text-mode   <==> set auto indent (does not work through putty)

To search for a string in all the files in a directory:


M - x grep-find [Enter]  <==> This will generate a command; at the end of the command, give the string you want to search; and also set the directory in which you wana search

Thursday, March 18, 2010

Links

http://www.catch22.net/tuts/kernel101

Basics

Courtesy: http://en.wikipedia.org/wiki/Device_driver

Device Driver
- an extension of OS
- a "bridge" between hardware and applications (and Operating Systems)
- abstracted into logical and physical layers (and hence can be categorized as Logical Device Drivers (LDD) and Physical Device Drivers (PDD))
- LDD written by OS vendors where as PDD written by device vendors

An attempt by Microsoft to reduce system instability due to poorly written device drivers by creating a new framework for driver development called "WINDOWS DRIVER FOUNDATION (WDF)".

WDF - includes UMDF (User Mode Driver Framework) and KMDF (Kernel Mode driver Framework)

1. UMDF - implements a message-based protocol for communicating with their devices - as user mode drivers.
- any malfunction on these drivers does not cause any system instability

2. KMDF - allows development of kernel-mode device drivers, but attempts 2 provide std implementations functions that are well known to cause probs, including cancellation of IO operations, power management, and PnP device support

Device Drivers on Windows, can run on *both Ring 0 and Ring 3*

Tuesday, January 5, 2010

C-States

C1
  • -      Internal CPU Clock signal is stopped
  • -      BIU and APIC are still fed with internal Clock Generator to allow the CPU to temporarily exits the HLT state
  • -      Since CPU temporarily “leaves” the HALT state, this state is called as “Stop Clock Snoop State” or “HALT/Grant Snoop State” or “Snoop State”
Intel C1E
  • -      Exactly similar to C1; but reduces the internal CPU voltage as well
  • -      If this is enabled in BIOS, the CPU will enter this mode instead of “traditional” C1 state on executing HLT instruction
  • -      Also called as Extended Halt/Stop Grant Snoop State”
AMD C1E
  • -      Works just like C3 (shutting down all CPU clocks – both internal and external)
  • -      Processor enters C1E state when this option is enabled in BIOS AND all CPU cores enter the regular C1 (HLT) state.
  • -      The diff between C1E and C3 is basically how the CPU enters the Sleep state:
  • -      While on traditional C3 state, CPU must be put in that state usually by a command from the OS
  • -      Where as on C1E, the CPU enters the Sleep state automatically when all cores are at HLT (C1) state
C2
  • -      Intel - Introduced by adding one extra pin to CPU called “STPCLK” (Stop Clock)
  • -      AMD - C2 state is entered by simply reading a register from the ACPI, circuit that is physically located on the chipset;
  • -      When this pin is asserted, the CPU core clock is cut
  • -      As you can notice, both C1 and C2 cuts the CPU core clock; the diff is in how the CPU achieves this
  • -      C1 is activated by software (HLT instruction) while C2 is activated by hardware (by sending a signal to CPU pin “STPCLK)
  • -      Two modes
o    Stop Grant
§  As explained above; CPU core clock is stopped but the clock generator chip (also know as PLL) is still active and generating the external bus reference clock i.e., CPU external clock
o    Stop Clock
§  Here the Clock Generator itself is turned off and thus the external clock generator chip would be turned off, thus saving more energy
§  Current CPUs don’t have this Stop Clock mode inside C2 state but on the C3 Deep Sleep State


C2E
  • -      Similar to C2; but reduces the CPU voltage besides stopping the CPU internal clock
C3
  • -      Also known as “Sleep” state
  • -      Both BIU and APIC clocks will be cut off (which means it cant answer to important requests coming from CPU external bus or interruptions)
  • -      C3 implementation on Intel
o    Given an extra pin called SLP (or DPSLP depending on CPU model) which must be activated when the CPU is in C2 state in order to switch the CPU into C3 state
o    So first STPCLK pin must be activated and then one should activate the SLP pin
o    Achieving the Deep Sleep state is achieved by simply cutting the “external clock signal”


  • -      C3 implementation on AMD
o    Achieved by simply reading a register from ACPI, circuit that is physically located on the chipset (P_LVL3) in the Processor control Block (P_BLK)
  • -      Two modes
o    Sleep – as explained above
o    Deep Sleep (pin is DPSLP instead SLP) – achieved by simply cutting the “external clock signal”
o    AltVid – allows reduction on CPU voltage while they are in C3 mode


-      Note: AMD C1E and C3 (Sleep State; not the Deep Sleep state) are similar


C4
  • -      Also known as Deeper Sleep State
  • -      Since on C3, all clock signals inside the CPU are stopped, there is no other way to save power by playing with CPU clock signals. The next step on reducing the CPU idle power is to reduce the CPU voltage (Power = VI)
  • -      Intel – C4 is achieved from C3 i.e., CPU must first enter C3 and then, from there, it can reduce its internal voltage
C4E
  • -      C4 + CPU voltage is reduced even more after the L2 memory cache has been disabled (some calls this C5 which is not the real name of this mode)
C6
  • -      Also known as Deep Power Down
  • -      When CPU enters this state, it saves its entire architectural state inside a special static (intel) or DRAM (which is fed from an independent power source)
  • -      This allows the CPU internal voltage to be lowered to any value, *including 0V*
  • -      When the CPU is waked up, it loads the previous state of all internal units  from its special static RAM (waking up CPU from this state takes a lot longer)
  • Notice that there is only one voltage line for the entire CPU( the only component with a different voltage source is the above mentioned static or DRAM where the entire architectural state is stored) and lowering or turning off the CPU voltage is an all-or-nothing kind of deal; if you turn off the CPU, you have to turn off it entirely when it goes into C6 mode