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.

No comments:

Post a Comment