Showing posts with label interview answers. Show all posts
Showing posts with label interview answers. Show all posts

Saturday, January 14, 2023

(Programming) Combining the constant and volatile keywords in C

Sometimes asked as an interview question in embedded programming, this summary article covers an interesting use case of combining two seemingly opposite keywords in C [1].

Here is a TLDR of what this means:
1. int volatile x; Tells the compiler that x can change independent of the code e.g a register change that happens in Hardware.
2. uint16_t const x; Is used to tell the compiler that the variable x is immutable.
3. So when can we end up using these together? The article [1] talks about 3 cases, but they are essentially the same thing. We can use these keywords together for a pointer where the address of the pointer does not change (constant) but the value contained at that address is volatile (e.g. a register, shared memory etc). 
e.g. Constant address of volatile hardware register / memory. uint8_t volatile * const p_ptr; i.e the p_ptr has a constant address for a volatile (register).
 
Reference:
[1] Embedded.com. Michael Barr Combining C's volatile and constant 

Monday, November 15, 2021

Programming: Find location of Stack, Heap and current memory

 Simple code:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[]) {

    printf("location of code : %p\n", main);

    printf("location of heap : %p\n", malloc(100e6));

    int x = 3;

    printf("location of stack: %p\n", &x);

    return 0;

}

Note that %p prints the address of the pointer. 

Saturday, May 8, 2021

WiFi: What is phy restart? Why is it important in dense RF environments?

 Phy restart on radio drivers typically means if the hardware (radio) is able to sync to the preamble of a packet which has a stronger signal strength while it is already in the process of decoding a packet.


This is typically useful in noisy environments to achieve a "capture effect", where the stronger packet is finally decoded at the rx. radio. If the radio does not support PHY restart then typically such a reception would result in a collision on the rx-side at the radio.

Monday, April 26, 2021

Linux: Nagle's Algorithm and How to Disable it

 Nagle's algorithm is a TCP optimization in the kernel stack that waits to aggregate small chunks of bytes before sending packets on a TCP connection. This approach optimizes the amount of frame overhead spent in sending very small packets over the network. However, when the data is fairly sporadic, this could also lead to an increase in the average delay experienced.


Nagle's algorithm running on a host can be disabled by:
echo 1 > /proc/sys/net/ipv4/tcp_low_latency

Monday, November 23, 2020

Linux: Comma separated arguments in an If Statement

 What happens when you write code like this:

    if ((x,y) == true) {

Is it even a legal condition to put in? If yes, why would you use it?

We have an example you could try out:

bash-4.1$ cat test.cc 
#include <iostream>
using namespace std;
int main() {
    int x = 1, y =0;
    if ((x,y) == true) {
        cout << “X TRUE PATH" << endl;
    } else {
        cout << “Y FALSE PATH" << endl;
    }
}

Saturday, May 23, 2020

Linux: Where to use packed structures

Not sure if the structure should be packed or not? When to use one vs the other? What is the default behavior? What do they do?

For answering these questions, please download this PDF tutorial.

Typically this stems from the question, "what is the size of this struct"?

struct test_A
{
char a;
int b;
char c;
} x;
PDF Download

Citation:
Gautam Bhanage, "Padding versus Packing in Embedded Systems Programming", Published online at www.bhanage.com. May 2020. [PDF]

Tuesday, June 25, 2019

Linux: How does the kernel invoke the correct driver for the corresponding /dev file ops?

To answer this question, I am picking snippets of texts from different blogs that I read online.
"When devfs is not being used, adding a new driver to the system means assigning a major number to it. The assignment should be made at driver (module) initialization by calling the following function, defined in <linux/fs.h>:
int register_chrdev(unsigned int major, const char *name,
struct file_operations *fops);
The return value indicates success or failure of the operation. A negative return code signals an error; a 0 or positive return code reports successful completion. The major argument is the major number being requested, name is the name of your device, which will appear in /proc/devices, and fops is the pointer to an array of function pointers" [1]
Thus, this will result in the registration of the device driver with a major number with the kernel. 
"Once the driver has been registered in the kernel table, its operations are associated with the given major number. Whenever an operation is performed on a character device file associated with that major number, the kernel finds and invokes the proper function  from the  file_operations structure. For this reason, the pointer passed to register_chrdev should point to a global structure within the driver, not to one local to the module’s initialization function."