Structure of kernel gives rise to two main modes for kernel code :
- process context : kernel code working for user programs by executing a system call.
- interrupt context : kernel code handling an interrupt. has access to user data only in process context. Any code running in process context may be pre-empted at any time by an interrupt. Interrupts have priority levels. Interrupts of lower priority are pre-empted by interrupts of higher priority.
Kernel modules
- can add code to running kernel.
- Useful for providing device drivers which are required only if hardware present.
modprobeinserts module into running kernel.rmmodremoves module from running kernel (if unused).lsmodlists currently running module.
Concurrency issues in the kernel
Correct handling concurrency in the kernel is important. Manipulation of data structures which are shared between :
- code running in process mode and code running in interrupt mode.
- code running in interrupt mode. Must happen only withing critical regions. In multi-processor system even manipulation of data structures shared between code running in process context must happen only within critical sections.
Achieving mutual exclusion
Two ways :
- Semaphores/Mutex : when entering critical section fails, current process is put to sleep until critical region is available
only usable if all critical regions are in process context
Functions :DEFINE_MUTEX(),mutex_lock(),mutex_unlock() - Spinlock : processor tries repeatedly to enter critical section. Usable anywhere
Disadvantage : Have busy waiting Functions :spin_lock_init(),spin_lock(),spin_unlock()
Programming data transfer between userspace and kernel
Linux maintains a directory called proc as interface between user space and kernel. Files in this directory do not exists on disk. Read-and write-operations on these files translated into kernel operations, together with data transfer between user space and kernel. Useful mechanism for information exchange between kernel and user space,