5. System Calls in Operating Systems

Tanish KumarTanish Kumar
3 min read

1. What is a System Call?

Definition:
A system call is a request made by a user program to the OS kernel to perform tasks that require privileged hardware access (e.g., file creation, memory allocation).

Why Needed?

  • User applications run in user mode (restricted access).

  • Kernel runs in kernel mode (full hardware access).

  • System calls bridge this gap via the System Call Interface (SCI).

Analogy:

  • User Mode = Customer at a restaurant (orders food).

  • Kernel Mode = Chef in the kitchen (executes orders).

  • System Call = Waiter (delivers order to kitchen).


2. How System Calls Work

Step-by-Step Flow:

  1. User Request: e.g., mkdir Movies (CLI) or "New Folder" (GUI).

  2. Trigger SCI: Command reaches the System Call Interface.

  3. Mode Switch: Software interrupt switches CPU to kernel mode.

  4. Kernel Execution: Kernel’s C code creates the folder on disk.

  5. Return to User Mode: Confirmation sent back to user space.

Example:

$ mkdir Movies  # User command  
↓  
SCI maps to `sys_mkdir()` in kernel  
↓  
Kernel creates folder → Returns success message

3. Types of System Calls

CategoryPurposeExamples (Linux/Windows)
Process ControlCreate/end processesfork(), exit(), wait()
File ManagementRead/write filesopen(), read(), chmod()
Device ManagementControl hardware (printers, USB)ioctl(), read(), write()
InformationGet system data (time, process ID)getpid(), time(), alarm()
CommunicationIPC (pipes, shared memory)pipe(), shmget(), mmap()

4. Key Examples Explained

(A) Process Control: fork()

  • What? Creates a child process (clone of parent).

  • Code:

      #include <unistd.h>
      int main() {
          fork();  // Creates child process
          printf("Hello OS!\n");
          return 0;
      }
    
  • Output:

      Hello OS!  // Parent  
      Hello OS!  // Child
    

(B) File Management: open() & write()

  • What? Creates/writes to a file.

  • Terminal Demo:

      $ echo "Hello" > file.txt  # Uses `open()` and `write()` internally.
    

(C) Communication: pipe()

  • What? Creates a channel for inter-process communication.

  • Use Case:

    • Process A writes to pipe → Process B reads from it.

5. Practical Demo: From Terminal

Creating a Script (Hello.sh)

  1. Write Script:

     #!/bin/bash
     echo "Hello CodeHelp"
     sleep 10  # Uses `sleep()` system call
    
  2. Run Script:

     $ chmod +x Hello.sh  # `chmod()` system call
     $ ./Hello.sh         # `fork()` + `exec()`
    
  3. Monitor Process:

     $ ps -a              # `getpid()` system call
     $ kill -9 [PID]      # `kill()` system call
    

6. Why System Calls Matter

  1. Security: Prevents apps from directly accessing hardware.

  2. Abstraction: Simplifies complex operations (e.g., mkdir vs. manual disk allocation).

  3. Standardization: Uniform interface across applications.


7. Interview Questions

  1. How does mkdir work internally?

    • Triggers sys_mkdir() in kernel via SCI.
  2. Difference between fork() and exec()?

    • fork() clones a process; exec() replaces it with a new program.
  3. What happens during a mode switch?

    • CPU saves user mode state → Switches to kernel mode via interrupt.

8. Homework

  1. Explore man pages:

     $ man 2 kill    # System call documentation
     $ man 2 open
    
  2. Experiment:

    • Use strace to trace system calls:

        $ strace ./Hello.sh
      

Key Takeaways

  1. System calls = Gateway between user apps and kernel.

  2. 5 Types: Process, File, Device, Info, Communication.

  3. Real-World Use: Every CLI/GUI action (e.g., file creation) relies on system calls.

0
Subscribe to my newsletter

Read articles from Tanish Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanish Kumar
Tanish Kumar