Skip to main content

Command Palette

Search for a command to run...

OS Part 2: Kernel ๐ŸŒฝ

Published
โ€ข8 min read
OS Part 2: Kernel ๐ŸŒฝ
A

That Indian Dev ๐Ÿ‡ฎ๐Ÿ‡ณ

The kernel acts as the bridge between software and hardware. It serves as a fundamental layer that provides essential functionalities and manages system resources, allowing applications to run smoothly and efficiently. From handling memory management and process scheduling to managing I/O operations and device drivers, the kernel plays a critical role in facilitating the overall operation and coordination of a computer system.

Functions of Kernel

  • Process Management: One of the primary responsibilities of the kernel is to efficiently manage processes and threads running on the system's CPUs. This includes tasks such as scheduling processes and threads, creating and deleting both user and system processes, as well as suspending and resuming processes. Additionally, the kernel provides mechanisms for process synchronization and communication, enabling efficient coordination between different processes.

  • Memory Management: Efficient memory management is crucial for an operating system, and the kernel plays a central role in this area. It is responsible for allocating and deallocating memory space as required by processes. The kernel keeps track of which parts of memory are currently in use and which processes are utilizing them. By managing memory effectively, the kernel ensures optimal utilization of system resources.

  • File Management: The kernel facilitates file management operations, which are essential for organizing and manipulating data on a computer system. It provides the necessary functions for creating and deleting files and directories. Furthermore, the kernel supports mapping files into secondary storage, allowing efficient access to large amounts of data. It also facilitates backup support, ensuring that important data is securely stored on stable storage media.

  • I/O Management: Efficient management and control of input/output (I/O) operations and devices are crucial for the smooth functioning of an operating system. The kernel plays a vital role in I/O management, encompassing tasks such as data buffering, caching, and spooling. Let's explore each of these functions in detail.

    1. Buffering: Buffering involves the temporary storage and transfer of data between two devices. The kernel employs buffering to facilitate efficient data transfer when the speed of data processing differs between the devices. By utilizing buffers, the kernel can optimize I/O operations by minimizing the impact of variations in device speeds. For example, buffering is commonly employed during video streaming, such as buffering a YouTube video. The kernel buffers a portion of the video data in advance, allowing for uninterrupted playback by compensating for potential delays in data retrieval.

    2. Caching: Caching is an optimization technique used to store frequently accessed data in a faster memory location. The kernel employs caching mechanisms to improve system performance by reducing the need to fetch data from slower storage mediums. The kernel implements various caching strategies, such as memory caching and web caching. Memory caching involves storing frequently accessed data in a portion of the system's memory, enabling faster retrieval. Web caching, on the other hand, involves storing web pages or resources on a local system, reducing the time required to fetch them from remote servers.

    3. Spooling: Spool, short for "Simultaneous Peripheral Operations OnLine", is a technique employed by the kernel to manage multiple tasks that require different speeds. The kernel acts as a coordinator, queuing jobs and handling them in an organized manner. Spooling enables efficient handling of tasks such as print spooling and mail spooling. In print spooling, the kernel accepts print jobs from different applications, queues them, and processes them one after another, ensuring smooth and timely printing. Similarly, mail spooling involves queuing outgoing mail messages for transmission, ensuring proper sequencing and reliable delivery.

Types of Kernels

Operating systems use different types of kernels to manage and control the execution of programs and the utilization of system resources.

  1. Monolithic Kernel: In a monolithic kernel, all functions are implemented within the kernel itself. However, this approach results in a bulky kernel that requires a significant amount of memory to run. Moreover, if one module crashes, the entire kernel is at risk of failure. On the upside, monolithic kernels offer high performance due to fast communication with fewer user modes and kernel mode overheads. Examples: Linux, Unix, and MS-DOS.

  2. Microkernel: Contrary to monolithic kernels, microkernels only implement essential functions, such as memory management and process management, within the kernel. File management and I/O management, on the other hand, are handled in user space. Microkernels are smaller in size compared to monolithic kernels and offer higher reliability and stability. However, the overhead of switching between user mode and kernel mode leads to slower performance. Examples: L4 Linux, Symbian OS, and MINIX.

  3. Hybrid Kernel: Hybrid kernels combine the advantages of both monolithic and microkernel designs. In a hybrid kernel, file management remains in user space, while other essential functions reside in the kernel space. This approach offers a balanced approach, incorporating the speed and design of a monolithic kernel with the modularity and stability of a microkernel. Inter-process communication (IPC) in hybrid kernels incurs lower overheads. Examples: macOS and Windows NT/7/10.

Inter-Process communication

To facilitate communication between processes the operating system employs a mechanism known as Inter-process communication (IPC). IPC allows independent processes with separate memory spaces to exchange information when necessary. There are two common methods of IPC:

  1. Shared Memory: Processes can share a portion of memory, allowing them to read and write data in a mutually accessible region.

  2. Message Passing: Processes can send and receive messages through specific system calls, enabling them to exchange information and synchronise their activities.

Software Interrupt

A software interrupt, also known as a trap or exception, is a mechanism used by an operating system to interrupt the normal execution of a program and transfer control to a specific routine or handler. It allows the operating system to respond to specific events, such as hardware events, exceptional conditions, or user requests, in a controlled manner.

Purpose of Software Interrupts

  1. Privileged Operations: User programs typically run in a restricted environment with limited access to system resources. Software interrupts allow user programs to request privileged operations that are beyond their normal privileges, such as file system access or device control.

  2. System Service Requests: User programs may require various services provided by the operating system, such as memory allocation, process creation, or I/O operations. Software interrupts provide a means for user programs to request these services.

  3. Exception Handling: Software interrupts are also used for exception handling and error reporting. If an exceptional situation occurs during program execution, the software interrupt can be used to transfer control to a predefined exception handler in the kernel.

Types of Software Interrupts

  1. System Calls: System calls provide an interface between user programs and the operating system. When a user program requires privileged operations or access to resources managed by the operating system, it invokes a system call. The software interrupt mechanism is employed to transfer control from user mode to the operating system, allowing it to perform the requested task on behalf of the user program.

  2. Exceptions: Exceptions are triggered by exceptional events or conditions that occur during program execution. These events can include division by zero, invalid memory access, page faults, or illegal instructions. When such an event is detected, the processor generates a software interrupt, enabling the operating system to handle the exceptional condition appropriately.

  3. Interrupt Requests (IRQ): Hardware devices often need attention from the operating system to process data or perform specific actions. Interrupt Request (IRQ) signals are used to notify the CPU about these hardware events. The CPU responds by generating a software interrupt, allowing the operating system to handle the request by executing the corresponding interrupt service routine (ISR).

  4. Signals: In Unix-like systems, signals are software interrupts used for interprocess communication. A process can send a signal to another process or itself to indicate a particular event or request termination. Signals are employed for various purposes, such as process synchronization, handling termination requests, and notifying about external events.

System Calls

In an operating system, applications need to interact with the kernel to perform tasks that are beyond their privileges. This interaction is facilitated through system calls, which act as a bridge between user programs and the kernel. System calls allow user programs to request services from the kernel, such as file management, process control, device management, information maintenance, and communication management.

"mkdir" Command

To understand how system calls work, let's consider the system call "mkdir" (short for "make directory"). The process of invoking the "mkdir" command in the user space and its interaction with the kernel can be broken down into the following steps:

  1. The user executes the "mkdir" command in the user space, specifying the desired directory name.

  2. The "mkdir" command, acting as a wrapper, indirectly calls the corresponding system call in the kernel's file management module.

  3. The system call "mkdir" in the kernel receives the request and verifies the permissions and validity of the directory name.

  4. The kernel's file management module creates the new directory within the file system, allocating the necessary resources.

  5. The kernel returns the appropriate status or error code to the user space, indicating the success or failure of the "mkdir" operation.

  6. The user program in the user space receives the result from the system call and continues its execution based on the returned status.

Types of System Calls

  1. Process Control: System calls related to managing processes, such as creating or terminating processes, changing process states, and waiting for process completion. Example: "fork", "exec", "exit".

  2. File Management: System calls for file operations, including creating, opening, reading, writing, and closing files. Example: "open", "read", "write", "close".

  3. Device Management: System calls to interact with devices, such as reading from or writing to devices, controlling device settings, and allocating resources. Example: "ioctl", "read", "write".

  4. Information Maintenance: System calls for obtaining or modifying system information, such as retrieving the current time, setting system parameters, or querying system statistics. Example: "time", "getpid", "setuid".

  5. Communication Management: System calls for inter-process communication, enabling processes to exchange data or synchronize their activities. This includes mechanisms like pipes, sockets, and message queues. Example: "pipe", "socket", "send", "receive".

In conclusion, Part 2 of our blog series delved into the critical role of the kernel in managing system resources and explored system calls as the interface between user programs and the kernel. We discussed process management, memory management, file management, and I/O management, highlighting their significance in operating systems. In Part 3, where we will delve deeper into processes.

More from this blog

A

Aaqil Raj Krishna

14 posts

That Indian Dev ๐Ÿ‡ฎ๐Ÿ‡ณ