2079 2

BIT204 ยท 2079 ยท 10 marks

What is kernel? Explain the process of system call with suitable diagram.

What is kernel? Explain the process of system call with suitable diagram.[10]

Operating SystemsChapter 2: Operating System StructuresSystem call process and mechanism

Quick answer

The kernel is the core component of an operating system that acts as a bridge between application programs and the computer hardware. It is the first program loaded into memory when the computer starts (after the bootloader) and remains in memory throughout the entire session.

Kernel and System Call Process

What is Kernel? [3 marks]

Key Characteristics of the Kernel:

  • It runs in privileged mode (also called kernel mode or supervisor mode), which gives it unrestricted access to all hardware resources.
  • It manages system resources such as CPU, memory, I/O devices, and file systems.
  • It provides a protected environment so that user programs cannot directly access hardware.

Functions of the Kernel:

FunctionDescription
Process ManagementCreates, schedules, and terminates processes
Memory ManagementAllocates and deallocates memory to processes
Device ManagementControls hardware devices via device drivers
File System ManagementManages files and directories on storage
Inter-Process CommunicationAllows processes to communicate with each other
System Call InterfaceProvides an interface for user programs to request services

Types of Kernel:

  1. Monolithic Kernel - All OS services run in kernel space (e.g., Linux, Unix)
  2. Microkernel - Only essential services run in kernel space (e.g., Minix)
  3. Hybrid Kernel - Combination of monolithic and microkernel (e.g., Windows NT)

System Call [7 marks]

Definition:

A system call is a programmatic way in which a user-level program requests a service from the operating system kernel. It provides the interface between a running process (user mode) and the operating system.

System calls are the only legal entry points into the kernel. When a user program needs to perform a privileged operation (such as reading a file, creating a process, or allocating memory), it must do so through a system call.

Types of System Calls:

  1. Process Control - fork(), exit(), wait()
  2. File Management - open(), read(), write(), close()
  3. Device Management - ioctl(), read(), write()
  4. Information Maintenance - getpid(), alarm(), sleep()
  5. Communication - pipe(), shmget(), mmap()

Process of System Call (Step-by-Step):

USER SPACE                          KERNEL SPACE
-----------                         ------------

[User Program]
     |
     | 1. Calls library function
     v
[Library / API]
  e.g., printf()
     |
     | 2. Prepares parameters,
     |    places system call
     |    number in register
     v
[TRAP / INT instruction]
  (Software Interrupt)
     |
     |-------- MODE SWITCH -------->
                                   [Kernel Mode Activated]
                                          |
                                          | 3. Hardware saves
                                          |    CPU state (registers,
                                          |    PC, flags)
                                          v
                                   [Trap Handler / Interrupt
                                    Vector Table]
                                          |
                                          | 4. Identifies system
                                          |    call number
                                          v
                                   [System Call Dispatcher /
                                    System Call Table]
                                          |
                                          | 5. Calls appropriate
                                          |    kernel routine
                                          v
                                   [Kernel Service Routine]
                                    e.g., sys_write()
                                          |
                                          | 6. Executes the
                                          |    requested service
                                          |
                                          | 7. Places return value
                                          |    in register
                                          |
     <------- MODE SWITCH ----------      |
[Return to User Mode]              [Kernel Mode Ends]
     |
     | 8. Library function
     |    retrieves return value
     v
[User Program Continues]

Detailed Steps Explained:

Step 1: User Program Makes a Request

  • The user program calls a high-level library function (e.g., read() in C).
  • This library function is part of the API (Application Programming Interface).

Step 2: Prepare for System Call

  • The library function places the system call number into a specific CPU register (e.g., register EAX in x86).
  • Any arguments/parameters are placed in other registers or on the stack.

Step 3: Execute TRAP Instruction

  • The library executes a special instruction called TRAP or INT (software interrupt), e.g., INT 0x80 in Linux x86.
  • This causes a mode switch from user mode to kernel mode.
  • The CPU saves the current state (program counter, registers, flags) onto the kernel stack.

Step 4: Trap Handler Invoked

  • The hardware transfers control to a fixed memory address -- the trap handler (defined in the interrupt vector table).
  • The trap handler verifies the system call number.

Step 5: System Call Dispatcher

  • The dispatcher uses the system call number as an index into the system call table.
  • The system call table is an array of function pointers pointing to kernel service routines.

Step 6: Kernel Service Routine Executes

  • The appropriate kernel routine (e.g., sys_read()) executes with full hardware privileges.
  • It performs the requested operation (reads from disk, allocates memory, etc.).

Step 7: Return from Kernel

  • The return value (success/error code) is placed in a register.
  • The kernel executes a return-from-trap instruction (e.g., IRET).
  • The CPU restores the saved state and switches back to user mode.

Step 8: User Program Resumes

  • The library function retrieves the return value from the register.
  • Control returns to the user program, which continues execution.

Diagram Summary:

+------------------+        System Call        +------------------+
|                  |  -----------------------> |                  |
|   USER PROCESS     |                           |     KERNEL       |
|   (User Mode)    |  <----------------------- |   (Kernel Mode)  |
+------------------+       Return Value        +------------------+
         |                                              |
         | 1. call read()                               |
         v                                              |
+------------------+                                    |
|  Library / API   |  2. system call number -> register |
|  (libc wrapper)  |  3. TRAP / INT 0x80                |
+---------+--------+                                    |
          |                                             v
          |                          +---------------------------+
          +------------------------->|  4. Trap handler          |
                                     |  5. System call dispatcher|
                                     |     (system call table)   |
                                     |  6. sys_read() executes   |
                                     +-------------+-------------+
                                                   |
                                     7. IRET, restore state, back to user mode

Conclusion

The kernel is the core of the operating system: it always resides in memory, runs in privileged mode, and owns the hardware. A user program can never touch that hardware directly, so a system call is the controlled doorway between the two worlds. The library wrapper loads the call number and arguments, a TRAP instruction switches the CPU into kernel mode, the dispatcher looks the number up in the system call table, the kernel routine does the work with full privilege, and a return-from-trap puts the CPU back in user mode with the result in a register. This mode switch is what keeps one process from corrupting another or the operating system itself.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.