Friday 18 August 2017

Linux Kernel

What is initrd image and what is its function in the linux booting process?
The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available.The initrd is bound to the kernel and loaded as part of the kernel boot procedure.  

 How to manipulate the current process states?

Linux kernel provide set_task_state(task,state) by which you can manipulate the state of given task. By using of set_task_state you can change to state of given task.

How Threads are implemented in Kernel?   
Every thread has a unique task_struct.creating thread also created by using the clone sys call except some flags for sharing the data like file system , signal handler,open files also set. Flags are used to specify that which resources are shared between parent and child process or thread.E.g clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);

Different states in Linux Process?

Linux kernel understand a thread as a process and implements process and thread in same way.
In linux a process can be in one of the following state. 1) TASK_RUNNING :- This state specify that process is either running or in ready state and waiting in runqueue . 2) TASK_INTRUPTTIBLE:- This condition states that the task is in sleeping state and waiting for some event to occur. When this condition occurs task comes in TASK_RUNNING state. 3) TASK_UNINTRRUPTIBLE:- This state is similar as previous state but in this state process does not wait for any event to occur. 4)_TASK_TRACED:- The process is getting traced by another process , such as a debugger. 

Preemptive multi tasking:
 
Preemptive multitasking:- In this kind of multitasking scheduler decides which process to start running and which process to stop. Good thing in this multitasking is that scheduler can take the global decision. All the linux kind OS comes in preemptive scheduling flavor.


I/O bound versus Processor bound processI/O bound processes spend most of the time waiting for some input/output event to occur and take very less time of processor. Processor bound process takes most of the time executing the code . These kind of process runs continuously without waiting for any input/output event.

What kind of priority maintained in Linux?
Linux implements two types of process priority value. 1) Nice value:-A number from -20 to +19 with a default value of 0. Large nice value corresponds to less priority. 2) Real Time Priority:-A value from 0 to 99. Higher the real time value means higher priority.

Cause of concurrency
In linux kernel there are various reason for concurrency to occur.
 If all the reason of concurrency well known then its very easy for Kernel developer to avoid the race conditions.
1)Kernel Preemption
As Kernel got preemptive after 2.6 version . It means one thread can preempt another task. The task which is newly scheduled might be executing in same critical region.
2) Interrupts
As interrupt occurs asynchronously any time , which interrupt the execution of current task.
3)softirqs and tasklets
The can raise or schedule a softirq or tasklet any time, interrupting the currently executing process.
4) Symmetrical multiprocessing
Two or more processor can execute same kernel code at same time which can cause race condition.
Page and Page Frames 
The system’s memory is broken up into fixed sized chunks called page frames.Please note that each architecture can defined its own page size.
 The kernel represents every page frame on the system with a struct page structure.The important point to understand is that the page structure is associated with physical pages, not virtual pages
The virtual address space is divided into pages, a contiguous span of addresses of a particular size. The pages are page aligned in that the starting address of a page is a multiple of the page size.


Interrupt Routine
If our driver supports any hardware interrupt then we need to define interrupt routine also. This function will be called whenever we will receive interrupt for our device. We need to register the interrupt support in the init function by using request_irq() funtion provided by Linux kernel.
while registering for interrupt we mention interrupt routine which will actual handles interrupt.
 Kernel Module



 Kernel module is the piece of code which can be loaded and unloaded into kernel upon demand. it means you can load your code or module into kernel whenever it required and you can unload your code and module from kernel whenever you don’s required.


IOCTL or Input output control
Linux provide us a generic call which can be used to control any device which is called IOCTL or input output control. As the name suggest by using of ioctl we can write to the device as well as we can read from the device also. 
ioctl function needs to be implemented in driver and an function pointer is initialized with it as we do in case of open(), close() etc. In implementation of ioctl we generally use switch case statement ,each case belongs to commends expected from the user space.
 
MMAP or Memory Map
  
Linux Kernel provides various ways of accessing the devices from user space. One way of sending and receiving data to and from user space is by using copy_to_user() and copy_from_user() system calls respectively.

Both of these system calls comes with some extra over head. One overhead is that you have to allocate a buffer in user space buffer also with the kernel buffer. Which is just wastage of extra memory. To overcome these limitations Linux kernel provides a way of mapping the device to some user space address .
 Mapping a Device means associating a range of user space address to device memory. Whenever the program reads or write in the assigned address range , it is actually accessing the device .
    
Linux Kernel provides mmap system calls for mapping a device file to user space address range. 

void *mmap(void *addr,size_t len, int prot, int flags , int fd, off_t offset );

Return Value:-
on success, mmap() returns a pointer to the mapped area. On error MAP_FAILED is returned.
Linux Device Drivers generally implement mmap function for providing facility to user space applications to access the device memory. Drivers contain mmap system in the file_operations(==>)structure and and is invoked when mmap system call is used by any application.
struct file_operations my_fops = {
read   :       my_read,
write   :       my_write,
open   :       my_open,
release :       my_close,
mmap   :        my_mmap,
owner   :       THIS_MODULE,
};

Memory mapped IO vs Port Mapped IO
 
Process of transaction of data between CPU and Devices, CPU supports multiple ports which are called IO or Input Output. CPU uses these ports for handling multiple devices connected to it.
 To access any of the external device we need to map that device at some memory address so that it can be accessed by using some standard command.
There are two ways available to map the external devices which are specified below:-
1) Port-mapped IO ( PMIO or isolated IO)
2) Memory-Mapped IO (MMIO)
Memory-Mapped IO
In case of memory mapped IO , external devices are mapped to the system memory in the same way as ROM and RAM is mapped . It means devices can be accessed in the same way as we access memory in general scenario.
 
In case of memory mapped IO , IO devices use same address bus as memory. The disadvantage of this approach is that entire address bus needs to be decoded for every device.For accessing these IO address bus will require logic gates to decode the address of device being accessed. It increase the cost of hardware.
Port-Mapped IO
Port mapped IO use different dedicated address space.For example Intel x86 architecture use IN and OUT commands to Read and Write data respectively .Port mapped IO also use the same address bus but might be needed only few lines to access all the available devices . Which means less hardware will require to decode the address of devices and hence less system cost.
 Kmalloc vs Vmalloc
Kmalloc is similar to malloc function, we use in our C program to allocate memory in user space. kmalloc allocates memory in kernel space. kmalloc allocates contiguous memory in physical memory as well as virtual memor
 

vmalloc is the other call to allocate memory in kernel space as like kmalloc.
vmalloc allocates contiguous memory in virtual memory but it doesn’t guarantee that memory allocated in physical memory will be contiguous.











 

No comments:

Post a Comment