Friday, December 19, 2025

System calls in os

System calls (syscalls) are the programmatic way for an application to request services from the operating system's kernel. They act as a vital bridge between user-space applications (with limited privileges) and the kernel (with full hardware access). 

How System Calls Work:

  1. User Mode: The application runs in a restricted "user mode" where it cannot directly touch hardware or memory of other processes.
  2. The Request: When it needs a service (like saving a file), it issues a system call instruction (often called a trap or software interrupt).
  3. Mode Switch: The CPU switches from user mode to kernel mode, giving the OS full authority to perform the task safely.
  4. Execution & Return: The kernel executes the requested service, then switches the CPU back to user mode and returns the results to the application. 
Types of System Calls with Examples
CategoryPurposeUnix/Linux ExampleWindows Example
Process ControlCreating, stopping, or managing programs.fork()exit()wait()CreateProcess()ExitProcess()
File ManagementOpening, reading, writing, or deleting files.open()read()write()close()CreateFile()ReadFile()
Device ManagementInteracting with hardware like printers or disks.ioctl()read()write()SetConsoleMode()ReadConsole()
Information MaintenanceGetting/setting system time or attributes.getpid()sleep()alarm()GetCurrentProcessID()Sleep()
CommunicationExchanging data between different programs.pipe()shmget()mmap()CreatePipe()MapViewOfFile()
ProtectionControlling access permissions.chmod()chown()SetFileSecurity()
Real-World Example: Saving a Document
When you click "Save" in a text editor like Notepad:
  1. The editor calls an API function (e.g., WriteFile in Windows).
  2. This triggers a system call to the kernel.
  3. The kernel verifies your permissions and physically writes the data to your SSD/HDD.
  4. Once done, the kernel tells the editor "Success," and you can continue typing.

Linker in OS

A linker (or link editor) is a system utility that combines multiple separately compiled object files and library modules into a single, cohesive executable file. It serves as a bridge between the translation phase (compilation/assembly) and the execution phase. 

Key Functions of a Linker

  • Symbol Resolution: The linker scans object files to match function and variable references (symbols) with their actual definitions. For example, if a program calls printf(), the linker finds the implementation of printf in the standard C library.
  • Relocation: Since compilers typically assume a program starts at address zero, the linker "relocates" these code and data segments to their final assigned memory addresses.
  • Code Consolidation: It merges various sections (like .text for code and .data for variables) from different modules into one continuous block. 


Types of Linking
Feature Static LinkingDynamic Linking
TimingPerformed during compilationPerformed at load or run time
File SizeLarger (includes all library code)Smaller (only contains references)
MemoryRedundant; each program has its own copyEfficient; multiple programs share one copy
UpdatesRequires recompilation if libraries changeLibraries can be updated independently
ExampleWindows .exe or Linux .a filesWindows .dll or Linux .so files
Practical Examples
  • C Programming: When you run the command gcc main.c utils.c -o myprog, the compiler first generates main.o and utils.o. The linker then "glues" these together with the standard C library to create the final myprog executable.
  • Shared Libraries: Most modern programs on Windows use dynamic linking for the DirectX or C++ Runtime libraries. This allows many different games or apps to use the same system files rather than each app carrying its own 100MB copy.

loader in os

A loader is a system utility and an essential component of an operating system responsible for taking an executable file (generated by the linker) from secondary storage (disk) and placing it into the main memory (RAM) for execution. It acts as the final bridge between static code on your disk and a running process in the CPU. 

Core Functions of a Loader
The loader typically performs four fundamental tasks to prepare a program: 
  1. Allocation: Reserving the required space in main memory based on the program's size.
  2. Linking: Resolving symbolic references between different modules or libraries that were not settled during static linking.
  3. Relocation: Adjusting internal memory addresses within the code so it can run correctly at its assigned location in RAM.
  4. Loading: Physically copying the machine code and data into the allocated memory space. 
Types of Loaders
  • Absolute Loader: Loads the program into a fixed, predetermined memory location. It is simple but inflexible because the program cannot be moved if that specific memory spot is occupied.
  • Relocating Loader: Allows a program to be loaded anywhere in available memory by modifying addresses on the fly during the loading process.
  • Dynamic Loader: Loads libraries and modules only when they are actually needed during program execution, which saves memory.
  • Bootstrap Loader: A specialized loader located in ROM that starts as soon as the computer is turned on; its only job is to load the primary operating system into RAM. 
Example Scenario
When you double-click a game icon on Windows:
  1. The OS Loader (invoked by a system call like execve() on Linux) reads the game's executable file.
  2. It checks for dependencies (like DirectX.dll) and finds space for them in memory.
  3. It allocates RAM for the game's code, player data, and sound files.
  4. Finally, it jumps the CPU's instruction pointer to the game's "entry point," and the game starts running.