OS Unit2 Detailed Notes
OS Unit2 Detailed Notes
Table of Contents
Overview
An operating system provides an environment in which programs can be executed. In order to fulfil
this role, it offers a defined set of services — both to the user who interacts with the system and to
the system itself so that it can operate efficiently. These services can be divided into two broad
categories: those that benefit the user, and those that benefit the system.
For the User Make the system convenient and UI, Program Execution, I/O Operations, File System
usable Manipulation, Communication, Error Detection
For the System Ensure efficient and secure Resource Allocation, Accounting, Protection and
operation Security
CLI Command Line Interface The user types text-based commands directly using the keyboard. The
shell reads each command and executes it.
GUI Graphical User Interface The user interacts through windows, icons, menus, and a pointing
device (mouse/touchpad). Examples: Windows, macOS.
Batch Batch Interface Commands are written into a file in advance. The entire file is
submitted to the system and executed as a batch, without user
interaction during execution.
2. Program Execution
The operating system is responsible for loading a program from secondary storage into main memory and
executing it. Once the program finishes its task, the OS must be able to end it, either normally (when the
program completes successfully) or abnormally (when an error occurs that prevents normal completion).
The OS handles both scenarios and reports appropriate status information to the user.
3. I/O Operations
While a program is running, it may need to perform input or output operations — for example, reading data
from a file or sending output to a printer. A running program is not permitted to directly access I/O devices,
because doing so without control could lead to errors or data corruption. The operating system therefore
acts as an intermediary: it provides a safe, controlled mechanism through which programs can request I/O
operations. This protects both the device and the data.
5. Communication
There are situations where one process needs to exchange information with another process — either on
the same computer or on a different computer connected through a network. The operating system
facilitates this through two primary models:
Shared Memory Two or more processes are permitted to Fast — operates at memory speed. Both
access a common region of memory. They processes must agree to share and must
read and write to this shared space to coordinate their access carefully.
exchange information.
Message The operating system transfers packets of Slower but safer and easier to use,
Passing information (messages) from one process to especially across different machines.
another. Processes do not share any memory
directly.
6. Error Detection
The operating system must constantly monitor the system for errors and respond appropriately. Errors can
arise from multiple sources:
• Hardware errors: Memory faults, power failures, or failures in the CPU itself.
• I/O device errors: A parity error on a disk, a printer that has run out of paper, or a network
connection failure.
• Program errors: Division by zero, an attempt to access a memory location that the program is not
permitted to use, or excessive consumption of CPU time.
When an error is detected, the OS responds in one of three ways: it may halt the entire system (for critical
hardware errors), terminate only the offending process, or return an error code to the program so that it
can handle the problem itself.
Category B — Services Provided for Efficient System Operation
7. Resource Allocation
When several users or processes are running simultaneously, the OS must allocate the available
resources — CPU time, main memory, storage, and peripheral devices such as printers — to each of them
in a fair and efficient manner. For the CPU, the OS uses scheduling algorithms that consider factors such
as processor speed, the number of available registers, and the number of processes waiting to run.
8. Accounting
The OS keeps a record of which users use which resources, and for how long. This information serves two
purposes: it can be used for billing users in commercial systems, and it provides usage statistics that
administrators and researchers can use to reconfigure the system for improved performance.
Protection Ensures that all access to system resources — CPU, Internal (between processes on
memory, files, devices — is controlled and authorised. the same system)
Prevents one process from accessing another's
resources.
Security Defends the system against unauthorised external External (hackers, unauthorised
access. This begins with user authentication (passwords, users)
biometrics) and includes monitoring all connections to
the system to detect and record intrusion attempts.
EXAM LINE: "A chain is only as strong as its weakest link." — This is the standard justification for why all
components of a system must be protected, not just some of them.
→ What is the role of error detection in an OS? Give examples of different types of errors. (5 marks)
→ Explain shared memory and message passing as methods of communication between processes. (5
marks)
MEMORY TRICK: "UI-PIFCE" — UI, Program execution, I/O, File system, Communication, Error detection
(the 6 user services) | "RAP" — Resource allocation, Accounting, Protection & Security (the 3 system
services)
CHAPTER 2.2
What Is a Shell?
A shell, also called a command interpreter, is a special system program whose primary function is to
receive commands from the user and cause the operating system to execute them. It acts as the
interface between the human user and the operating system kernel. The shell does not itself carry
out the operations — it passes the instructions to the OS and displays the results.
In some operating systems, the command interpreter is part of the kernel itself. In others — such as
Windows and UNIX/Linux — it runs as a separate program that is launched when the user logs in.
Systems that provide multiple command interpreters refer to each one as a shell.
Bourne Shell The original shell of UNIX. All other shells are, in some way, derived from or
compared to it.
Bourne-Again Shell (bash) The most widely used shell today. It is the default shell in most Linux
distributions.
Korn Shell Combines the best features of the Bourne shell and the C shell.
Code inside the The shell itself contains the code to MS-DOS As more commands are
interpreter execute each command. When a added, the shell grows larger.
command is typed, the shell runs the It becomes difficult to
corresponding code from within itself. maintain and update.
Approach How It Works Used By Drawback
System programs The shell does not understand commands UNIX/Linux None — this approach is
(external files) at all. It simply uses the command name to highly flexible and easy to
find a separate file (program) with that extend.
name, loads it into memory, and executes
it.
IMPORTANT: User interface design is NOT considered a direct function of the operating system. It
belongs to the system programs layer, not the kernel.
→ What is a command interpreter? Explain the two approaches to implement commands in a CLI. (5
marks)
→ How does UNIX implement the 'rm' command? Which approach does it use? (3 marks)
MEMORY TRICK: "Shell = Translator" — it translates human commands into operating system actions.
CHAPTER 2.3
System Calls
A system call is the mechanism through which a user-level program requests a service from the
operating system. It provides a programmatic interface between a running program and the services
made available by the OS kernel. System calls are the only legitimate way for a program to cross
from user mode into kernel mode.
Every program runs in user mode — a restricted mode of operation in which the program cannot directly
access hardware, memory of other processes, or critical OS data structures. When a program needs to
perform a privileged operation — such as reading a file, allocating memory, or communicating over a
network — it must make a system call. The CPU then switches to kernel mode, the OS performs the
requested service, and control is returned to the program.
System calls are primarily written in C or C++. For very low-level hardware tasks, assembly language is
used.
1 Display a prompt asking for the input file name Write to screen
2 Read the input file name entered by the user Read input
3 Display a prompt asking for the output file name Write to screen
4 Read the output file name entered by the user Read input
5 Open the input file; if not found, print error and File open / error handling / abort
abort
6 Create the output file; if it already exists, ask user File create / read input
to overwrite or abort
7 Loop: read a block from the input file, write it to File read / file write (repeated)
the output file
NOTE: Even a simple program can make thousands of system calls per second. This illustrates how
fundamental system calls are to the operation of any program.
An Application Programming Interface (API) is a set of functions made available to the programmer
that specify: what parameters each function accepts, what operations it performs, and what values it
returns. Internally, these API functions make the actual system calls on behalf of the programmer.
Programmers almost always use APIs rather than making system calls directly. There are two primary
reasons for this:
• Portability: Code written using a standard API (such as POSIX) will run on any system that supports
that API, without modification.
• Simplicity: System calls are complex, require precise parameter values, and differ between
operating systems. APIs hide this complexity behind simple, well-documented function calls.
Example: When a programmer calls printf() in C, the C standard library internally calls the write() system
call, which causes the OS to send output to the screen. The programmer never interacts with the system
call directly.
Registers The parameters are placed directly into Basic systems The number of parameters is
the CPU's registers before the system limited by the number of CPU
call is made. The OS reads them from registers available.
there.
Block / Table The parameters are stored in a Linux, Solaris None — there is no limit on
contiguous block (table) in memory. the number or size of
The address of that block is placed in a parameters.
register. The OS reads the address and
retrieves the parameters from memory.
Stack The program pushes the parameters Some operating None — there is no limit on
onto the process stack before making systems the number or size of
the system call. The OS pops them off parameters.
the stack to retrieve them.
KEY POINT: The Block and Stack methods are preferred over Registers because they do not impose any
limit on the number or length of parameters that can be passed.
→ What is a system call? Why do programmers use APIs instead of direct system calls? (5 marks)
→ Explain the three methods of passing parameters to the OS during a system call. (5 marks)
→ With the help of an example, explain how system calls are used in a simple file copy program. (10
marks)
→ What is an API? Name the three most common APIs used in programming. (3 marks)
MEMORY TRICK: "Menu = API, Kitchen = System Call" — The API is the menu you order from; the
system call is the actual cooking that happens behind the scenes. | "RBS" — Registers, Block/Table, Stack
(3 parameter passing methods)
CHAPTER 2.4
4 Information Maintenance Getting and setting system information, time, and date
end() / abort() Stop a program. end() indicates normal completion; abort() indicates an
error has caused abnormal termination.
load() / execute() Load another program into memory and begin its execution.
wait_time() / wait_event() / Cause a process to pause for a specified amount of time, or wait until a
signal_event() particular event occurs. Another process can signal that the event has
happened.
acquire_lock() / release_lock() Lock a shared resource so that only one process can use it at a time,
preventing simultaneous conflicting writes.
MS-DOS vs FreeBSD — A Comparison
These two systems illustrate contrasting approaches to process management:
Concurrency Only one process can run at a Multiple processes can run simultaneously.
time.
Creating a new process No new process is created. The Uses fork() to create a new child process that
shell overwrites itself to give is a copy of the parent.
maximum memory to the
program.
Loading a program The shell is overwritten in Uses exec() to replace the child process's
memory by the new program. memory with a new program.
While program runs The shell is gone — it no longer The shell remains running in the background.
exists in memory.
When program ends MS-DOS reloads its shell from The child process calls exit(), returning a
disk. status code to the parent.
KEY CALLS IN FreeBSD: fork() — creates a new process (a copy of the calling process). exec() —
replaces the calling process's program with a new program. exit() — terminates the process and returns a
status code (0 = success, non-zero = error).
→ What is fork()? What is exec()? How are they used in FreeBSD? (5 marks)
create() / delete() Create a new file or remove an existing one from the file system.
open() / close() Open a file before reading or writing to it, establishing a connection between
the program and the file. Close it when operations are complete.
System Call What It Does
read() / write() Read data from a file into the program, or write data from the program into
the file.
reposition() Move the file pointer to a different location within the file, allowing
non-sequential access.
get_file_attributes() / Read or modify a file's attributes, such as its name, type, protection codes,
set_file_attributes() and size.
→ List and explain the file management system calls provided by an OS. (5 marks)
request() / release() Request exclusive access to a device before using it. Release the device
when done so that other processes can use it.
read() / write() / reposition() Perform the same read, write, and seek operations on devices that are
performed on files.
get_device_attributes() / Read or modify the settings of a device, such as the baud rate of a serial
set_device_attributes() port.
attach / detach devices Logically connect a device to the system (mount) or disconnect it (unmount).
IMPORTANT — UNIX Similarity: In UNIX, devices and files are treated almost identically. The same
system calls — read(), write() — work on both files and devices. This design simplifies programming and
creates a uniform interface for all I/O.
WARNING: If devices are not properly managed, two processes may simultaneously attempt to use the
same device, leading to a deadlock — a situation where neither process can proceed. (Deadlocks are
covered in detail in Chapter 7.)
→ Explain device management system calls. How are devices similar to files in UNIX? (5 marks)
time() / date() Retrieve the current date and time from the system clock.
get_system_data() Retrieve system-level information such as the number of logged-in users, the
OS version, the amount of free memory, and the amount of free disk space.
dump() Write the current contents of memory to disk. This is used for debugging —
when a program crashes, the memory dump is saved and later examined by
a debugger.
get/set_process_attributes() Read or modify the attributes of a running process (e.g., its priority, status, or
time limits).
Single-step mode A debugging feature in which the CPU executes exactly one instruction, then
generates a trap (interrupt) so the debugger can inspect the system state
before the next instruction runs.
Time profile Uses timer interrupts to record how much CPU time the program spends at
each point in its code. This helps identify performance bottlenecks.
Type 5 — Communication
Communication system calls allow processes to exchange information with one another. There are two
models:
Speed Slower — data is copied by the OS from Faster — data is accessed directly
one process to another. in memory.
→ Explain the two models of communication used in OS. Compare message passing and shared memory.
(10 marks)
→ What is a daemon? What role does it play in message passing communication? (3 marks)
Type 6 — Protection
Protection system calls are used to control access to the resources of the system. They allow
administrators and programs to specify who can access what, and under what conditions.
System Call What It Does
set_permission() / get_permission() Set or retrieve the access permissions on files, directories, or disks (e.g.,
read-only, read-write, execute).
allow_user() / deny_user() Explicitly allow or block a specific user from accessing a specific
resource.
In the early days of computing, protection was only a concern for large multi-user systems. Today, with the
widespread use of networking and the internet, even single-user devices such as mobile phones require
robust protection mechanisms.
→ What are protection system calls? List any four examples. (3 marks)
→ List all six types of system calls and give two examples of each. (10 marks)
MEMORY TRICK: "PF-DIC-P" — Process control, File management, Device management, Information
maintenance, Communication, Protection | "fork = photocopy yourself, exec = become someone else" |
"WhatsApp = Message Passing, Google Docs = Shared Memory"
CHAPTER 2.5
System Programs
System programs, also called system utilities, are a collection of programs that provide a convenient
environment for program development and execution. They sit as a layer between the operating
system kernel and the application programs that users install. Most users interact with system
programs far more than with the OS kernel itself — in fact, what many users think of as 'the operating
system' is largely the collection of system programs that come pre-installed with the OS.
Layer Examples
Application Programs (top) Web browsers, word processors, games — installed by the user
System Programs File manager, text editor, compiler, Task Manager — come with the OS
1. File Management
These programs create, delete, copy, rename, print, list, and otherwise manage files and directories. They
provide a user-friendly way to perform the same operations that file management system calls perform at
the program level.
Examples: File Explorer (Windows), and commands such as ls, cp, mv, rm in Linux.
2. Status Information
These programs query the system for current status information and display it to the user. The types of
information they can provide include:
• Current date and time
• Amount of available memory and disk space
• Number of users currently logged in
• Performance metrics, error logs, and debugging data
Some operating systems store configuration information in a centralised database called the registry.
Status programs can read from and write to the registry to retrieve or update configuration settings.
Example: Task Manager (Windows), top / htop (Linux).
3. File Modification
These programs allow users to create and modify files stored on disk. They include text editors as well as
specialised tools for searching within files or performing transformations on file content.
Examples: Notepad (Windows), Vim, Nano, and Gedit (Linux).
Compiler Translates the entire source code of a program into GCC (for C/C++)
machine language in one step.
Interpreter Executes source code line by line at runtime, without Python interpreter
compiling it first.
Relocatable loader Loads the program at any available memory location, adjusting addresses as
needed.
Linkage editor Combines multiple separately compiled program files (object files) into a single
executable.
Overlay loader Loads only the currently needed part of a very large program into memory,
swapping parts in and out as required.
Debugger Used after loading to trace and fix errors in the program during execution.
6. Communications
These programs provide the mechanisms for users and processes to communicate with each other, both
on the same machine and across a network. They include programs that allow users to:
• Send messages to the screens of other users on the same system
• Browse web pages through a web browser
• Send and receive electronic mail
• Log into a remote computer and execute commands on it
• Transfer files between two different machines
A daemon is a system program process that starts automatically at boot time and continues running
in the background until the system is shut down. Daemons are never directly visible to the user but
provide essential services to the OS and other programs.
Network daemon Listens continuously for incoming network connection requests and handles
them.
Process scheduler daemon Starts processes or jobs at pre-scheduled times (e.g., nightly backups).
Error monitoring daemon Continuously watches for system errors and logs them or alerts administrators.
Print server daemon Manages the queue of print jobs and sends them to the printer in order.
Some daemons complete a specific task and then stop. Others run indefinitely until the system is shut
down. A typical operating system has dozens of daemons running at any given time.
→ What are system programs? List and explain their categories with examples. (10 marks)
MEMORY TRICK: "FS-FP-LCB" — File management, Status information, File modification, Programming
language support, Loading and execution, Communications, Background services
CHAPTER 2.7
Introduction
A modern operating system is an extremely large and complex piece of software. It must be designed with
care so that it functions correctly, can be maintained over time, and can be modified or extended without
breaking existing functionality. The fundamental principle is to divide the OS into smaller, manageable
components. There are four principal approaches to structuring an OS:
In a simple or monolithic structure, the OS is not divided into clean, separate modules. All
components of the kernel — memory management, file system, device drivers, CPU scheduling —
are combined into a single large program running in kernel mode. Different parts of the kernel can
call any other part directly, without restriction.
MS-DOS as an Example
MS-DOS was written by a small team with the goal of providing maximum functionality in minimum
memory space. Because it was never expected to become widely used, it was not designed with a clean
modular structure. The consequences were significant:
• The system was not divided into proper modules — all layers of the OS could communicate with all
other layers directly.
• Application programs could directly access hardware — the display adapter, disk controller — without
going through the OS.
• This made the system highly vulnerable: a single poorly written or malicious program could corrupt
the entire system or cause it to crash.
• The Intel 8088 processor on which MS-DOS ran had no dual-mode operation (no user mode / kernel
mode distinction), so hardware-level protection was not even possible.
Aspect Detail
Advantage Very fast execution, because all kernel components can communicate directly with no
overhead from passing messages or switching layers.
Aspect Detail
Disadvantage The kernel is enormous, extremely difficult to implement, and very hard to maintain or
modify.
In the layered approach, the operating system is organised as a hierarchy of layers. The bottommost
layer (Layer 0) is the hardware. The topmost layer (Layer N) is the user interface. Each layer is built
on top of the layer below it, and may only use the services provided by the layers directly beneath it.
No layer may call a layer above it.
Layer Component
… Higher-level OS services
Advantages
• Simple construction: Each layer is built and tested in isolation, using only the verified lower layers.
• Easy to debug: Debugging proceeds from the bottom up. If a bug is found in Layer 3, the
programmer already knows that Layers 0, 1, and 2 are correct. This greatly narrows the search for the
cause.
• Information hiding: Each layer only needs to know what the layers below it provide — not how they
provide it. Implementation details are hidden.
Disadvantages
• Difficult to define layers correctly: Deciding which component belongs in which layer requires
careful planning. For example: the disk driver must be below the memory manager (because the
memory manager may need to use the disk). But the CPU scheduler may need to be above the disk
driver (because scheduling may depend on disk activity). Working out the correct ordering for all
components is complex.
• Less efficient: A system call must pass through every layer between the user and the hardware.
Each layer adds a small amount of time overhead. In a system with many layers, this cumulative
overhead significantly reduces performance.
• Modern systems use fewer layers with more functionality in each to reduce this overhead.
→ Explain the layered approach of OS structure. What are its advantages and disadvantages? (10 marks)
→ Why is the layered approach less efficient than a monolithic structure? (3 marks)
Structure 3 — Microkernel
The microkernel approach structures the OS by removing all non-essential components from the
kernel and implementing them as user-level programs in user space. The result is a very small kernel
that contains only the absolute minimum required for the system to function.
HISTORICAL NOTE: The microkernel approach was developed in the mid-1980s at Carnegie Mellon
University. The operating system produced was called Mach.
Advantages
• Easy to extend: New services can be added by creating new user-space server processes. The
kernel itself does not need to be modified or recompiled.
• Easy to port: Because the kernel is small, it is much easier to adapt it to run on a new hardware
architecture.
• More reliable and secure: If a server process (e.g., the file system server) crashes, only that service
is affected. The kernel itself continues to run. In a monolithic kernel, a crash in any driver or module
can bring down the entire system.
• Smaller kernel = fewer bugs: A smaller codebase is easier to verify, test, and maintain.
Disadvantages
• Performance overhead: All communication between components must go through the microkernel's
message passing mechanism. This involves two context switches and two memory copies for every
interaction, which is significantly more expensive than a direct function call within a monolithic kernel.
• Real-world example: Windows NT was originally designed as a microkernel OS. Its performance
was so poor that many services had to be moved back into the kernel. Windows XP and later versions
are therefore more monolithic than the original NT design.
Mac OS X (Darwin) The core of macOS is partly based on the Mach microkernel.
QNX A real-time OS widely used in embedded and safety-critical systems. Its microkernel
handles only message passing, process scheduling, and hardware interrupts.
→ What is a microkernel? Explain its structure, advantages and disadvantages with examples. (10 marks)
The modular approach organises the kernel around a small set of core components, with additional
functionality implemented as separate modules that can be loaded into the kernel either at boot time
or dynamically during runtime. This is the approach used by most modern operating systems,
including Linux, Solaris, and Mac OS X.
Key Characteristics
• The kernel provides essential services: CPU scheduling, basic memory management.
• Any additional service — a new file system, a new device driver, a new network protocol — can be
implemented as a module and loaded when needed.
• Modules can be loaded at boot time (before the OS is fully running) or at runtime (while the system is
already in use), without rebooting the system.
• No need to recompile the kernel to add new functionality.
3 Loadable system calls Add new system calls without recompiling the kernel
7 Device and bus drivers Support new hardware devices and buses
→ What are loadable kernel modules? How do they differ from the layered and microkernel approaches?
(5 marks)
Organisation No structure; all in Strict hierarchy of Tiny kernel + Core kernel + plug-in
one layers user-space servers modules
Speed Fast — direct calls Slow — overhead Slow — message Fast — direct module
at each layer passing calls
→ Compare and contrast the four OS structures: simple, layered, microkernel and modular. (10 marks)
MEMORY TRICK: "SLMM" — Simple, Layered, Microkernel, Modules | "Monolithic = Messy room (no
organisation) | Layered = Relay race (strict order) | Microkernel = Minimalist apartment (bare essentials
only) | Modules = Smartphone + apps (core + add what you need)"
KEY DEFINITIONS
System calls
System calls provide an interface to the services made available by an operating system.
API
Security
A chain is only as strong as its weakest link. — The standard argument for why every component of a
system must be secured.
Protection
User Authentication
Security of the system from outsiders starts with requiring each user to authenticate himself or herself
to the system, usually by means of a password.
The main function of the command interpreter is to get and execute the next user-specified command.
In UNIX, the command interpreter does not understand the command in any way; it merely uses the
command to identify a file to be loaded into memory and executed.
Message Passing
Message passing is useful for exchanging smaller amounts of data, because no conflicts need be
avoided.
Shared Memory
Shared memory allows maximum speed and convenience of communication, since it can be done at
memory transfer speeds.
System Programs
System programs provide a convenient environment for program development and execution.
Daemon
Microkernel
The microkernel approach structures the OS by removing all non-essential components from the
kernel and implementing them as system and user-level programs.
Layered Approach
The main advantage of the layered approach is simplicity of construction and debugging.
The kernel has a set of core components and links in additional services via modules, either at boot
time or during run time.
Best of luck for your exams. You have covered everything in this unit.