0
0
Operating Systemsknowledge~5 mins

System calls and their role in Operating Systems - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: System calls and their role
O(n)
Understanding Time Complexity

System calls let programs ask the operating system to do tasks like reading files or creating processes.

We want to understand how the time taken by system calls grows as the number of calls increases.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


for (int i = 0; i < n; i++) {
    int fd = open("file.txt", O_RDONLY);
    read(fd, buffer, size);
    close(fd);
}
    

This code opens, reads, and closes a file n times in a loop.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The loop runs n times, each time making three system calls: open, read, and close.
  • How many times: Each system call happens once per loop iteration, so n times total.
How Execution Grows With Input

As n grows, the total system calls grow directly with n because each iteration does the same fixed work.

Input Size (n)Approx. Operations
1030 system calls (10 open, 10 read, 10 close)
100300 system calls (100 open, 100 read, 100 close)
10003000 system calls (1000 open, 1000 read, 1000 close)

Pattern observation: The total work grows steadily and directly with n.

Final Time Complexity

Time Complexity: O(n)

This means the time needed grows in a straight line as the number of system calls increases.

Common Mistake

[X] Wrong: "System calls are instant, so their time cost doesn't add up."

[OK] Correct: Each system call involves switching from user mode to kernel mode, which takes time, so many calls add up to more time.

Interview Connect

Understanding how system calls scale helps you reason about program efficiency and resource use in real systems.

Self-Check

"What if we opened the file once before the loop and only read and closed inside the loop? How would the time complexity change?"