OS notes for final
OS notes for final
Memory Management
Memory Management is an important function of the Operating System. A computer uses RAM
(main memory) to store programs and data while they are running. A process cannot run until
its required code and data are loaded into RAM. In a multiprogramming system, many processes
use memory at the same time. Memory management makes sure that processes get the required
memory and do not interfere with each other. It also allows processes to share memory when
they have permission. The main goal of memory management is to use RAM efficiently so that
more processes can run.
1. Relocation
Relocation means changing the logical address of a program into its actual physical address
in RAM. A process may not always be loaded at the same memory location. The Operating
System can move a process from one memory location to another. Therefore, the system must
find the correct physical location of the process before execution.
Example:
Suppose a program is first loaded at memory location 1000. Later, it is moved to location 5000.
Relocation helps the system find the new location 5000 correctly.
2. Protection
Protection means protecting the memory of one process from other processes without
permission. Many processes can run in RAM at the same time, so each process must be
protected. A process should not be allowed to read, change, or delete another process's data. The
processor checks the memory access and stops unauthorized access.
Example:
Suppose Process A has its data in memory. Process B tries to change Process A's data without
permission. Memory protection will stop Process B from accessing it.
3. Sharing
Sharing means allowing two or more processes to use the same memory when they have
permission. Sharing saves memory because the same program code or data does not need to be
stored many times. The Operating System controls the shared memory and allows only
authorized processes to use it.
Example:
Suppose two processes are using the same text editor program. Instead of keeping two copies
of the program code in RAM, both processes can share one copy. This saves memory.
Time Sharing
Time Sharing System
A Time Sharing System is an Operating System in which many users or processes share the
CPU at the same time. Each process gets the CPU for a small amount of time, called a time
slice. When its time is finished, the CPU is given to the next process. This switching happens
very quickly, so users feel that they are using the computer at the same time.
1. Interactive System
In a Batch System, the user cannot interact with the program while it is running. If there is a
problem, the user usually has to wait until the job finishes. In a Time Sharing System, the user
can interact with the program during execution. The user can enter commands, give input,
and see the current status of the job. This makes debugging and correcting errors easier.
Example:
If a program asks for input, the user can enter the input immediately and see the result.
2. Time Slicing
The CPU is the most important resource in a Time Sharing System. Each process gets the CPU
for a small fixed time, called a time slice. When the time slice ends, the CPU switches to the
next process. This is usually done using Round Robin scheduling. The CPU switches between
processes very quickly.
Example:
Suppose three users have tasks A, B, and C. The CPU works on A for a short time, then B, then
C, and again A. This continues until the tasks are completed.
A Time Sharing System allows many users and many processes to use the computer
simultaneously. Several jobs must be ready in memory so the CPU can quickly switch between
them. The Operating System uses CPU scheduling, memory management, and protection to
manage these jobs. Jobs may also be swapped in and out of memory. Virtual memory can be
used when a complete job cannot fit into RAM.
Example:
Many students can use one central computer through a network. One student may run a program
while another is editing a file, and another is compiling code. The CPU gives each task a small
amount of time.
1. Single-Processor System
A Single-Processor System is a computer system that has one main processor (CPU) to
perform the main processing tasks. The CPU executes user programs and instructions. The
system may also have special processors for devices like keyboard, disk, or graphics, but the
main processing is done by one CPU.
Example:
A normal personal computer with one main CPU is a single-processor system.
Advantages
Simple: Easy to design and manage.
Low Cost: Usually cheaper than multi-processor systems.
Easy Maintenance: Easier to maintain.
Suitable for Normal Work: Good for basic tasks and applications.
2. Multi-Processor System
A Multi-Processor System is a computer system that has two or more processors (CPUs) that work
together. Multiple processors can perform different tasks at the same time, which increases the
processing power and allows the system to complete more work in less time.
Example:
A computer with 4 CPUs can use one CPU for a browser, another for a compiler, and other
CPUs for different tasks.
Advantages
High Speed: Multiple processors can work at the same time.
More Work: It can handle many tasks simultaneously.
Increased Reliability: If one processor fails, other processors may continue working.
3. Distributed System
A Distributed System is a system in which multiple separate computers work together
through a network. Each computer has its own processor and memory. These computers
communicate with each other and share resources to complete tasks.
Example:
In a university, many computers connected through a network can share files, printers, and other
resources.
Advantages
Resource Sharing: Computers can share files, printers, and other resources.
High Reliability: If one computer fails, other computers can continue the work.
Speedup: Tasks can be divided among different computers.
Scalability: More computers can be added when more power is needed.
Definition: A clustered system is a group of computers that work together as one system. They
share work and resources to improve performance and reliability.
Example: Two or more servers working together to provide the same service.
2. Real-Time Systems
Definition: A real-time system is a system that must give a response within a fixed time. It is
used where a delay can cause problems.
Example: Airbag system in a car.
3. Hand-Held Systems
Definition: Hand-held systems are small and portable computer systems that are easy to carry
and use.
Example: Smartphone or tablet.
4. Multimedia Systems
Definition: A multimedia system is a system that can handle text, images, audio, video, and
animation.
Example: A computer playing a video with sound and images.
Interrupts
An interrupt is a signal that temporarily stops the current work of the CPU and tells it that an
important event needs attention. The CPU handles the interrupt using an Interrupt Service
Routine (ISR) and then returns to the previous program.
Example:
When you press a key on the keyboard, an interrupt tells the CPU that a key has been pressed.
The CPU handles it and then continues its previous work.
Classes of Interrupts
1. Supervisor Call Interrupt (SVC): Generated when a program requests a service from
the Operating System.
2. I/O Interrupt: Generated by an I/O device when it needs the CPU's attention.
3. External Interrupt: Generated by an event outside the CPU, such as a timer or hardware
signal.
4. Restart Interrupt: Used to restart or recover the system or a program after a specific
event.
5. Program Check Interrupt: Generated when a program makes an error, such as an
invalid instruction.
6. Machine Check Interrupt: Generated when there is a serious hardware or machine
error.
1. User Mode
In User Mode, normal user programs run with limited access to system resources. A user
program cannot directly access important system functions or hardware.
Example: When you open a browser or MS Word, it normally runs in User Mode.
2. Kernel Mode
Kernel Mode is also called Supervisor Mode, System Mode, or Privileged Mode. In this
mode, the Operating System has full access to hardware and system resources.
Manages input and output devices such as keyboard, mouse, printer, and disk.
5. File Management
6. Protection System
7. Networking
Provides a way for users to interact with the computer, such as GUI or command line.
2. Program Development
3. Program Execution
6. Error Detection
Detects and handles errors in hardware, memory, programs, and I/O devices.
System Calls
Definition:
A System Call is a way for a user program to communicate with the Operating System and
request its services. It provides an interface between the user program and the OS. When a
program needs to use a system resource, the request is handled by the kernel for security and
proper control.
Example:
When a program wants to open or read a file, it uses a system call to ask the Operating System
to perform this task.
Passing Parameters
Parameters are the information sent by a program to the Operating System. There are three
common methods:
2. File Management
3. Device Management
4. Information Maintenance
5. Communication
CPU Scheduling
CPU Scheduling is the process of selecting which process will use the CPU next. When many
processes need the CPU, the Operating System decides which process should run first. This helps
to use the CPU efficiently and gives each process a chance to run.
Example:
If Process A, B, and C are waiting for the CPU, the Operating System selects one process, gives
it the CPU, and then selects the next process.
Multiprogramming
Multiprogramming is a technique in which the Operating System keeps
multiple processes in memory and switches the CPU between them. When
one process is waiting for I/O, the CPU is given to another ready process. The
main goal is to keep the CPU busy and increase CPU utilization.
Example:
Suppose Process A is waiting for a file to be read. Instead of keeping the CPU idle, the OS gives
the CPU to Process B. When A is ready again, it can get the CPU.
Ready Queue
The Ready Queue is a list of processes that are ready and waiting to use the CPU. When the
CPU becomes available, the Operating System selects a process from the ready queue and gives
it the CPU.
If A runs completely first and then B, the CPU remains idle while each process is waiting.
With multiprogramming, when A waits, the CPU starts B. When B waits, the CPU can return to
A.
So, the CPU stays busy for more time and CPU utilization increases.
Scheduling Objectives
1. Maximum CPU Utilization
The OS should keep the CPU busy as much as possible and reduce idle time.
Example: When one process is waiting for I/O, the CPU runs another process.
The OS should use other resources such as disk, printer, and memory efficiently.
Example: While one process is using the printer, another process can use the disk.
3. Maximum Throughput
Throughput means the number of jobs completed in a given time. The OS tries to complete as
many jobs as possible.
Waiting time is the total time a process waits in the ready queue for the CPU or another
resource.
Example: If a process waits 5 seconds before getting the CPU, its waiting time is 5 seconds.
Turnaround time is the total time taken to complete a process, from starting the job until it
finishes.
Formula:
Turnaround Time = Waiting Time + Computation Time + I/O Time
Example: If a job takes 2 seconds waiting, 5 seconds computing, and 3 seconds for I/O,
turnaround time = 10 seconds.
Response time is the time between entering a command and getting the first output. It is
especially important in Time Sharing Systems.
Example: You type a command and the system starts showing the result after 1 second. The
response time is 1 second.
7. Fairness
Fairness means that all similar processes should get a fair chance to use the CPU and other
resources.
Example: One process should not always get the CPU while another process keeps waiting.
A process should not wait forever for a resource. This problem can be avoided by using aging.
Example: If a process waits for a long time, its priority is gradually increased until it gets the
CPU.
9. Uniformity
Uniformity means the system should behave in a consistent and predictable way.
It means that when the system has too much load, its performance should decrease gradually
instead of suddenly stopping.
Example: If many users use a server, it may become slower, but it should continue working.
11. Predictability
Predictability means a job should take approximately the same amount of time even when the
system load changes.
Example: A program that normally takes 5 seconds should not suddenly take 1 minute under
normal changes in system load.
Non-Preemptive Scheduling is a scheduling method in which a process keeps the CPU until it
finishes or voluntarily gives up the CPU. The Operating System cannot forcibly remove the
CPU from the running process.
Example:
Process A is using the CPU. It continues running until it finishes or requests I/O. The OS does
not forcefully stop it.
2. Preemptive Scheduling
Preemptive Scheduling is a scheduling method in which the Operating System can
forcibly take the CPU away from a running process and give it to another
process, usually when a higher-priority process needs the CPU.
Scheduling can occur when:
Example:
Process A is running, but a higher-priority Process B becomes ready. The OS can stop A and
give the CPU to B.
Interval Timer
Definition:
An Interval Timer is a timer used by the Operating System to control how long a process can
use the CPU. When a process gets the CPU, the OS sets a timer for a specific time. When the
time ends, the process can be preempted and the CPU can be given to another process.
Example:
If a process gets the CPU for 5 milliseconds, the timer is set for 5 ms. After 5 ms, the OS can
stop that process and give the CPU to another process.
Dispatcher
Definition:
A Dispatcher is a part of the Operating System that gives the CPU to the process selected by
the CPU Scheduler. It performs the actual switching from one process to another.
Functions of Dispatcher:
1. Context Switching: Saves the state of the current process and loads the state of the next
process.
2. Switching to User Mode: Changes the CPU from kernel mode to user mode.
3. Jumping to Proper Location: Starts the selected process from the correct point in its
program.
Dispatcher Latency:
The time taken by the dispatcher to stop one process and start another process is called
dispatcher latency. The dispatcher should be very fast because it is used every time the CPU is
given to another process.
Example:
If the scheduler selects Process B, the dispatcher stops Process A, loads Process B's information,
and gives the CPU to Process B.
Scheduling Algorithms
Process:
The OS puts processes in a queue according to their arrival. The first process gets the CPU and
runs until it finishes. After that, the next process gets the CPU.
Example:
If processes arrive as P1, P2, P3, the CPU runs:
P1 → P2 → P3
Easy line:
FCFS = Jo pehle aaye, woh pehle chale.
Process:
The OS checks the CPU time required by the processes. It selects the process that needs the least
time and runs it first. After it finishes, the next shortest process is selected.
Example:
If P1 needs 8 seconds, P2 needs 3 seconds, and P3 needs 5 seconds:
P2 → P3 → P1
Easy line:
SJF = Shortest job pehle chalega.
3. Priority Scheduling
Priority Scheduling selects the process with the highest priority. Each process has a priority
number. It can be preemptive or non-preemptive.
Process:
The OS checks the priority of all ready processes. The process with the highest priority gets the
CPU first. When it finishes, the next highest-priority process gets the CPU.
Example:
If P1 has priority 3, P2 has priority 1, and P3 has priority 2, and 1 is the highest priority:
P2 → P3 → P1
Easy line:
Priority = Highest priority wala process pehle chalega.
Process:
The OS gives the CPU to the first process for a fixed time. When its time is finished, the process
is moved to the end of the queue, and the next process gets the CPU. This continues in a circular
way.
Example:
If the time quantum is 4 seconds and there are P1, P2, and P3:
P1 → P2 → P3 → P1 → P2 → P3
Easy line:
Round Robin = Har process ko bari bari thora time milta hai.
Deadlock
Deadlock
A Deadlock is a situation in which two or more processes are waiting for resources held by
each other, so none of them can continue their execution. The processes remain blocked because
each process is waiting for another process to release a resource.
Example:
Suppose there are two processes (P1 and P2) and two tape drives.
1. Traffic Deadlock:
Several cars are blocking each other at a crossing. No car can move unless one car moves back
or is removed.
2. Train Deadlock:
Two trains reach a narrow crossing from opposite sides. Both stop and wait for the other train to
move, so neither can continue.
Resources
A resource is anything that is needed or used by a process to perform its work. A resource can
be a hardware device, such as a printer, disk, or tape drive. It can also be information, such as a
file, shared variable, or record.
Example:
A process needs a printer to print a document, so the printer is a resource.
Types of Resources
1. Preemptible:
A preemptible resource is a resource that can be taken from one process and
given to another process without causing a problem. Later, it can be given back
to the first process.
Example:
CPU is a preemptible resource. The OS can take the CPU from one process and give it to
another process.
2. Non-Preemptible Resources
Definition:
A non-preemptible resource is a resource that cannot be taken away from a process while it
is being used, because doing so may cause a problem or incorrect result.
Example:
A printer is a non-preemptible resource. If one process is printing a document, the printer
cannot normally be taken and given to another process in the middle of printing.
Easy line:
Preemptible = Can be taken and given to another process.
Non-Preemptible = Cannot be taken until the process finishes.
Deadlock Characterization
A deadlock occurs when all four conditions exist at the same time.
1. Mutual Exclusion
Mutual Exclusion means that a resource can be used by only one process at a time. Other
processes must wait until the resource becomes free.
Example:
Only one process can use a printer at a time.
Hold and Wait means a process is holding one resource while waiting for another resource
that is being used by another process.
Example:
P1 is holding the printer and waiting for the disk, while the disk is held by P2.
3. No Preemption
No Preemption means a resource cannot be forcefully taken from a process. The process must
release the resource itself after finishing its work.
Example:
A printer cannot be forcefully taken from a process while it is printing.
4. Circular Wait
Circular Wait means processes are waiting for each other in a circle. Each process is waiting
for a resource held by the next process.
Example:
P1 waits for P2, P2 waits for P3, and P3 waits for P1. Therefore, none of them can continue.
1. Deadlock Prevention
The system uses rules to make sure that deadlock never occurs. At least one of the four
necessary deadlock conditions is prevented.
The system allows deadlock to occur, detects it, and then takes steps to recover from it.
The Operating System can ignore the deadlock problem if it is very rare and handling it is too
costly.
Deadlock Prevention
Deadlock can be prevented by making sure that at least one of the four necessary conditions
does not occur.
1. Mutual Exclusion
Mutual Exclusion means that a resource can be used by only one process at a time. Some
resources cannot be shared, such as a printer. However, some resources can be shared, such as a
read-only file.
Example:
Many processes can read the same read-only file at the same time, but normally only one process
can use a printer at a time.
Important:
Deadlock cannot always be prevented by removing mutual exclusion because some resources are
naturally non-shareable.
2. Hold and Wait
Hold and Wait means a process is holding one resource while waiting for another resource.
We can prevent this condition by making a process request resources in a controlled way.
Two methods:
1. A process requests all required resources at the beginning. The OS gives them only
when all are available.
2. A process can request a new resource only when it is holding no other resource. It must
release its current resources before requesting new ones.
Example:
If a process needs a printer and disk, it must request both together instead of holding the printer
while waiting for the disk.
Problem:
It may reduce resource utilization because a process may hold resources that it does not need yet.
It can also cause starvation.
3. No Preemption
No Preemption means a resource cannot be taken forcefully from a process. To prevent
deadlock, the system can take resources away from a process when necessary and give them to
another process.
Example:
If P1 is holding a resource and waiting for another resource, the OS may take its held resource
and give it to another process, if the resource can safely be taken.
Problem:
This method works mainly with preemptible resources such as CPU or memory. It does not
work well with resources like a printer because taking a printer during printing can disturb the
output. It can also cause starvation.
4. Circular Wait
Circular Wait means processes are waiting for each other in a circle. To prevent it, the OS
gives every resource type a number and requires processes to request resources in a fixed order.
Example:
Suppose:
Tape Drive = 1
Disk Drive = 5
Printer = 12
A process that needs the disk and printer must request Disk (5) first and then Printer (12). It
cannot request them in reverse order.
Problem:
The fixed order may sometimes be inconvenient. A process may also have to request a resource
before it actually needs it, which can reduce resource utilization.
Operating System Security means protecting the computer system, programs, and data from
unauthorized access, damage, or changes. Security is very important because computers and
networks are used for important services such as electric power, transportation,
telecommunications, and business.
The Internet has increased the need for computer security because many systems are connected
to each other. The OS provides different security mechanisms to control who can access
programs and data. The main purpose is to protect information from unauthorized users,
malicious attacks, accidental changes, and destruction.
Example:
A user should only be allowed to open or modify the files for which they have permission. An
unauthorized person should not be able to access those files.
Security Problem
Definition
A security problem occurs when a person or program accesses, changes, or destroys computer
resources without proper permission. An OS provides different mechanisms to protect
programs and data, but 100% security can never be guaranteed.
Security Levels
Physical Level: Computer systems and their locations should be protected from
unauthorized people.
Human Level: Users should be given proper permissions because a careless or dishonest
user can give access to an intruder.
Operating System Level: The OS uses passwords, permissions, access control, and other
mechanisms to protect programs and data.
Example:
If an unauthorized person enters a computer room and uses a system, physical security has failed.
If a user gives their password to an attacker, human security has failed.
1. Authentication
Authentication is the process of checking and confirming the identity of a user or process in a
computer system. It is an important part of operating system security. It helps the system know
who is using the computer and whether the user is authorized or not. A user can be identified by
something they have, such as a card or key, something they know, such as a password or PIN, or
something they are, such as a fingerprint, retina, or signature.
Passwords are the most common method used to verify a user's identity. A user normally enters
a user ID or account name and then a password. The system compares the entered password with
the stored password, and if they match, the user is allowed to access the system. However,
passwords have some vulnerabilities because they can be guessed, stolen, accidentally exposed,
or shared with unauthorized people. Attackers can guess passwords using personal information
or by using brute-force attacks. Password security can be improved by using long passwords
with letters, numbers, special characters, and different cases.
3. Encrypted Passwords
Encrypted passwords are passwords that are converted into an encoded form before being
stored in the system. When a user enters a password, the system encodes it again and compares it
with the stored encoded password. This provides better security because the original password is
not directly stored. However, if an attacker gets the password file, they can try many possible
passwords and compare their encoded results with the stored values. Common or dictionary-
based passwords can therefore still be cracked.
4. One-Time Passwords
One-Time Password (OTP) is a password that can be used only once for authentication. The
system provides a temporary password or code to the user, and the user enters it to verify their
identity. After it has been used, the password becomes invalid and cannot normally be used
again. This provides better security because even if an attacker gets the password, it cannot be
reused. OTPs are commonly used for secure login and verification.
5. Prevention
Prevention is the process of protecting a computer system from unauthorized access and
security attacks before they happen. Strong passwords should be used and checked for proper
length and character variety. Passwords should be changed regularly, and important data should
be encrypted when it is stored or transmitted. Unnecessary system entry points should be
reduced, and firewalls should be used to block unauthorized network access. The main purpose
of prevention is to stop intruders from entering the system.
6. Detection
Detection is the process of finding out whether a security attack or unauthorized access has
occurred in a computer system. System activities can be recorded using audit logs, including
login times and user information. Unusual activities can be monitored to identify possible
attacks. Antivirus programs can also detect known malicious software. The current state of the
system can be compared with a previous safe state to find suspicious changes. The main purpose
of detection is to discover attacks as quickly as possible.
7. Correction
Correction is the process of repairing a computer system after it has been attacked or
compromised. Regular backups can be used to restore the system to an earlier safe state. If a
backup is not available or is not trusted, the entire system may need to be reinstalled. Security
information such as passwords may also need to be changed. The cause of the attack should be
identified and fixed by disabling unsafe services, installing bug fixes, or changing the system
configuration.
8. Identification
Identification is the process of finding the source of a security attack. It is one of the most
difficult tasks in computer security. Audit records can provide useful information about the
attacker, although an attacker may try to change or delete these records. A network can record
the address of the connecting computer to help trace the attack. If an attacker uses several
computers, the attack must be traced through those computers to find its original source.
Authentication information can also help identify unauthorized users.
9. Threats
Threats are possible dangers that can damage a computer system, steal information, or break
system security. In operating systems, threats are commonly divided into program threats and
system threats. Program threats include Trojan Horses and Trap Doors, while system threats
include Worms and Viruses. These threats can affect system performance, damage files, steal
information, or provide unauthorized access to the system.
A Trojan Horse is a malicious program that looks like a harmless or useful program. It tricks
the user into running it by appearing as something normal, such as a game or screen saver. After
it is executed, it can perform harmful activities such as stealing information or damaging the
system. The Love Bug is a famous example of a Trojan-based attack. A Trojan Horse is
dangerous because the user may not know that the program is malicious.
A Trap Door is a hidden way of accessing a program or computer system while bypassing the
normal security procedures. A programmer may create a trap door to get special access to a
program or system. It can be created legally for testing and maintenance, but it can also be
misused for unauthorized access. If an attacker discovers a trap door, they can use it to bypass
normal security controls.
12. Worms
A Worm is a malicious process or program that can create copies of itself and spread through a
computer system or network. A worm can use system resources and reduce the performance of
the computer. It can create many copies of itself and spread from one computer to another. A
large number of worm copies can consume memory, processing power, and network resources,
causing the system to become slow or unavailable.
13. Viruses
A Virus is a malicious program that attaches itself to other programs or files and spreads when
the infected program or file is used. A virus can modify, damage, or destroy files and can cause
problems in the operating system. It can also spread to other files and programs. Viruses are
dangerous because they can damage data, reduce system performance, and disturb normal
computer operations.