Thursday 19 September 2013

Interview Experience

I am sharing here some of the interview questions that I faced in this post.

C
1. What is volatile and why is it used?
2. What is the necessity for a const qualifier?
3. What is little Endian and big Endian? Write a code to convert little endian to big endian and vice versa.
4. Write a single line macro to do the above conversion
5. Declare a structure.
6. Structure has been declared with int; char; int; char members in same order. What is the size allocated for it in memory? Is there a way to optimise it?
7. Implement a stack using linked list.
8. String copy without using default functions from string.h
9. How to toggle a bit in a number?
10. Convert decimal to binary
11. Verify if a linked list is a palindrome or not.
12. What will happen if there is
main()
{
printf("Hello");
while(1);
}
13. What is the difference between char * const ptr, char const * ptr and const char * ptr??
14. What are preprocessor macros?
15. What is compilation and linking?
16. Is a global variable extern?
17. Can a static variable be made extern?
18. What are static variables and static functions?
19. Is a MACRO advantageous or a function?
20. What is difference between macro and inline function?
21. Reverse a linked list.
22. Find the middle element of linked list in a single traversal.
23. What are the differences between malloc and calloc?
24. Where are static and global variables stored in the memory?
25. How can a multi threaded application be written?

Linux
1. What is a kernel?
2. What is difference between user space and kernel space?
3. Why kernel mode of operation is required?
4. What happens when a system call is made?
5. How a kernel can be live patched? (Ksplice)
6. sed related, awk related question
7. how to write a module?
8. How to write a system call? steps
9. How the boot time of an embedded machine can be reduced?
10. How are embedded distribution different from regular distributions?
11. rpm and all its options
12. patch and all its options
13. How to debug a shell script/binary?
14. what are different mechanisms of inter process communications?
15. What is procfs and sysfs?
16. The ways to set ip for an interface.
17. What all kind of tests LTP covers?
18. What is the need of asmlinkage?
19. How many arguments can be passed to a system call?
20. yocto related questions.

Agile
1. TDD
2. Iterative model

I have added all the questions that I could remember. As and when I remember, I will update the post.

Thanks,
Gomathi

Wednesday 18 September 2013

Linux Boot Process - Step by Step

Linux Boot process is the process that happens right from power on until a shell appears for an user to start working on. Here I try to explain the process step by step.

1. Once the system is powered on, the control is taken by BIOS(Basic Input Output System).
2. This system triggers POST. POST is Power On System Test. It verifies that all hardware that are connected to the system are working fine.
3. If the verification succeeds, a bootable device is searched for. The bootable device could be a USB stick, a partition in hard drive, a CD ROM, via network etc.
4. When the bootable disk is identified, MBR is loaded from it. MBR is nothing but the Master Boot Record, which occupies the first sector (512 bytes) of the bootable device. Out of these 512 bytes, first 446 bytes consist of the primary boot loader information, next 64 bytes store the partition table information and the last 2 bytes store the magic for validating the MBR.
5. The MBR loads itself to the RAM and starts searching for the active partition. It also verifies no other partition is active at that point of time. It loads this in the RAM. In nut-shell it transfers control to GRUB.
6. The Grub (or earlier days it was lilo) uses /etc/grub/grub.cfg  file. It opens a boot menu, which lists the set of available distributions for deployment based on grub.cfg. If nothing is selected in this menu, the distribution which is specified as ddefault in grub.cfg will be deployed on the system.
7. After the decision of which distribution is to be deployed, an attempt is made to search for the kernel in the predetermined path. It will be zImage (Compressed, less than 512 Kb) or a bzImage(Big compressed, more than 512 Kb).
8. The head of this image itself will have some code that can
           a) Setup hardware
           b) Decompress the kernel image
           c) Copy the kernel to the memory.
9. Once the kernel is loaded into the memory,
           a) It initializes the memory and configures hardware that are connected to the system
           b) It finds the Initrd, decompresses it and mounts the Initrd.
           c) It then loads the kernel modules/device drivers.
           d) Initializes virtual devices.
