System calls (abbreviated to syscalls) are used for everything that works with an operating system and is a fundamental aspect of computing.

  • Opening a file?
  • Allocating memory?
  • Networking?
  • Controlling user hardware and input?

It’s a system call. This is because all of these things are managed by the kernel (your operating system). But why?

The user and the kernel

Before you learn how system calls are used, you need to know the distinction between the user space and the kernel space.

User space

The user space refers to the environment where code is executed outside of the kernel. This means most of your code falls under user space, as they run as a process of your operating system.

Processes

A process is the instance of a computer program that is being executed by one or many threads. Process (computing)

User level programs are ran in an unique, restricted environment. This ensures that the operating system and the hardware of the user’s computer is protected from any crashes and failure.

This is done by:

  • limiting the virtual address space of that program
  • limiting the amount of system resources given to that program
  • restricting access to internal hardware directly

So if these protections are in place, how do software and applications implement these features?

Kernel space

All code executed in the kernel space has unrestricted access to hardware and resources. However, it’s a single virtual address space - so malicious code and crashes can cause the crash of the entire operating system.

So what are syscalls?

Since the user space restricted to ensure safety, the user space needs a way to contact the kernel space and request whatever they need.

These requests are system calls - they act as a bridge between the user space and the kernel space.

https://phoenixnap.com/kb/wp-content/uploads/2023/08/system-call-steps-execution.png phoenixnap.com

By handling kernel code like so, it ensures safety and provides an abstraction that allows user space programs to easily ‘swap’ into kernel space, request whatever it needs from the kernel space and continue on with execution.

If you want to learn more, watch 99% of Developers Don’t Get System Calls by The Coding Gopher.