System Calls
What is a System Call?
A system call is a mechanism through which a user-level application requests a service from the operating system kernel.
In simple words:
System Call = A controlled way for a program to ask the OS to perform a privileged operation.
Applications normally cannot directly access hardware or critical kernel resources. Instead, they use system calls.
Basic flow
+-------------------------+ | Application | | Chrome / VS Code | +-------------------------+ | | System Call ↓ +-------------------------+ | Kernel | | | | Process / Memory / File | | Device / Network etc. | +-------------------------+ | ↓ +-------------------------+ | Hardware | +-------------------------+
Why Do We Need System Calls?
Suppose a program wants to read a file.
The program should not directly control the SSD/HDD because that would be unsafe.
Instead:
Program ↓ "Please read this file" ↓ System Call ↓ Kernel ↓ File System ↓ Storage Device
The kernel checks the request, performs the operation, and returns the result.
This provides:
- Security
- Controlled hardware access
- Resource management
- Process isolation
- Abstraction from hardware details
User Mode and Kernel Mode
System calls are closely related to user mode and kernel mode.
User Mode
Normal applications execute here.
Examples:
Chrome VS Code VLC Games Python programs
Applications have limited privileges.
Kernel Mode
The OS kernel executes here.
The kernel has privileged access to:
- CPU control
- Memory
- Storage
- Devices
- Network interfaces
- Other protected resources
The simplified flow is:
User Mode | | System Call ↓ Kernel Mode | | Perform operation ↓ Hardware
After completing the operation, control returns to the application.
How a System Call Works
Consider a program that wants to read a file.
1. Application requests operation ↓ 2. Application invokes system call ↓ 3. CPU transfers control to kernel ↓ 4. Kernel validates the request ↓ 5. Kernel performs requested operation ↓ 6. Kernel returns result ↓ 7. Application continues execution
For example:
Application | | read() ↓ System Call Interface | ↓ Kernel | ↓ File System | ↓ Disk
System Call Interface
The System Call Interface (SCI) provides the mechanism through which programs request kernel services.
+-----------------------+ | Application | +-----------------------+ ↓ +-----------------------+ | System Call Interface | +-----------------------+ ↓ +-----------------------+ | Kernel | +-----------------------+ ↓ +-----------------------+ | Hardware | +-----------------------+
The application doesn't need to know the internal implementation of the kernel.
It simply requests a service.
Major Types of System Calls
System calls can be grouped into several categories:
- Process Control
- File Management
- Device Management
- Information Maintenance
- Communication
- Protection
Let's understand each.
1. Process Control System Calls
These system calls deal with creating, executing, and terminating processes.
Typical operations include:
- Create a process
- Terminate a process
- Execute a program
- Wait for a process
- Allocate process resources
Examples in Unix/Linux include:
fork() exec() exit() wait()
Example: fork()
fork() creates a new process.
Parent Process | fork() | +--+--+ ↓ ↓ Parent Child
The child process is created from the calling process.
2. File Management System Calls
These system calls are used to work with files.
Common operations:
- Create a file
- Open a file
- Read a file
- Write a file
- Close a file
- Delete a file
Examples:
open() read() write() close() unlink()
Example:
Program | | open("notes.txt") ↓ Kernel | ↓ File System | ↓ Storage
3. Device Management System Calls
These system calls allow programs to request services from devices.
Examples of devices:
- Keyboard
- Mouse
- Printer
- Disk
- Network interface
- USB device
Operations may include:
- Request device
- Release device
- Read device
- Write device
- Configure device
In Unix/Linux, operations can often use interfaces such as:
read() write() ioctl()
4. Information Maintenance System Calls
These system calls provide or modify information about the system.
Examples:
- Get current time
- Get process ID
- Get system information
- Get/set attributes
- Get user information
Examples in Unix/Linux include:
getpid() getuid() uname()
For example:
getpid()
returns the process ID of the calling process.
5. Communication System Calls
These system calls allow processes to communicate with each other.
This is called Inter-Process Communication (IPC).
Common mechanisms include:
- Pipes
- Shared memory
- Message queues
- Sockets
- Signals
For example:
Process A | | Message ↓ Kernel | ↓ Process B
Network communication can also use system-level interfaces such as sockets.
6. Protection System Calls
These system calls manage permissions and access to system resources.
They can be used for:
- File permissions
- User identity
- Access control
- Security attributes
Examples in Unix/Linux include:
chmod() chown() setuid()
For example:
chmod()
can modify the permissions associated with a file.
Common System Calls in Linux/Unix
| System Call | Purpose |
|---|---|
fork() | Create a process |
exec() | Execute a program |
exit() | Terminate a process |
wait() | Wait for a child process |
open() | Open a file |
read() | Read data |
write() | Write data |
close() | Close a file |
unlink() | Remove a file |
getpid() | Get process ID |
getuid() | Get user ID |
chmod() | Change file permissions |
chown() | Change file owner |
pipe() | Create a pipe for IPC |
socket() | Create a communication endpoint |
System Call vs Normal Function
This is an important distinction.
A normal function generally executes within the application's own execution context.
A system call requests a service from the OS kernel.
For example:
Normal function: Program ↓ Function ↓ Program System call: Program ↓ System Call ↓ Kernel ↓ Operation ↓ Program
Some programming-library functions may internally invoke one or more system calls.
Example: printf() vs write()
Consider C programming:
printf("Hello");
printf() is a library function, not itself a raw system call.
The library may eventually use a lower-level mechanism such as:
write()
to send output to a file descriptor.
Conceptually:
printf() ↓ C Library ↓ write() system call ↓ Kernel ↓ Terminal/device
This distinction is important because library APIs and system calls are not always the same thing.
System Call and CPU Privilege
Modern CPUs provide privilege mechanisms.
A simplified model is:
User Mode ↓ System Call Instruction ↓ Kernel Mode ↓ Kernel executes service ↓ Return to User Mode
The exact instruction and mechanism depend on the CPU architecture and operating system.
For example, Linux on modern x86-64 systems commonly uses the syscall instruction for entering the kernel for system calls.
System Call Number
Operating systems typically associate system calls with identifiers.
Conceptually:
System Call Number ↓ Kernel ↓ Corresponding system-call handler
For example:
Application ↓ system call number + arguments ↓ Kernel ↓ Handler
The exact numbers depend on the operating system and architecture.
Arguments to System Calls
A system call may require arguments.
For example:
open("notes.txt", flags)
The kernel needs information such as:
- Which file?
- What access mode?
- What flags?
Another example:
write(fd, buffer, size)
The kernel needs:
fd → file descriptor buffer → data location size → number of bytes
Return Value
After performing a system call, the kernel generally returns a result.
For example:
System Call ↓ Operation successful ↓ Return value
Or:
System Call ↓ Error ↓ Error indication
For example, read() may return the number of bytes actually read, or an error indication if the operation fails.
System Calls and File Descriptors
Unix/Linux systems commonly represent open files and many I/O resources using file descriptors.
For example:
0 → Standard Input 1 → Standard Output 2 → Standard Error
Then:
write(1, "Hello", 5)
conceptually asks the kernel to write 5 bytes to file descriptor 1, which normally refers to standard output.
Real-Life Example
Suppose you run:
cat notes.txt
A simplified view is:
cat program ↓ open() ↓ Kernel ↓ File System ↓ Disk ↓ read() ↓ Kernel ↓ write() ↓ Terminal
The cat program doesn't directly control the disk or terminal hardware.
The kernel coordinates these operations.
System Calls vs APIs
These terms are also commonly confused.
API
An API (Application Programming Interface) is an interface provided to programmers.
Examples:
C standard library POSIX APIs Windows APIs Python APIs
System Call
A system call is the mechanism used to request a kernel service.
Relationship:
Application ↓ API / Library ↓ System Call ↓ Kernel
An API function may:
- directly invoke a system call,
- invoke several system calls,
- perform work entirely in user space,
- or use another library mechanism.
Windows System Calls
Windows also uses system calls, although the public programming APIs are commonly accessed through the Windows API and related system libraries rather than directly by most application developers.
Simplified:
Application ↓ Windows API ↓ System Libraries ↓ System Call ↓ Windows Kernel ↓ Hardware
So system calls are not limited to Linux.
Advantages of System Calls
1. Security
Applications cannot freely access privileged resources.
2. Hardware Abstraction
Programs don't need to understand hardware-specific details.
3. Resource Management
The kernel controls access to CPU, memory, storage, and devices.
4. Process Isolation
Processes are prevented from freely interfering with each other.
5. Standardized Access
Programs can request common OS services through defined interfaces.
Complete System Call Flow
Remember this diagram:
+-------------------------+ | Application | +-------------------------+ | ↓ +-------------------------+ | Library / API | +-------------------------+ | ↓ +-------------------------+ | System Call | +-------------------------+ | ↓ +-------------------------+ | Kernel Mode | | | | Validate Request | | Perform Operation | | Manage Resources | +-------------------------+ | ↓ +-------------------------+ | Hardware | +-------------------------+ | ↓ Result | ↓ +-------------------------+ | Application | +-------------------------+
Quick Revision
| Concept | Meaning |
|---|---|
| System Call | Request from a program to the OS kernel |
| User Mode | Restricted application execution |
| Kernel Mode | Privileged OS execution |
| System Call Interface | Interface for requesting kernel services |
| Process Control | Create/execute/terminate processes |
| File Management | Open/read/write/close files |
| Device Management | Access and control devices |
| Information Maintenance | Get/set system information |
| Communication | IPC and communication services |
| Protection | Permissions and access control |
Exam Definition
A system call is a controlled interface through which a user-level program requests a service from the operating system kernel, such as process creation, file access, memory-related operations, device access, communication, or protection.
One-line memory trick
System Call = Application → Kernel → OS Service → Application