Operating Systems · Introduction and System Calls
Operating System as Interface Between User and Hardware
Overview
The operating system is the layer that sits between the user and the hardware (CPU, memory, printers, and other physical devices), so the user never deals with the hardware directly.
This matters because it lets a programmer write a small program, such as one using printf, without writing any of the code that actually talks to the hardware.
Key concepts
- Operating system as interface: The OS sits between the user (above) and the hardware (below), so the user never has to deal with the hardware directly.
- Hardware: The CPU, the memory, the printers, and all such physical devices.
- System call: The mechanism by which the operating system provides functionality to a program.
printfinternally makes a system call, and the real work is done by the OS in thewritesystem call. - Function call vs. system call: Any support taken from the operating system is obtained by calling something — either a function call or a system call. The two are different; the distinction is covered later.
- Transparency of implementation: The user is guaranteed that writing a command causes the action to happen, without knowing how the
writesystem call, thereadsystem call, orprintfis implemented.
How it works
- Lay out three levels: hardware (CPU, memory, printers, ...) at the bottom, the user above it, and the operating system in between.
- If the user had to access the hardware directly, he would have to get into a lot of code — he would have to write a lot of code even to execute a small function.
- Concretely, a small C function using
printfworks only becauseprintfinternally makes a system call, and it is the operating system that provides that system call. - Without an OS, you could not simply call
printf; you would first have to write the program yourself for everything, including the code that enables access to the hardware and then gives it commands. - If you had to write 10 programs with no operating system, every program would have to contain the code that first enables you to access the hardware and then give it some commands.
- With an operating system present, you need not worry about how to interact with the hardware. You just say what your program should do; the OS takes those commands and appropriately generates the system calls.
- Because many people need the same functionality, the OS implements it once instead of asking every user to implement it; users directly use that functionality and need not bother about its implementation.
Worked examples
The printf → write example
- In your program you write
printf. - The corresponding functionality of
printfis not actually insideprintf; it is done by the operating system in the system callwrite. - If there were no operating system, you would be required to write the entire code that is actually written inside
write. - Since many people need the same functionality, the OS provides it, and users can directly use it without bothering about its implementation.
Ten programs without an OS
- Without an OS, each of the 10 programs would need its own code to access the hardware and issue commands — the same work repeated 10 times.
- With an OS, that code exists once inside the OS, and each program just issues its commands.
Revision summary
- The OS is the interface between the user and the hardware; the user never touches the hardware directly.
printfdoes not itself perform the output — the OS does the real work in thewritesystem call.- System calls exist so that commonly needed functionality is implemented once by the OS and reused by everybody.
- You are guaranteed the effect of a call without knowing its internal implementation; you focus only on your own task.
- Any support from the OS is obtained by calling either a function call or a system call — they are not the same thing.
- Using hardware without an OS was tried long ago, but every program then had to write its own hardware-interaction code, which is inefficient and time consuming.
Previous-year exam insights
- The kernel is the part of the OS that lies below the system-call interface and above the physical hardware; shells, buses and stubs are not.
Asked inUGC NET 2012 - In a CPU with memory-mapped I/O there is no explicit I/O instruction, so I/O protection is ensured by the operating system (privileged OS routines / address translation), not by a hardware trap or configuration.
Asked inGATE 2005 - The core functions of an OS are controlling peripherals (I/O devices), allocating memory and allocating processor (CPU) time; organising data into fields/records and virus protection are not core OS functions.
Asked inUGC NET 2019 - UNIX treats hardware devices as files: devices are accessed through special files in the file system (e.g. /dev), so the same read/write system calls work for devices and ordinary files.
Asked inUGC NET 2008 - Interrupts are classified by source: hardware (external) interrupts come from devices such as the keyboard, mouse or disk, while software (internal) interrupts/traps come from the executing program (e.g. system calls, divide-by-zero, page faults); a CPU temperature sensor is a hardware interrupt.
Asked inGATE 2011 - Privileged (kernel-mode) instructions such as explicit I/O instructions and setting the mode bit can be executed only in kernel mode; user programs must request such services through system calls, which switch the CPU to kernel mode via a trap.
Asked inGATE 2005