0
0
Operating-systemsConceptBeginner · 3 min read

What Is a System Call: Definition and Examples

A system call is a way for a program to request a service from the operating system's kernel, such as reading a file or creating a process. It acts as a controlled bridge between user programs and the core system functions to ensure safety and proper resource management.
⚙️

How It Works

Imagine you want to borrow a book from a library. You can't just take it yourself; you must ask the librarian. In this analogy, the librarian is like the operating system's kernel, and your request to borrow a book is like a system call. When a program needs to do something that requires special permission or access to hardware, it makes a system call to ask the operating system to do it on its behalf.

Technically, a system call switches the program from user mode (where it runs normally) to kernel mode (where the operating system has full control). This switch allows the OS to safely perform tasks like reading files, writing to the screen, or managing memory without risking the system's stability.

💻

Example

This example shows a simple program in C that uses the write system call to print text to the screen. The write call asks the OS to output the message to the standard output (usually the terminal).

c
#include <unistd.h>

int main() {
    const char *message = "Hello, system call!\n";
    write(1, message, 19); // 1 is the file descriptor for stdout
    return 0;
}
Output
Hello, system call!
🎯

When to Use

System calls are used whenever a program needs to interact with the hardware or perform tasks that require operating system control. For example:

  • Reading or writing files on disk
  • Creating or ending processes
  • Communicating over a network
  • Allocating or freeing memory

Developers use system calls indirectly through programming libraries, but understanding them helps when writing low-level code or debugging system issues.

Key Points

  • System calls provide a safe way for programs to use OS services.
  • They switch the CPU from user mode to kernel mode.
  • Common system calls include read, write, open, and fork.
  • Programs rarely call system calls directly; they use libraries that wrap them.

Key Takeaways

A system call lets a program request services from the operating system safely.
It switches the program from user mode to kernel mode to perform privileged tasks.
Common uses include file access, process control, and communication.
Most programs use system calls through libraries, not directly.
Understanding system calls helps with low-level programming and debugging.