Sunday 19 February 2012

OS Puzzle: Can it happen?

Consider two different threads are running the same piece of code below. When it will print the following ?

1. x is 7
2. x is 8
3. x is 9
4. x is 10

shared int x; 
x = 10; 
while (1) 

 x = x - 1; 
 x = x + 1; 
 if (x != 10) 
  printf(“x is %d”,x) 
}

Tuesday 7 February 2012

Effective Endian conversion in x86

Instead of Using the bit shift operators, we can use the assembly instruction bswap provided by x86 systems.
I am using the EAX Register for manipulation because it is the same register used for returning value. So i dont need any extra instruction to move my variable to EAX register

Thursday 12 January 2012

Embedded: Edge vs Level Triggered Interrupts




S.No Edge Triggered Interrupts Level Triggered Interrupts
1 Can cause high spurious Interrupts (if there is no signal proper filters, but can be avoided with filters) Minimal Spurious Interrupts
2 If the Pulse is very short it needs dedicated HW to detect it It is easy to detect the Level triggered Interrupts
3 From H/W Perspective more gates and complex to implement Less gates required and Less complex to implement
4 Not good for Interrupt Sharing. As if we dont clear the interrupt immediately it will cause other device interrupt to get lost. (But can be avoided if we clear the interrupt immediately) Good for Interrupt sharing with devices.
5 No Lockup problem If there is any interrupt without the handler to clear it will cause the whole system to Lockup
6 While Processing the interrupt first Clear the interrupt and then process Process the interrupt and then Clear. (or else may get the same interrupt again)


Verdict:  Edge Triggered Interrupts are preferred.

Thursday 29 December 2011

QuickBits-12: Bash Shortcuts for Geeks

 

These are some of the shortcuts that i use ...

Command Editing Shortcuts

  • Ctrl + a – go to the start of the command line
  • Ctrl + e – go to the end of the command line
  • Ctrl + k – delete from cursor to the end of the command line
  • Ctrl + u – delete from cursor to the start of the command line
  • Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
  •  Ctrl + w – delete from cursor to start of word (i.e. delete backwards one word)

 

Command Recall Shortcuts

  • Ctrl + r – search the history backwards
  • Ctrl + g – escape from history searching mode
  • Ctrl + p – previous command in history (i.e. walk back through the command history)
  • Ctrl + n – next command in history (i.e. walk forward through the command history)
  • Alt + . – use the last word of the previous command
  • Ctrl + o -  This command is my favourite. If you hit CTRL+o after picking a command from your history, the command is executed, and the next command from history is prepared on the prompt.
  •  

Command Control Shortcuts [Not a Bash Feature]

  • Ctrl + l – clear the screen
  • Ctrl + s – stops the output to the screen (for long running verbose command)
  • Ctrl + q – allow output to the screen (if previously stopped using command above)
  • Ctrl + c – terminate the command
  • Ctrl + z – suspend/stop the command
  •  

Bash Bang (!) Commands

  • !! - run last command
  • !blah – run the most recent command that starts with ‘blah’ (e.g. !ls)
  • !$ – the last word of the previous command (same as Alt + .)
  • !* – all of the previous command except for the first word (e.g. if you type ‘ls foo foo/bar foo/bar/foo‘, then !* would give you ‘foo foo/bar foo/bar/foo‘)
  • ^^ - If you type a command and run it, you can re-run the same command but substitute a piece of text for another piece of text using ^^ (e.g.: if you run 'ls this/is/cool/stuff' and if you do '^cool^geek' then it will run 'ls this/is/geek/stuff'. It replaces only the first occurance)
  • !!:gs/search/replace/ - It is same as previous except it replaces all occurance

 Tricks with ~/.inputrc

  • Put this in your ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-p": history-search-backward
"\C-n": history-search-forward

Type something, then pressing Ctrl-p(or Ctrl-n) will initiate the search in the history with the already typed text as prefix
  •  

Monday 21 November 2011

QuickBits-11: Giving User Input to cmd through file

Lots of user developed commands requires user input for "if you want to continue ... [y/n]" . If you want to automate this

For example: If mycommand requires user input saying 'y' then add 'y' to the arguments.txt file.


cat arguments.txt - | mycommand


Now your mycommand will not ask for user input. It takes it from the file arguments.txt

Saturday 19 November 2011

QuickBits-10: Highlight a word in Emacs buffer

If you have used Source Insight for code browsing, you would have used its word hightlight feature.You can highlight the word under cursor, in your file you are browsing using shift+f8.

You can do the same in Emacs, by adding this to your .emacs file


 
(defun is-word-highlighted ()
  (interactive)
  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
   (if (facep face) (if (face-equal face "hi-yellow") t nil) nil)))

(defun toggle-highlight-word ()
(interactive)
(setq sym (current-word))
(if (is-word-highlighted) (unhighlight-regexp sym) (highlight-regexp sym)) 
)

(global-set-key [S-f8] 'toggle-highlight-word)

Sunday 11 September 2011

QuickBits-9: Search String in files

1. To search for a string/regular expression in all files in a folder
grep -H "string" * 
This will print the matching line.
-H is for printing the matching filename.

2. To search for a string/regular expression in all files in a folder + subfolders
grep -H "string" * -r

3. To search for a string in all files of certain type
find . -name "pattern" -exec grep -H "string" {} \;


grep -H "string" $(find . -name "pattern")
Example: To find string "main" in all .c file
find . -name "*.c" -exec grep -H "main" {} \;


grep -H "main" $(find . -name "*.c")