Where to get the Kernel ? :
The Kernel source code can be downloaded from the http://www.kernel.org/ . The kernel source is typically installed in /usr/src/linux/ directory. Don't use this kernel source for the development, because the C library compiled along with this kernel is linked with this kernel. You can use root directory only to instll the kernel and the home directory for the kernel development.
I hope that you would get more information when you Google for the "how to compile/install kernel", for that reason i am skipping that.
Kernel source tree :
When you untar the kernel source directory, the root source tree of the kernel consists of different directories. The following is the list the directories and their description.
Directory Description
arch Architecture-specific source
crypto Crypto API
Documentation Kernel source documentation
drivers Device drivers
fs The VFS and the individual file systems
include Kernel headers
init Kernel boot and initialization
ipc Interprocess communication code
kernel Core subsystems, such as the scheduler
lib Helper routines
mm Memory management subsystem and the VM
net Networking subsystem
scripts Scripts used to build the kernel
security Linux Security Module
sound Sound subsystem
usr Early user-space code (called initramfs)
The Different nature of the kernel :
The kernel has several differences than compared to the user-space applications. Certainly not in the programming prospective but in nature. The most important differences of kernel are as follows
1. The kernel doesn't have access to the C library.
2. The kernel is developed in GNU C.
3. The kernel is doesn't have protection to the memory usage, like in user-space.
4. The Kernel can't easily use the floating point.
5. The kernel has a small fixed size stack.
6. The kernel has a asynchronous interrupts, is preemptive. The kernel supports SMP (symmetric multi processors), synchronization and conccurancy are the major concerns with in the kernel.
7. Portability is important.
We briefly look at each concern ....
No Lib C :
Unlike the user-space applications, the kernel doesn't link with the lib C (Or any other library, for that matter). There are multiple reasons for not linking the lib C with the kernel. The important reasons is, speed and the size. The C library or the decent subset of it is too large and insufficient for the kernel.
One of the important missing function is printf (), the kernel have printk (), it works same as the printf () function. In addition to that, it has the priority flag. This flag is used by the syslogd to decide where to show the messages.
ex : printk (KERN_INFO, "This is informational message\n");
GNU C :
The developers uses the ISO C99 and the GNU C, uses the extesnions of the language that are available in the gcc.
Inline Functions :
The inline functions is inserted in the function instead of calling the function, this will reduces the overhead of the function call (register saving) and return (register restore), and allows potentially more optimization because the compiler can optimize the caller and the called funcion. The disadvantage of the inline function is, increases the size and foot print of the application. Inline functions are used when the function is time critical. If the function is large and the function is called more than once the inline functions doesn't make send to use.
Ex : static inline void dog (unsigned long int arg)
The declaration of the function make the any usage, else the compiler will not make the function inline. Usually the inline functions are declared in the header files. As the functions are declared as static, can't be exported, so can be declared at the starting of the file.
Inline Assembly :
The gcc C compiler ebables embedding the assembly instructions in otherwise normal C functions. Ther directive asm () is used for the inline assembly code.
No Memory Protection :
When a user application access the illegal memory, the kernel traps it and throws the SIGSEGV and kill the process. When the kernel access the illegal memory location, the results are less controlled. Memory violations in kernel results in major kernel errors. It should go without saying that you must not access the illegal memory, such as dereferencing the NULL pointer but with in the kernel the stakes are much higher.
No easy use of a floating point :
When the user space application uses the floating point, the kernel manages the transition from integer to the floating point mode.
Unless the user space application, the kernel doesn't have luxary to use the floating point because the kernel can't trap itself. Using the floating point inside the kernel requires the manual saving and restore of the floating point registers. The best part is avoid using it. No floating point in the kernel.
Synchronization :
The kernel is liable to race conditions. Unlike the single threaded user space application, no.of properties of kernel allow for concurrent access of shared resources and thus require synchronization to prevent race conditions.
Specifically,
1. Linux is a preemptive multi tasking OS. Therefore, the processes schedule and reschedule at the whim of the scheduler, so synchronization is must among the processes.
2. Linux supports multiprocessing. Therefore, without proper protection, the kernel code executing on one or more processors can access the same resources.
3. In Linux the interrupts are asynchronous, Therefore, without proper protection, the interrupt can occur at the midst of accessing the resource and the interrupt handler can access the same resource.
4. Linux is preemptive, Therefore, without proper protection, The kernel code can be preempted in favor of other code and thus access the same resource.
The solution for the race conditions is spinlock and semaphores.
Portability :
This means that architecture-independent C code must correctly compile and run on a wide range of systems, and that architecture-dependent code must be properly segregated in system-specific directories in the kernel source tree.
A handful of rules such as remain endian neutral, be 64-bit clean, do not assume the word or page size, and so on go a long way....
Hopefully i will write about the Process Management in Kernel in the next post.
Tuesday, April 20, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment