Skip to main content

Command Palette

Search for a command to run...

Function Calls vs. System Calls (Without the Headache)

Lets crack your OS Interview

Updated
5 min read
Function Calls vs. System Calls (Without the Headache)

"You've written thousands of lines of code. But do you actually know what happens when your program wants to read a file, create a process, or talk to hardware?"

If an interviewer asks you this, don't panic.

You don't need to memorize an Operating Systems textbook. You just need to understand one simple idea:

Function calls are conversations within your program. System calls are conversations with the Operating System.

Let's break it down.


1. The Core Concept: User Mode vs. Kernel Mode

Imagine your computer is a high-security building.

User Mode – The Public Lobby

This is where your applications run:

  • Browsers

  • Games

  • IDEs

  • Text editors

Applications can perform computations and manage their own memory, but they cannot directly access hardware or critical system resources.

Kernel Mode – The Restricted Control Room

This is where the Operating System lives.

The kernel has unrestricted access to:

  • CPU

  • Memory

  • Disk

  • Network devices

  • Peripheral hardware

Allowing every application direct access to these resources would be a security and stability nightmare.

That's why the OS acts as a gatekeeper.


2. Function Calls: Staying Inside Your Program

Consider this code:

int add(int a, int b) {
    return a + b;
}

int result = add(5, 10);

This is a function call.

What Happens?

  • Control jumps to the add() function.

  • The calculation is performed.

  • Control returns to the caller.

Everything happens within the same process and in user mode.

Why It's Fast

There is:

  • No permission check

  • No operating system involvement

  • No mode switch

The CPU simply executes another piece of code in your program.

Typical Use Cases

  • Mathematical operations

  • Data processing

  • Business logic

  • Utility functions

Think of function calls as internal communication within your application.


3. System Calls: Asking the OS for Help

Now consider this:

int fd = open("resume.pdf", O_RDONLY);

Your application cannot directly access the disk.

Instead, it requests the Operating System to do it on its behalf.

This request is called a system call.

What Happens?

  1. The application executes a special instruction (syscall, sysenter, or an equivalent mechanism).

  2. The CPU switches from User Mode to Kernel Mode.

  3. The kernel validates the request.

  4. The kernel performs the operation.

  5. Control returns to the application in User Mode.

Why It's Slower

A system call requires:

  • Entering kernel mode

  • Performing security checks

  • Executing kernel code

  • Returning back to user mode

This mode transition introduces overhead.

Note: A system call causes a mode switch, not necessarily a full process context switch. The operating system may continue running the same process after servicing the request.

Typical Use Cases

  • Reading or writing files

  • Creating processes

  • Allocating resources

  • Network communication

  • Accessing hardware devices


4. Function Call vs. System Call

Feature Function Call System Call
Runs In User Space User Space → Kernel Space
OS Involvement No Yes
Mode Switch No Yes
Speed Very Fast Relatively Slower
Purpose Internal program logic Access OS services and hardware
Example add(5, 10) open("file.txt")

A simple way to remember it:

Function Call = Solve it yourself. System Call = Ask the Operating System to solve it for you.


5. Five System Call Categories Every Interviewer Loves

You don't need to memorize dozens of system calls.

Just remember these five categories and one example from each.

1. Process Control

Used for creating and managing processes.

Examples:

  • fork()

  • exec()

  • exit()

Interview answer:

"These system calls help create, execute, and terminate processes."


2. File Management

Used for interacting with files.

Examples:

  • open()

  • read()

  • write()

  • close()

Interview answer:

"Applications use file management system calls to access data stored on disk."


3. Device Management

Used for interacting with hardware devices.

Examples:

  • Reading keyboard input

  • Sending data to a printer

  • Device-specific I/O operations

Interview answer:

"The kernel provides controlled access to hardware devices through system calls."


4. Information Maintenance

Used for retrieving system information.

Examples:

  • getpid()

  • getuid()

  • time()

Interview answer:

"These system calls allow programs to query information about themselves or the system."


5. Communication

Used for Inter-Process Communication (IPC).

Examples:

  • pipe()

  • socket()

  • shmget()

Interview answer:

"These system calls allow different processes to exchange data."


Interview Rapid-Fire Question

Q: Why can't applications directly access hardware?

Because unrestricted hardware access would compromise:

  • Security

  • Stability

  • Resource isolation

The Operating System acts as a controlled intermediary.


The 30-Second Interview Answer

If you're short on time, say this:

"A function call executes code within the application's own address space and remains in user mode, making it very fast. A system call is a request to the Operating System for privileged services such as file access, process creation, or device interaction. It requires switching from user mode to kernel mode, which introduces overhead but ensures security and controlled access to hardware."

If you can confidently explain that, you've already covered what most interviewers expect from this topic.