Thursday, August 18, 2016

Kernel vs. Usermode

Welcome to my first blog article ever. I wrote this a long time ago when thought I would like to start a blog but then it somehow never happened :). But hey here it is now life and in colour. The content of this article started quite a long time ago when I had a class at University with the name "IT Infrastructure" where we went into the depth of the OS kernel, understanding caching in the CPU and how the CPU actually calculates and obviously many
more things. At that time I thought wow being in the OS kernel is very slick but on the flip side something very complicated so eventually dangerous e.g. overwrite memory...C programming...procedural no objects. Of course every OS driver using hardware is also running in the Ring 0 so the complexity really depends a lot on the functionality of the software running in the kernel as well as on QA itself. So enough of the initial talking now let's get into the article.
Operating systems are generally pretty big programs while the core component is the kernel. The kernel is the master and owner of all hardware resources runs runs in the deepest software layer and has direct access to hardware. The duties of a kernel are:


  • The interface to applications (API)
  • Control of the CPU (Central Processing Unit), hardware devices, memory (scheduler, driver, memory management).
  • Scheduling of resources e.g. process time for applications
  • The structure of resources e.g. the mapping of file systems onto a block oriented device like a disk.
  • Resolving conflicts of resources e.g. queuing of resources, locking of CPU resources
  • Virtualization of resources: processor (processes), disk (files), memory (virtual memory)
  • Monitoring of access control to files and devices


There are many different kind of kernels for different purposes like Exokernel, Monolithic kernel, layered kernel but without going into details of all different kind of kernels the microkernel is the most used kernel today. A microkernel is used for every Windows Workstation/Server and Linux based OS as well as VMware ESXi. The idea is to split and divide the OS into several processes while a client, can be a either another OS or an application. So this client requests a service by sending the message to the appropriate server, the server then performs the operation and the microkernel delivers the result back to the client as illustrated in the following figure:


Figure 1: Microkernel


Some of the microkernel different characteristics are:


  • Parts of the OS are easy to replace
  • Driver are able to run in user or kernel mode
  • Physical I/O access is challenging to implement
  • Context switches (sometime also referred as task switch or process switch is the switching of the CPU from one process or process thread thread to another).


User mode (non-privileged mode for user programs):
Where all user programs get executed. User mode has no direct access to memory or hardware. The reason is that every program could overwrite each other’s memory which could lead into corruption. User mode programs are in general seen as untrusted software from the kernel perspective. If there is a need to access hardware resources the process makes a call via the underlying API (system calls).


Kernel mode (also referred to as system mode):
The mode where all kernel programs execute. In kernel mode the processes have direct access to every underlying hardware. The CPU itself can only run in either kernel or user mode at one time. A switch from user to kernel mode is not done automatically it will get done by interrupts.


Without going too much into detail of the microkernel I would like to focus on context switches a bit. Every process has one or more threads. Programs use threads to use more than one CPU in quasi parallel time. As we learned earlier the microkernel is the master of all resources. So if a process wants to run in the CPU the overhead it quite huge. The existing environment running currently in the CPU has to be saved which includes:


  • Status of the process
  • The program counter
  • Stack Pointer
  • State of open files
  • Memory management: pointer into the actual process environment


Every step is an access to main memory, the cache of this process in the CPU has to be deleted because it won’t be valid anymore which concludes into a cache miss in the future. Every time a process want to get time on the CPU the OS scheduler decides when this process will get time again to run within the CPU.


process_status.png
Figure 2: Process status


A context switch can only happen in the kernel so if an application wants to use the CPU the process has to go from user mode to kernel mode via system calls. So a permanent switch from user to kernel mode is very expensive and is using a lot of CPU cycles. A software running directly in the kernel can reduce the overhead dramatically and improves performance. Two examples for kernel implementations in the virtualization world are:
  • VMware VSAN
  • PernixData FVP
Lets summarise: it all depends what needs to be approached but in general you say the following:
  • Kernel mode drivers / software is very complicated due to the fact that is runs in the core of the OS plus kernel mode is somehow limited how it can be programmed but has a huge performance benefit when done right. Also think about security which is in the kernel much easier to handle than outside
  • User mode software is extremely powerful because it has much less implications on stability and has the benefit that one can choose their framework for programming. Often you see a combination of user mode and kernel mode software because there there need to be a way to interact with the kernel modules. This is usually a daemon in user mode running on the core OS itself.

16 comments:

  1. as the first blog its gud .This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.
    https://www.acte.in/php-training-in-chennai
    https://www.acte.in/machine-learning-training-in-chennai
    https://www.acte.in/iot-training-in-chennai
    https://www.acte.in/blockchain-training-in-chennai
    https://www.acte.in/openstack-training-in-chennai

    ReplyDelete
  2. Great Post. Very informative. Keep Sharing!!

    Apply Now for MERN Stack Training Course In Noida

    For more details about the course fee, duration, classes, certification, and placement call our expert at 70-70-90-50-90

    ReplyDelete
  3. We provide solutions for any kind of emerging issues. We provide solutions for any emerging or existing QuickBooks related queries at our toll-free number: QuickBooks Customer Service +1 888-471-2380.

    ReplyDelete

  4. The best way to get answers for all of your questions is by calling their customer service department number, Quickbooks Customer Service +1 888-698-6548

    ReplyDelete


  5. Thankyou so much for sharing such a informative blog.

    Pest in general terms, are the insects or animals that affect you and your home too. Some pests can be controlled with one application or treatment but pests like cockroaches or termites are not easy to be controlled. They need regular treatment so that they don’t enter your premises again and again. But when the pests are entering in excess then it would be the right decision to take help from the pest control Service team.

    VMSWALE is a certified company offering VMSWALE Delhi NCR for Residential and Commercial spaces. It also offers Home Sanitization Services. High-Quality Chemicals with Trained and Verified Staff with Full Satisfaction. We Sanitize each and every corner and moveable of your home.

    ReplyDelete
  6. Thank you for sharing this post and this post is a very usefully my name is shop.jvatec May live in jaipur city of India Country I am a web devleper shop.jvatec For any information related to domain and hosting, visit our site

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. APTRON Solution's Data Science Training in Noida is your gateway to a successful career in the exciting field of data science. With a comprehensive curriculum, hands-on experience, expert guidance, top-notch infrastructure, placement support, and flexible learning options, we are committed to helping you achieve your data science aspirations. Join us today and unlock your data science potential with APTRON Solution in Noida. Your future in data science begins here.

    ReplyDelete
  9. It is such a nice blog content , I very feel of informative content and timely taken also. If you want to learn Best Mern Stack Training in noida, Then contact us now.

    ReplyDelete
  10. A manufacturing license authorizes pharmaceutical companies to produce drugs, ensuring compliance with regulatory standards for quality, safety, and efficacy. This license is essential for legal drug production, covering all stages from raw material procurement to final product packaging. It ensures that drugs meet stringent safety and quality criteria before reaching the market.

    ReplyDelete
  11. Are you looking to build a successful career in full-stack web development? APTRON Solutions offers a comprehensive Mean Stack Training Course in Gurgaon , designed to equip aspiring developers with the latest industry skills. Our course covers every aspect of the MEAN (MongoDB, Express.js, Angular, Node.js) stack, providing hands-on training and real-world project experience to help you become job-ready.

    ReplyDelete