Logo

dev-resources.site

for different kinds of informations.

From User to OS: Exploring System Calls with glibc through a Restaurant Analogy

Published at
5/11/2024
Categories
linux
kernel
devops
systemcall
Author
lakhera2015
Categories
4 categories in total
linux
open
kernel
open
devops
open
systemcall
open
Author
11 person written this
lakhera2015
open
From User to OS: Exploring System Calls with glibc through a Restaurant Analogy


Image Ref: https://blog.karlrock.com/top-5-five-star-indian-restaurants-that-are-almost-perfect/

https://youtu.be/g6DVAFGcizU?si=bqbiK2bkbUvQNjnu

Understanding the Linux system call concept is often challenging for Linux beginners, so here is my attempt to explain system calls in simple language and the role of glibc.

Understanding System Calls:
Imagine you are in a restaurant. The kitchen is like the core of the operating system (OS), which does all the heavy work like cooking food (i.e., managing hardware resources such as CPU, memory, and disk operations). As a customer, you cannot go into the kitchen to cook the food directly (just as a user program cannot directly access the OS resources). Instead, you give your order to a waiter (system call), who is authorized to communicate your request to the kitchen.

System calls are the mechanisms through which user programs interact with the operating system. For example, when a software needs to read a file, it requests the OS to do this by making a system call. This is similar to asking the waiter to bring a specific dish. The system call acts as a request to the OS to perform operations like reading or writing data, sending network packets, accessing hardware devices, etc.

The Role of glibc:
Now, imagine that instead of speaking directly to the waiter, you have a menu and a set of rules on how to order (this represents glibc or GNU C Library). glibc provides a comprehensive and standard way for your programs (you, as the customer) to interact with the system (kitchen) without needing to know the details of how the kitchen works.
glibc is a library that provides a wrapper around the system calls. These wrappers make it easier and safer for programs to request services from the OS. It handles many details internally, such as setting up the correct inputs for the system call, handling the invocation of the system call, and managing the results.

What Happens Under the Hood?

  • User Program Makes a Request: When you write a program in C (or any other language that uses glibc) and call a function like fopen() to open a file, your program uses glibc.
  • glibc Wraps the System Call: glibc translates your fopen() call into one or more system calls that the OS understands. For instance, it might translate fopen() into the open() system call. glibc prepares all necessary parameters to make the OS understandable of the request.
  • Switch from User Mode to Kernel Mode: System calls involve a context switch from user mode (less privileged, where user applications run) to kernel mode (highly privileged, where the OS operates). This switch is necessary because the OS needs to protect itself and the system's resources from direct and potentially harmful interactions.
  • System Call is Executed: The kernel receives the system call and performs the necessary actions (like reading the file). This is like the kitchen preparing your order.
  • Return to User Mode: Once the OS has completed the operation, the result is returned to glibc, which returns it to your program. This is similar to the waiter bringing the food back to your table.
  • Error Handling: If something goes wrong (say, if the file doesn't exist), glibc handles the error in a standardized way and informs your program accordingly.

Through this process, glibc provides a consistent interface to system services across various hardware and configurations, abstracting many complexities of direct system call use and enhancing portability and security. This ensures that, as a programmer, you can focus more on the logic of your application rather than the intricacies of the underlying operating system interactions.

đź“š If you're interested in more in-depth explanation of these topics, please check out my new book "Cracking the DevOps Interview" https://pratimuniyal.gumroad.com/l/cracking-the-devops-interview

đź“š To learn more about AWS, check out my book "AWS for System Administrators" https://www.amazon.com/AWS-System-Administrators-automate-infrastructure/dp/1800201532

kernel Article's
30 articles in total
Favicon
Ditana GNU/Linux: Unmatched Configuration Flexibility and Generic Hardware Detection
Favicon
I added networking standards to my custom kernel!
Favicon
I'm building my own kernel!
Favicon
Bash vs. Shell: The Ultimate Comparison
Favicon
x64 Virtual Address Translation
Favicon
Debian 12: NVIDIA Drivers Installation
Favicon
Exploring the Linux Kernel and Switching Kernels on Arch Linux & Snigdha OS in Short!
Favicon
All About Linux: A Comprehensive Guide
Favicon
Exploring Applications That Fully Utilize the Linux Kernel for Maximum Efficiency
Favicon
Raspberry Pi 4B: Natively Build a 64 Bit Fully Preemptible Kernel (Real-Time) with Desktop
Favicon
How to Configure a Network Block Device on a Debian-Based System
Favicon
Kernel vs Operating System
Favicon
C++ in Kernel Development: A Comprehensive Guide
Favicon
The Importance of Separating Kernels and Userspace in Modern Computing Systems
Favicon
Written by a 16 year old, a book on how computers work
Favicon
The Linux Audacity
Favicon
How to cross compile linux kernel on MacOS
Favicon
esBPF: Stress-Testing compares Software-Offload with iptables
Favicon
Commentary on CrowdStrike BSOD Root Cause Analysis Release
Favicon
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---
Favicon
eBPF: Revolutionizing Linux Kernel Programming
Favicon
The Fundamentals of Support Vector Machines
Favicon
Understanding Docker Containers: Leveraging Linux Kernel's Namespaces and cgroups
Favicon
Understanding the Structural Differences Between BSD-based Kernels and Linux Kernels
Favicon
nvidia-dkms-545 error in Ubuntu 24.04
Favicon
AppArmor testsuite
Favicon
From User to OS: Exploring System Calls with glibc through a Restaurant Analogy
Favicon
Writing a bootloader: learnings
Favicon
Instalando kernel Linux Zen no Arch Linux com systemd-boot 🧑🏽‍💻
Favicon
Kernel Internals and Kernel Module Development in Fedora Linux

Featured ones: