4.1 Concept

System calls are controlled interfaces through which user programs request services from the operating system kernel.

Applications running in user mode cannot directly:

  • Access hardware

  • Perform privileged operations

  • Manipulate protected resources

Instead, they must request services from the operating system using:

System Calls

System calls act as:

Controlled entry points into the kernel

They provide a secure mechanism for interaction between:

  • User space

  • Kernel space

Almost every major operating system service relies on system calls.

Examples include:

  • File access

  • Process creation

  • Memory allocation

  • Device communication

  • Network operations

Without system calls:

  • Applications could not safely interact with hardware resources.

Why System Calls Are Necessary

Modern operating systems enforce:

  • Protection

  • Isolation

  • Security

User programs operate in:

User Mode

with limited privileges.

Direct hardware access is prohibited because:

  • Malicious applications could damage the system

  • Bugs could crash the operating system

  • Sensitive resources could be corrupted

System calls solve this problem by allowing applications to:

Request kernel services safely and in a controlled manner.

User Space vs Kernel Space

User Space

Applications execute in:

  • Restricted mode

  • Limited privilege environment

Kernel Space

The operating system kernel executes with:

  • Full hardware privileges

  • Direct resource access

System calls create a controlled bridge between these two spaces.

4.2 Internal Working

When a user application invokes a system call, the CPU transfers control from user mode to kernel mode so that the operating system can safely perform privileged operations.

Step-by-Step Flow

Step 1: User Program Invokes System Call

The application calls a system function such as:

  • write()

  • read()

  • open()

  • fork()

Step 2: Parameters Passed

Arguments required for the system call are passed through:

  • CPU registers

  • Stack

  • Memory locations

Examples of parameters:

  • File descriptor

  • Memory address

  • Data size

Step 3: Trap Instruction Executed

The application executes a special instruction known as:

Trap Instruction

Examples:

  • syscall

  • int 0x80

This instruction signals the CPU to:

  • Switch execution privilege

  • Enter kernel mode

Step 4: CPU Switches to Kernel Mode

The processor changes from:

  • User Mode
    to

  • Kernel Mode

The CPU saves:

  • Program counter

  • Registers

  • Processor state

This ensures execution can resume later.

Step 5: OS Validates Request

The operating system verifies:

  • Parameter correctness

  • Access permissions

  • Resource availability

This prevents:

  • Illegal memory access

  • Unauthorized operations

  • Security violations

Step 6: OS Performs Operation

The kernel executes the requested service.

Examples:

  • Writing data to disk

  • Creating process

  • Reading file contents

  • Allocating memory

Because the kernel has:

  • Full privileges

  • Hardware access

it can safely perform these operations.

Step 7: Result Returned

After execution:

  • Return value generated

  • Error codes set if necessary

Step 8: CPU Switches Back to User Mode

The kernel restores:

  • Saved CPU context

  • Previous execution state

Execution resumes in:

  • User mode

The application continues normally.

Complete Flow Visualization

User Program
      ↓
System Call Invocation
      ↓
Trap Instruction
      ↓
Switch to Kernel Mode
      ↓
Kernel Validates Request
      ↓
Kernel Performs Operation
      ↓
Return Result
      ↓
Switch Back to User Mode

4.3 Categories of System Calls

Operating systems provide different categories of system calls based on functionality.

Process Control System Calls

These manage processes and program execution.

Examples:

  • fork()

  • exec()

  • exit()

  • wait()

fork()

Creates a new process.

exec()

Replaces current process image with another program.

exit()

Terminates a process.

File Management System Calls

These manage files and directories.

Examples:

  • open()

  • read()

  • write()

  • close()

open()

Opens a file.

read()

Reads data from file.

write()

Writes data to file or device.

close()

Closes file descriptor.

Device Management System Calls

Used for:

  • Device communication

  • Device configuration

  • I/O control

Examples:

  • ioctl()

Information Maintenance System Calls

Provide system-related information.

Examples:

  • getpid()

  • time()

  • uname()

These retrieve:

  • Process ID

  • System time

  • System information

Communication System Calls

Support:

  • Inter-process communication

  • Networking

  • Message passing

Examples:

  • pipe()

  • socket()

  • send()

  • recv()

4.4 Example (Linux)

Example of a simple Linux system call:


Explanation

write()

The write() function is a system call that requests the operating system to output data.

Parameters:

  • 1 → Standard output (terminal)

  • "Hello\n" → Data to print

  • 6 → Number of bytes

Internal Operation

When write() executes:

  1. Trap instruction generated

  2. CPU enters kernel mode

  3. Kernel validates request

  4. Terminal device accessed

  5. Data printed

  6. CPU returns to user mode

The application itself never directly controls the hardware.

4.5 Key Design Principle

The most important principle behind system calls is:

Controlled entry into the kernel

Applications cannot arbitrarily execute privileged operations.

Instead:

  • The operating system exposes carefully designed interfaces.

Advantages:

  • Security

  • Stability

  • Resource management

  • Controlled hardware access

The kernel decides:

  • Which operations are allowed

  • Which permissions are required

  • How resources are allocated

This creates:

A secure boundary between user programs and the operating system.

4.6 Performance Consideration

Although system calls are essential, they are relatively expensive operations compared to normal function calls.

Why System Calls Are Expensive

Mode Switching Overhead

System calls require:

  • User mode → Kernel mode transition

  • Kernel mode → User mode transition

These privilege changes are costly.

Context Saving and Restoring

CPU state must be:

  • Saved before entering kernel mode

  • Restored before returning

This increases execution overhead.

Validation and Protection Checks

The kernel performs:

  • Permission checks

  • Memory validation

  • Security verification

These operations consume additional CPU time.

Cache and Pipeline Effects

Mode switching may:

  • Disrupt CPU pipelines

  • Affect cache performance

reducing efficiency.

System Call vs Function Call

FeatureFunction CallSystem Call
Execution ModeUser ModeSwitches to Kernel Mode
Privilege ChangeNoYes
Performance CostLowHigher
Hardware AccessNoYes
Context SwitchingNoRequired

Optimization Techniques

Modern operating systems attempt to reduce system call overhead using:

  • Fast system call instructions

  • Shared memory

  • Buffering

  • Asynchronous I/O

Examples:

  • sysenter

  • syscall instructions

Importance of System Calls

System calls are fundamental to:

  • Process management

  • File systems

  • Networking

  • Security

  • Device interaction

Every modern application depends heavily on them.

Examples:

  • Browsers opening files

  • Databases allocating memory

  • Servers handling network sockets

All require system calls.

Real-World Analogy

Imagine a secure bank vault.

User Application

A customer cannot directly access the vault.

System Call

The customer submits an official request through authorized staff.

Kernel

The bank manager verifies permissions and safely performs the operation.

Similarly:

  • Applications request privileged operations through system calls rather than directly accessing hardware.