Monday, May 3, 2021

Embedded C Interview questions

MORE IMPORTANT QUESTIONShttp://pandyaelectronics.blogspot.com/2021/06/an-ultimate-guide-for-embeddedfirmware.html


 https://sites.google.com/site/embedsystem1/home/interview-questions

https://www.codeproject.com/Articles/6154/Writing-Efficient-C-and-C-Code-Optimization

  • Count-down loops is always better than count-up loops (primarily because, != is faster than <= operation)
  • Loop Unrolling: Small loops can be unrolled for higher performance, with the disadvantage of increased code size.
  • Referring to address of local variable results in unoptimized code: Reason - Compiler will try to optimize by using it's registers for the variables; but when you refer the address, it cannot allocate a register and rather use memory (and there-by resulting in memory operation)
  • Reduce global variables: For the same reason that it has to use memory.
  • char, short, int - which is better?: Use int as much as possible because for 'char' and 'short' CPU has to do some shift operations before giving the value to the user
  • How to reduce the function call overhead?

  • Avoid using ++ and -- etc. within loop expressions. E.g.: while(n--){}, as this can sometimes be harder to optimize.
  • Minimize the use of global variables.
  • Declare anything within a file (external to functions) as static, unless it is intended to be global.
  • Use word-size variables if you can, as the machine can work with these better (instead of charshortdouble, bit fields etc.).
  • Don't use recursion. Recursion can be very elegant and neat, but creates many more function calls which can become a large overhead.
  • Avoid the sqrt() square root function in loops - calculating square roots is very CPU intensive.
  • Single dimension arrays are faster than multi-dimension arrays.
  • Compilers can often optimize a whole file - avoid splitting off closely related functions into separate files, the compiler will do better if it can see both of them together (it might be able to inline the code, for example).
  • Single precision math may be faster than double precision - there is often a compiler switch for this.
  • Floating point multiplication is often faster than division - use val * 0.5 instead of val / 2.0.
  • Addition is quicker than multiplication - use val + val + val instead of val * 3puts() is quicker than printf(), although less flexible.
  • Use #defined macros instead of commonly used tiny functions - sometimes the bulk of CPU usage can be tracked down to a small external function being called thousands of times in a tight loop. Replacing it with a macro to perform the same job will remove the overhead of all those function calls, and allow the compiler to be more aggressive in its optimization..
  • Binary/unformatted file access is faster than formatted access, as the machine does not have to convert between human-readable ASCII and machine-readable binary. If you don't actually need to read the data in a file yourself, consider making it a binary file.
  • If your library supports the mallopt() function (for controlling malloc), use it. The MAXFAST setting can make significant improvements to code that does a lot of malloc work. If a particular structure is created/destroyed many times a second, try setting the mallopt options to work best with that size.

Last, but definitely not least - turn compiler optimization on! Seems obvious, but is often forgotten in that last minute rush to get the product out on time. The compiler will be able to optimize at a much lower level than can be done in the source code, and perform optimizations specific to the target processor.

Sunday, April 4, 2021

Switches VS Routers

 Switches:

- Works out of the box straight away

- Supports broadcast

- Works on DL layer

- Creates a network

- Has more ports (typically, 24, 36, 48 ports etc).

- Works on MAC addresses (HW address)

- Maintains CAM table (Content Addressable Memory)

Routers:

- Does not work out of the box (needs configuration)

- Does not support broadcast

- Works on Network Layer

- Connects the networks

- Has limited set of ports (typically, 2, 4, 6, 8)

- Works on IP addresses (software address)

- Maintains Routing table

Tuesday, March 2, 2021

Friday, December 4, 2020

DOS Attack (Denial Of Service)

 https://www.cloudflare.com/learning/ddos/what-is-a-ddos-attack/

A distributed denial-of-service (DDoS) attack is a malicious attempt to disrupt the normal traffic of a targeted server, service or network by overwhelming the target or its surrounding infrastructure with a flood of Internet traffic


From a high level, a DDoS attack is like an unexpected traffic jam clogging up the highway, preventing regular traffic from arriving at its destination.


Thursday, November 5, 2020

Red Zone - Stack Frame (x86-64)

 https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=100685

https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64






Kernel Threads

  • kthreadd (pid = 2) created by kernel (pid = 0) during boot
  • kthreadd helps in creating other kernel threads which are required to run periodically/asynchronously
  • "ps" shows kernel threads
  • Everything under [square bracket] are kernel threads

sw0:FID128:root> ps -eaf
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 01:21 ?        00:00:00 init [5]
root         2     0  0 01:21 ?        00:00:00 [kthreadd]
root         3     2  0 01:21 ?        00:00:00 [ksoftirqd/0]
root         4     2  0 01:21 ?        00:00:00 [kworker/0:0]

  • All calls from user-space to create a new process (including "create_thread" in kthreadd), will end up in do_fork call which internally calls copy_process

Thursday, August 27, 2020