Wednesday, August 15, 2018

stack and heap

Stack:
1. Allocated at compiled time
2. Fast access
3. eg: Arrays
4. LIFO (Last In First Out)
5. Difficult to modify/insert/delete
6. Can be sorted
7. Global variables declared in program, args passed to functions

Heap:
1. Dynamically allocated
2. slow to access
3. eg: malloc allocated memory as heap
4. Easy modify/insert/delete
5. Not Sorted
6. Difficult to traverse
7. Generally pointers used to access memory location

ref:
https://sites.google.com/site/wdhamilttutorials/c/q2/stack_vs_heap

Why use pointers in C/C++

Function return values: normal functions can return only single value. But on the other hand if we call function by reference then you can change multiple values without returning it.

Dynamic memory allocation:

Work directly on memory locations, avoid temporary variable assignment.


Tuesday, August 14, 2018

online compilers

List of online compilers:

https://ide.geeksforgeeks.org/index.php

https://www.tutorialspoint.com/compile_c_online.php

https://www.onlinegdb.com/
( for some reason recursive factorial in C returns 0, while other compilers works fine)

man vs apropos

man keyword shows the man page for given keyword
apropos keyword shows in which man pages this keyword appeared

Friday, June 22, 2018

Using translate "tr" to remove space or replace chars

Using translate (tr) to squeeze space :
# cat test
 Intel(R) Core(TM)   iii7-6600U CPU @      2.60GHz
 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
#

squeeze repeated spaces to a single space:
# cat test |tr -s ' '
 Intel(R) Core(TM) iii7-6600U CPU @ 2.60GHz
 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
#

Squeeze repeated 'i' to a single 'i' :
# cat test |tr -s 'i'
 Intel(R) Core(TM)   i7-6600U CPU @      2.60GHz
 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
#

squeeze all 'i' and replace to 'X' :
# cat test |tr -s 'i' 'X'
 Intel(R) Core(TM)   X7-6600U CPU @      2.60GHz
 Intel(R) Core(TM) X7-6600U CPU @ 2.60GHz
#

Tuesday, February 20, 2018

How to run shell commands in parallel under for loop

I came across a situation where i need to run multiple commands at the same time i.e. in parallel.
I had to run the for loop to capture the device name and then run some write tests on all these devices at the same time.

 A "for" loop is completed with "do" , "done" semantic.
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write ; done

But this will end up running the fio command sequentially one after another.
The little secret is to use "&" instead of ";" after do phrase :
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write & done

And this will run the fio command on all the disks at the same time.

Saturday, February 4, 2017

Find a line with Control Character

I came into a situation where one of the application was failing to read a file.
There was no clue for that reason and it was just throwing exception of some bad string.
We knew there is something wrong with the file it is trying to read.

So we suspected some control character within the file. In order to find that character we can use the following commands :

Filename: test.txt
Line 1
line 2
line3
linne 4
line 5
line 6

GNU grep :
#/usr/gnu/bin/grep '[^[:print:]]' test.txt
line 5

#/usr/gnu/bin/grep '[[:cntrl:]]' test.txt
line 5

Above example indicates that there is a control character in the above line.

Using "cat" : reveals the control character at line 5 :
#cat -vte test.txt
Line 1$
line 2$
line3$
linne 4$
line 5^M$
line 6$

You can use vi/vim too but if the file is huge it may be difficult.
You can open file in vi and do ":set list" to see control characters