Os Notes
Os Notes
UNIT-I
Computer hardware refers to the physical components that make up a computer system.
These components interact with the OS to perform tasks and execute programs.
Major Components:
An Operating System (OS) is system software that manages hardware and software
resources and provides common services for computer programs. The OS serves as an
intermediary between users and the computer hardware, providing a user interface and
managing resources such as CPU, memory, and devices.
Batch OS: Executes jobs in a batch without user interaction (e.g., early mainframe
systems).
Time-Sharing OS: Allocates CPU time to different processes in a way that gives
users the illusion of simultaneous execution (e.g., UNIX, Linux).
Distributed OS: Manages a collection of independent computers and makes them
appear as a single system (e.g., Google’s Android OS).
Embedded OS: Designed to run on embedded systems with specific functions, such
as in smartphones or smart TVs.
Real-Time OS: Used when timely execution is critical, often in systems like
industrial machines, medical devices, or robotics (e.g., QNX).
4. System Calls:
System calls provide an interface between user programs and the operating system kernel.
They are used for services such as input/output, memory allocation, process creation, and file
management. Examples include:
Different operating systems have different internal architectures. Common structures include:
Monolithic Kernel: A single large kernel that directly interacts with hardware and
provides core services like process management, memory management, file systems,
etc. (e.g., Linux).
Microkernel: A minimal kernel that only handles basic functions like
communication, with additional services (e.g., file management) provided by user-
level processes. (e.g., Minix, QNX).
Layered System: The OS is divided into layers, each responsible for specific tasks.
Lower layers handle basic hardware interactions, while upper layers provide user-
facing functionality.
Modular Structure: The OS is divided into modules that can be loaded or unloaded
dynamically. This allows the OS to be flexible and extensible (e.g., modern Linux
systems).
6. Important OS Concepts:
1. What is a Process?
A process is a program in execution, which consists of the program code, data, and
the state of the program (e.g., program counter, registers).
Each process has its own memory space, execution context, and system resources.
2. Process States:
4. Process Scheduling:
Process scheduling refers to the method by which the operating system decides
which process will run next.
Key scheduling algorithms:
o First-Come-First-Served (FCFS)
o Shortest Job Next (SJN)
o Round Robin (RR)
o Priority Scheduling
5. Process Creation:
A new process is created using the fork() system call in Unix-like systems or similar
calls in other OSs.
The parent process creates a child process.
The child process can be an exact copy of the parent (via exec()) or run a different
program.
Processes often need to communicate with each other. IPC mechanisms include:
o Shared Memory: Multiple processes can access a common memory region.
o Message Passing: Processes send and receive messages via communication
channels.
o Pipes and Sockets: Used for communication between processes in
Unix/Linux.
7. Context Switching:
Context switching is the process of saving and restoring the state (context) of a
process when switching from one process to another.
It involves saving the PCB of the currently running process and loading the PCB of
the next process to be executed.
9. Synchronization:
1. Processes:
2. Threads:
A thread is the smallest unit of execution within a process. A process can contain
multiple threads, all sharing the same memory space but executing independently.
Threads are sometimes referred to as "lightweight processes" because they share
resources within the process but can still execute concurrently.
Shared Resources: Threads within the same process share resources like memory,
file handles, and I/O devices.
Concurrency: Multiple threads can run within the same process.
Faster Context Switching: Switching between threads is generally faster than
switching between processes because threads share memory.
IPC refers to the mechanisms that allow processes to communicate with each other.
Since processes are isolated, they need to use special methods to exchange data or
coordinate actions.
4. Scheduling:
Process Scheduling refers to the technique that the operating system uses to manage
the execution of processes on the CPU. Scheduling algorithms determine the order in
which processes are given CPU time.
First-Come, First-Served (FCFS): Processes are scheduled in the order they arrive.
Shortest Job Next (SJN): The process with the smallest execution time is scheduled
next.
Round Robin (RR): Each process is given a fixed time slice, and then the next
process is scheduled.
Priority Scheduling: Processes are assigned priorities, and the highest-priority
process is scheduled first.
5. IPC Problems:
Race Conditions: Occur when multiple processes or threads access shared resources
concurrently and at least one of them modifies the resource. This can lead to
unpredictable results.
Locks: Ensure that only one process/thread accesses a shared resource at a time.
Semaphores: Use semaphores to control access to shared resources and prevent race
conditions.
Deadlock: A situation where two or more processes are blocked forever because they
are waiting for each other to release resources.
Deadlock Prevention/Resolution Strategies:
Solutions to Starvation:
Aging: Gradually increase the priority of a process that has been waiting too long,
ensuring that it eventually gets CPU time.
UNIT-II
Here are some key points to consider for memory management without abstraction:
1. Address Spaces:
Address Space refers to the range of addresses that a process can use to access
memory.
Logical Address Space: The set of addresses generated by the CPU during a
program’s execution.
Physical Address Space: The set of addresses in the physical memory (RAM).
The OS maps logical addresses to physical addresses using hardware and software
mechanisms.
2. Virtual Memory:
Virtual memory allows processes to use a larger address space than is physically
available by swapping data between RAM and disk storage.
It enables programs to use more memory than the system can physically provide,
improving efficiency and flexibility.
Key Components:
o Page Table: Maps virtual pages to physical frames.
o Page Fault: Occurs when a program accesses a part of memory that is not
currently in physical memory (requires fetching from disk).
o Segmentation: Divides memory into segments (e.g., code, data, stack) for
more efficient access.
These algorithms manage which pages should be swapped out when a page fault occurs,
aiming to optimize memory usage and minimize the number of page faults.
Page Size: Small page sizes result in higher page table sizes, while larger pages can
waste memory due to internal fragmentation.
Page Table Management: Efficient handling of page tables is crucial for system
performance. Multi-level page tables or inverted page tables are used to manage large
address spaces.
TLB (Translation Lookaside Buffer): A cache for frequently accessed page table
entries to reduce the time spent on address translation.
5. Implementation Issues:
Memory Protection: Ensuring that a process cannot access memory belonging to
other processes.
Sharing of Pages: Some pages (e.g., code) may be shared between processes, but
others (e.g., private data) should not be.
Context Switching: When switching between processes, the system must save and
restore the state of the page tables and memory mappings.
Hardware Support: Modern processors support memory management features like
hardware-based page tables and TLBs.
6. Segmentation:
UNIT-III
Deadlocks: Notes
1. Resources:
2. Introduction to Deadlocks:
The ostrich algorithm is a strategy to ignore the problem of deadlocks and hope it
doesn't occur.
It is often used in situations where deadlocks are considered rare or when the cost of
dealing with deadlocks is higher than ignoring them.
Detection: Involves checking the system for cycles in the resource allocation graph or
using a wait-for graph to identify deadlocked processes.
o Can be done periodically or when resources become unavailable.
Recovery:
o Process Termination: Abort one or more processes to break the deadlock.
This could involve aborting processes until the deadlock cycle is eliminated.
o Resource Preemption: Forcefully take resources from some processes to give
them to others, breaking the cycle.
5. Deadlock Avoidance:
Techniques:
6. Deadlock Prevention:
Efficiency: Deadlock detection can consume resources and time. The system must
balance detecting deadlocks while maintaining performance.
Fairness: When recovering from deadlocks (e.g., process termination), fairness
should be ensured to avoid starvation of certain processes.
System Complexity: Techniques like deadlock avoidance and prevention can add
complexity to the system and may not always be feasible in real-time systems.
1. Overview of Linux
2. Linux Goals
Open-source: To make the operating system free for anyone to use, modify, and
distribute.
Multitasking: Linux is designed to handle multiple tasks concurrently.
Portability: It is platform-independent, meaning it can run on a wide variety of
hardware architectures.
Security: Built-in security features like access control and user authentication.
Performance: Optimized for high performance, especially in server and network
environments.
Stability: Known for reliability and uptime, making it ideal for servers and critical
systems.
3. Interfaces to Linux
Command-Line Interface (CLI): The most direct way to interact with Linux, using
terminal commands.
Graphical User Interface (GUI): Many Linux distributions come with a GUI, such
as GNOME or KDE, providing a more user-friendly interface.
Network Interfaces: Linux supports many networking protocols (TCP/IP, etc.) for
networking with other systems.
4. The Shell
The Shell is a command interpreter that provides a user interface to the Linux system.
Common shells include:
o Bash (Bourne Again Shell)
o Zsh (Z Shell)
o Fish (Friendly Interactive Shell)
The shell allows users to execute commands, run scripts, and manage processes.
It can be run interactively or in batch mode.
Linux comes with a variety of utility programs to perform specific tasks, such as:
o File management: ls, cp, mv, rm, mkdir, etc.
o Text processing: grep, awk, sed, cut, etc.
o System monitoring: ps, top, htop, df, du, etc.
o Networking: ping, ifconfig, ssh, scp, wget, etc.
o Disk management: fdisk, mount, umount, fsck, etc.
o Package management: apt, yum, rpm, pacman, etc.
6. Kernel Structure
The Kernel is the core part of Linux and manages hardware resources like CPU,
memory, and I/O devices.
It provides services like process management, memory management, file system
management, device drivers, and networking.
Key components:
o Process Scheduler: Manages process execution and multitasking.
o Memory Manager: Handles the system's memory, including virtual memory.
o File System: Manages data storage and retrieval.
o Device Drivers: Provide communication between the kernel and hardware.
o System Calls: Interface between user programs and the kernel.
History of Android
Android Architecture
Linux Kernel: The foundation of Android is the Linux kernel, providing low-level
system functions such as memory management, process management, and hardware
abstraction.
Libraries: Android uses various native libraries written in C and C++, such as
WebKit, OpenGL, and SQLite, to provide functionality for applications.
Android Runtime (ART): Replaces Dalvik in newer Android versions, executing
apps written in Java or Kotlin. ART compiles the code to native instructions at install
time, improving performance.
Application Framework: Provides the high-level API used by developers to interact
with the system, including user interface components, content providers, and location-
based services.
Applications: Android apps are built using Java or Kotlin and run on the Android
Runtime.
Android extends the Linux kernel with its own set of services and APIs that are tailored for
mobile devices, such as:
Android Applications
Android applications are primarily written in Java or Kotlin and packaged in APK (Android
Package) files. Each app runs in its own virtual machine, and developers interact with
Android's extensive APIs to build apps that can run on various devices.
History of Windows
MS-DOS-Based Windows:
o Windows 1.0 (1985) was the first graphical user interface (GUI) on top of
MS-DOS. It was limited in functionality, but it introduced basic elements like
windows, menus, and dialog boxes.
o Windows 3.x (1990-1994) significantly improved the interface and added
support for multitasking and virtual memory.
o Windows 95 (1995) was a major leap forward with the introduction of a 32-
bit preemptive multitasking system, a new taskbar, and the Start menu.
NT-Based Windows:
o Windows NT (1993) was designed as a more powerful, secure, and stable
operating system. It introduced a new kernel architecture and was the
foundation for later versions of Windows, such as Windows 2000, XP, and
later.
o Windows XP (2001) merged the consumer-friendly interface of Windows
95/98 with the stability of NT, and it became one of the most widely used
versions.
Modern Windows:
o Windows Vista (2007) and Windows 7 (2009) focused on improving
security, user interface, and system stability. Windows 7 was especially
popular due to its performance improvements over Vista.
o Windows 8 (2012) introduced a new, tablet-friendly interface with Live Tiles,
but was criticized for its departure from the traditional desktop interface.
o Windows 10 (2015) combined the traditional desktop experience with features
from Windows 8, providing a more user-friendly, unified operating system
across devices.
o Windows 11 (2021) introduced a redesigned Start menu, rounded corners, and
improved performance, with a focus on productivity, gaming, and integration
with cloud services.
UNIT-IV
Open Source: Linux is open-source software, meaning its source code is freely
available for modification and distribution.
Multitasking: Linux supports multitasking, allowing multiple programs to run
simultaneously.
Multiuser: Linux allows multiple users to use the system at the same time without
interfering with each other's work.
Portability: Linux can run on many different hardware platforms, including desktops,
servers, mobile devices, and more.
Security: Linux has a robust security model, including user permissions, file
encryption, and security patches.
Networking: Linux is well-suited for networking tasks, supporting a wide range of
network protocols.
Shell Interface: Linux offers a powerful command-line interface (CLI) in addition to
graphical user interfaces (GUIs).
Package Management: Linux uses package managers like apt, yum, pacman, etc., to
easily install, update, and remove software.
Advantages of Linux
Cost-Effective: Linux is free to use, and most of its software is also open source.
Stability and Reliability: Linux is known for being highly stable and less prone to
crashes.
Customizability: Linux is highly customizable, allowing users to change almost any
part of the system according to their needs.
Community Support: Being open-source, Linux has a large, active community of
developers and users who provide support.
Performance: Linux is known for its high performance, particularly in server
environments.
Security: Linux has strong security mechanisms, with built-in tools like firewalls,
SELinux, and AppArmor.
Software Availability: Many professional tools, programming languages, and
enterprise applications are available for Linux.
Resource Efficient: Linux can run on older hardware and has low system resource
requirements.
1. Kernel:
o The kernel is the core part of the Linux system. It interacts with the hardware
and manages system resources like CPU, memory, and storage.
o The kernel handles low-level tasks like process management, device drivers,
file systems, and system calls.
2. System Library:
o These are essential programs that provide functions to applications. Most
libraries are written in C (e.g., the GNU C Library, glibc).
o Libraries help in performing tasks like system calls or file operations without
having to access the kernel directly.
3. System Utilities:
o These are programs that help manage the system. Examples include
command-line utilities (ls, grep, cp, mkdir, etc.) and system management
tools.
4. Shell:
o The shell is an interface for user interaction with the system. It interprets and
executes user commands (e.g., bash, zsh).
o It can be used to execute commands directly or run shell scripts.
5. User Space:
o This is the environment where user applications run. It includes user
applications, system libraries, and utilities.
o User space is isolated from the kernel to maintain system stability.
6. File System:
o Linux uses a hierarchical file system where everything is treated as a file (e.g.,
devices, directories, etc.).
o It follows a standard file system structure with directories like /home, /bin,
/usr, and /etc.
7. Device Drivers:
o Linux uses device drivers to interact with hardware. These drivers are part of
the kernel and enable the system to communicate with various hardware
components.
Here are notes on some essential Linux commands related to files and directories:
1. cd (Change Directory)
Syntax: cd <directory>
Changes the current working directory to the specified one.
Example:
bash
CopyEdit
cd /home/user/Documents
2. cp (Copy Files)
bash
CopyEdit
cp file1.txt /home/user/Documents/
cp -r folder1 /home/user/Backup/ # for copying directories
bash
CopyEdit
mv file1.txt /home/user/Docs/ # move file
mv oldname.txt newname.txt # rename file
Syntax: rm <file>
Removes a file. To remove a directory, use -r for recursive deletion.
Example:
bash
CopyEdit
rm file1.txt # remove a file
rm -r foldername # remove a directory and its contents
rm -f file1.txt # force deletion without confirmation
bash
CopyEdit
mkdir new_folder
mkdir -p parent/child # create parent directory if it doesn’t exist
bash
CopyEdit
more file1.txt
bash
CopyEdit
less file1.txt
bash
CopyEdit
cat file1.txt
cat file1.txt file2.txt > combined.txt # combine files
9. File Comparisons
bash
CopyEdit
diff file1.txt file2.txt
bash
CopyEdit
cmp file1.txt file2.txt
bash
CopyEdit
df -h # Human-readable format
du (Disk Usage)
o Syntax: du <file|directory>
o Displays disk usage for files or directories.
o Example:
bash
CopyEdit
du -sh /home/user/Documents # shows the size of the directory
bash
CopyEdit
lsblk
bash
CopyEdit
fdisk /dev/sda
bash
CopyEdit
ls -l # long listing with permissions, owner, size, and date
ls -a # shows hidden files
bash
CopyEdit
pwd
bash
CopyEdit
touch newfile.txt
bash
CopyEdit
chmod 755 script.sh
bash
CopyEdit
chown user:group file1.txt
UNIT-V
Understanding Shells:
Shell: A command-line interface (CLI) that allows users to interact with the operating
system. Common Linux shells include bash (Bourne Again Shell), sh (Bourne Shell),
zsh, and others.
Interactive Shell: This is the shell session you use directly, where you can run
commands and see output.
Non-Interactive Shell: Shells that run scripts and don’t interact with users directly.
Processes in Linux:
Batch Commands:
bash
CopyEdit
#!/bin/bash
echo "Starting process..."
sleep 2
ps aux
kill 1234
File-Related Commands:
Mathematical Commands
1. bc (Basic Calculator)
o bc is a command-line calculator in Unix-like systems. It can handle arbitrary
precision arithmetic.
o To start:
bash
CopyEdit
bc
o Example:
bash
CopyEdit
5+3
o You can also use -l for the math library (e.g., sine, cos).
bash
CopyEdit
bc -l
o To perform a more complex calculation:
bash
CopyEdit
echo "scale=2; 10/3" | bc
2. expr
o expr is used to evaluate expressions and perform basic arithmetic operations.
o Example:
bash
CopyEdit
expr 5 + 3
bash
CopyEdit
a=5
b=3
expr $a + $b
3. factor
o factor is used to find the prime factors of a number.
o Example:
bash
CopyEdit
factor 100
Output:
makefile
CopyEdit
100: 2 2 5 5
4. units
o units is a command-line tool for converting between different units of
measurement.
o Example:
bash
CopyEdit
units "10 kilometers" "miles"
Output:
CopyEdit
10 kilometers = 6.2137 miles
Text Editors
1. vi (Visual Editor)
o vi is a classic, powerful text editor available on most Unix systems.
o Launch:
bash
CopyEdit
vi filename
o Modes:
Normal mode: For navigation, editing, and commands.
Insert mode: For typing text (press i to enter).
Command mode: For commands like save, quit (:w, :q).
o Example:
To enter insert mode: i
To save: :w
To quit: :q
To save and quit: :wq
2. joe (Joe's Own Editor)
o joe is a simple and user-friendly text editor.
o Launch:
bash
CopyEdit
joe filename
o Basic commands:
Move cursor: Arrow keys
Insert text: Start typing.
Save: Ctrl+K, then X
Exit: Ctrl+K, then Q
3. vim (Vi Improved)
o vim is an improved version of vi with more features like syntax highlighting,
better configuration, etc.
o Launch:
bash
CopyEdit
vim filename
o Similar to vi, it has modes (normal, insert, and command), but with extra
features like autocompletion, color schemes, and more.
o To install plugins and customize vim, you can modify ~/.vimrc.
Shell programming is a way to automate tasks and manage the system using shell
scripts.
A shell script is a text file that contains a series of commands for the shell to execute.
The script can be executed by typing the script's name at the command prompt,
provided it has execution permissions.
Bash (Bourne Again Shell): The most common shell used in Linux.
Bourne Shell (sh): An older shell, but still in use for script compatibility.
C Shell (csh): A shell with syntax similar to C programming language.
Korn Shell (ksh): Combines features from both the Bourne and C Shells.
Z Shell (zsh): An extended version of bash with additional features.
Bash is widely used for scripting due to its simplicity and compatibility with most
Linux distributions.
A basic bash script starts with a shebang (#!/bin/bash), followed by commands.
If-Else Statement:
bash
CopyEdit
if [ condition ]; then
command1
else
command2
fi
For Loop:
bash
CopyEdit
for var in 1 2 3 4; do
echo $var
done
While Loop:
bash
CopyEdit
while [ condition ]; do
command
done
5. Case Statements
bash
CopyEdit
case "$var" in
pattern1)
command1 ;;
pattern2)
command2 ;;
*)
default_command ;;
esac
bash
CopyEdit
./script.sh arg1 arg2
7. Shell Variables
Defining Variables:
bash
CopyEdit
var="value"
Accessing Variables:
bash
CopyEdit
echo $var
8. Shell Keywords
bash
CopyEdit
grep "pattern" file
bash
CopyEdit
awk '{print $1, $2}' file
You can use it for processing and manipulating data in files, such as extracting
specific columns or performing arithmetic.