After all this the smaller Initrd, which was used for bringing up the board is unmounted and the actual initrd is pivot_root ed. Some times it is possible to embed a small Initrd which contains only the bare essentials to bring up the board inside the kernel itself.
10. With this stage, The kernel is loaded and initialised. Now the control is transferred to user space.
           a) The first thing started in user space is /sbin/init. It runs with a PID 1
           b) Then /etc/rc.d/rc.sysinit script is executed.
           c) The /etc/inittab is executed. This is a special file, which spawns/starts services (like for console logging, syslog etc). Here the run level can also be mentioned.
           d) Then all the services i /etc/rc.d/rc<RUN LEVEL>.d is started.
           e) The last step is execution of /etc/rc.d/rc.local. This script can be very useful if we need to customise the start up procedure.

Once all this is done, Shell is available for the user to start using the system.

Thanks,
Gomathi

Linux Kernel Module : Internals of insmod and rmmod

The post intends to explain the internals of a kernel module and the way insmod and rmmod utilities work.

Internals of a kernel module 
         The kernel module is a dynamically loadable section of the kernel. It means it is a piece of kernel code which can be added to the running kernel when loaded and can be removed from kernel when the functionality is not required.
         To do so, the kernel configuration of the object is to be made as m, ie., CONFIG_<object> = m in the .config of the kernel build directory. To be more precise, the object has to be given as obj-m in the Makefile.
         This configuration and building will provide a .ko which is nothing but the kernel module. Many device drivers can be written this way, so that they can be loaded only when necessary and can be removed otherwise. This reduces the actual size of the kernel image.
         The loading of a kernel module can be performed by insmod or modprobe utilities, whereas unloading can be done by rmmod utility. These utilities are supported as part of busybox package and module-init-tools package. The busybox commands will not have all the features and options supported. Hence module-init-tools can be installed. If there is a constraint on size, the busybox package itself can be used for basic functionality.
          The kernel module after compilation is nothing but an elf file. It will have .text section for the instructions, .data for the data, .bss section for uninitialized data, .init.text for module_init function of a module, .exit.text section for the module_exit function of the module and .modinfo section which has all the macros.

How insmod works
1) Insmod is a small program, which calls init_module() to intimate the kernel that a module is attempted to be loaded and transfers the control to the kernel.
2) In kernel, sys_init_module() is run. It does a sequence of operations as follows
    a) Verifies if the user who attempts to load the module has the permission to do so or not.
    b) After verification, load_module function is called.
        b.1) The load_module function assigns temporary memory and copies the elf module from user space to kernel memory using copy_from_user.
        b.2) It then checks the sanity of the ELF file (Verification if it is a proper ELF file etc)
        b.3) Then based on the ELF file interpretation, it generates offset in the temporary memory space allocated. This is called the convenience variables.
        b.4) User arguments to the module are also copied to the kernel memory
        b.5) The state of the module is updated to MODULE_STATE_COMING
        b.6) The actual location in the kernel memory is allocated using SHF_ALLOC
        b.7) Symbol resolution is done.
        b.8) The load_module function returns a reference to the kernel module.
    c) The reference to the module returned by load_module is added to a doubly linked list that has a list of all the modules loaded in the system.
    d) Then the module_init function in the module code is called.
    e) Module state is updated to MODULE_STATE_LIVE

How rmmod works
1) rmmod calls delete_module which hints the kernel that an rmmod request has come in and the control is transferred to kernel.
2) The sys_delete_module() is called in the kernel. This does following operations.
     a) Checks if the user who attempts to remove the module has the permission to do so or not.
     b) Checks if any other module that is loaded is dependent on the module attempted to be unloaded. This information can be obtained from the modules_which_uses_me list.
     c) Checks if the module is actually loaded or not in the kernel. This is done by verifying if the current state of the module is MODULE_STATE_LIVE.
     d) It executes the module_exit function written in the module code.
     e) Then free_module function is called, which does the following.
          e.1) Removes any sysfs references of the module
          e.2)Removes all the kernel module object references.
          e.3) Performs architecture specific clean up if any.
          e.4) Unloads the module from kernel
          e.5) Updates the state of module to MODULE_STATE_GOING
          e.6) Frees the memory used by user space arguments for the module.


I sincerely hope that this blog gives a detailed understanding of insmod/rmmod utilities. Please feel free to correct me if any of the above information is incorrect or if it can be elaborated.

Thanks,
Gomathi