Thursday, August 16, 2018

Signal vs Interrupt

Interrupts are for external events.
When an interrupt occurs , a context switch of already running thread is done to handle that interrupt.
Interrupts are handled by CPU for hardware events.
On receiving interrupt CPU does context switch i.e. saves the state of registers and starts processing the interrupt. (eg: keyboard input).

eg:
Keyboard input
I/O processing
high priority interrupt : NMI


Signals are more of internal mechanism to handle different events.
When a signal is sent to a process then OS interrupts the normal process flow control and handles that signal, if process has its own instructions to handle signal, else OS follows default signal handling. Signals are for process or threads and interrupts are for hardware/external events.

eg: SIGHUP, SIGTERM, 

Process vs Thread

Process states:
Running
Waiting:
Stopped
Zombie :Process died by its entries are still in the process table


Process has a virtual address space which is shared by threads it creates.

A running process has the following thread states :
Thread states:
Init
Ready/Runnable: TS_RUN
Running: TS_ONPROC
Sleeping: TS_SLEEP
Stopped: TS_STOPPED
zombie: TS_ZOMB

Threads inherit address space from parent process.
eg: fork. a child PID returns 0 if it is a thread.

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
#