Professional Documents
Culture Documents
Os Notes
Os Notes
Operating System:
An operating system (OS) is a software that acts as an intermediary between computer
hardware and the user. It manages computer hardware resources and provides services for
computer programs. The primary purpose of an operating system is to provide a platform on
which other software applications can run efficiently. It also facilitates communication between
hardware devices and software applications.
Operating systems perform various functions, including:
1. Process Management: This involves managing multiple processes running concurrently
on the system, allocating resources to them, and scheduling their execution.
2. Memory Management: The OS manages the system's memory, allocating memory
space to processes, and ensuring efficient utilization of available memory resources.
3. File System Management: It provides mechanisms for storing, retrieving, and
organizing files on storage devices such as hard disks, SSDs, and optical drives.
4. Device Management: The OS controls and coordinates the operation of peripheral
devices such as printers, scanners, keyboards, and network adapters.
5. User Interface: Operating systems provide user interfaces for interacting with the
computer. This can range from command-line interfaces (CLI) to graphical user
interfaces (GUI) or touch-based interfaces in mobile devices.
6. Security and Access Control: Operating systems enforce security policies, control
access to system resources, and protect the system from unauthorized access and
malicious software.
7. Networking: OS facilitates networking capabilities, enabling computers to
communicate with each other over networks such as LANs, WANs, and the internet.
8. Error Handling: It handles errors and exceptions that occur during system operation,
ensuring system stability and reliability.
Classification of Operating Systems:
1. Batch Operating System: Batch processing involves executing a series of jobs
(programs) without user interaction. Jobs are submitted to the system as a batch, and
the OS executes them one after another without user intervention.
2. Interactive Operating System: Interactive operating systems allow user interaction
through a user interface, enabling users to interact with the system in real-time,
providing immediate responses to user inputs.
3. Time-sharing Operating System: Time-sharing OS allows multiple users to share the
same system simultaneously. It divides the CPU time into small time slots, allowing
each user to have a fair share of CPU time for their tasks.
4. Real-Time Operating System (RTOS): RTOS is designed to handle tasks with strict
timing requirements. It guarantees timely response to events and ensures that critical
tasks are completed within specified deadlines.
5. Multiprocessor Systems: Multiprocessor operating systems manage computer
systems with multiple CPUs (Central Processing Units), distributing tasks among
processors to improve performance and scalability.
6. Multiuser Systems: Multiuser operating systems support multiple users accessing the
system concurrently, providing each user with a separate login session and resources.
7. Multithreaded Systems: Multithreading allows multiple threads within the same
process to execute concurrently. Multithreaded operating systems support the creation
and management of threads, improving system responsiveness and efficiency.
Operating System Structure:
The structure of an operating system consists of various components and services:
1. Kernel: The core component of the operating system responsible for essential functions
such as process management, memory management, and hardware abstraction.
2. System Components: These include device drivers, file systems, networking protocols,
and user interface components that work together to provide the operating system's
functionality.
3. Operating System Services: Services provided by the operating system to facilitate
application development and execution, including process management, memory
management, file system access, and input/output operations.
Definition: Operating System (OS) services are functionalities provided by the OS to enable
applications to interact with the underlying hardware and manage system resources
effectively.
Purpose: OS services abstract hardware complexities and provide a standardized interface
for applications to access resources such as CPU, memory, storage, and I/O devices. They
facilitate tasks such as process management, memory allocation, file system access,
device communication, and user interaction.
Examples of OS Services:
• Process Management: Services for creating, scheduling, and terminating
processes and threads.
• Memory Management: Services for allocating, deallocating, and protecting
memory space, including virtual memory management.
• File System Services: Services for organizing, storing, and accessing files and
directories on storage devices.
• Device Management: Services for managing interactions with hardware devices,
including device drivers and I/O operations.
• Networking Services: Services for network communication, including protocols,
socket management, and data transmission.
• Security Services: Services for enforcing access control, authentication,
encryption, and data protection.
• User Interface Services: Services for providing user interfaces, such as command-
line interpreters, window managers, and graphical toolkits.
4. Monolithic and Microkernel Systems: Operating systems can be structured as
monolithic kernels, where all operating system services run in kernel space, or
microkernel systems, where only essential services run in kernel space, and other
services run as user-space processes.
What is a kernel ?
The kernel is a computer program at the core of a computer’s operating system and has
complete control over everything in the system. It manages the operations of the computer and
the hardware.
There are five types of kernels :
1. A micro kernel, which only contains basic functionality;
2. A monolithic kernel, which contains many device drivers.
3. Hybrid Kernel
4. Exokernel
5. Nanokernel
But in this tutorial we will only look into Microkernel and Monolithic Kernel.
1. Microkernel :
kernel manages the operations of the computer, In microkernel the user services and kernel
services are implemented in different address space. The user services are kept in user address
space, and kernel services are kept under kernel address space.
2. Monolithic kernel :
In Monolithic kernel, the entire operating system runs as a single program in kernel mode. The
user services and kernel services are implemented in same address space.
Differences between Microkernel and Monolithic Kernel :
S.
Parameters Microkernel Monolithic kernel
No.
Process scheduler
Categories of Scheduling
Scheduling falls into one of two categories:
• Non-preemptive: In this case, a process’s resource cannot be taken before the process
has finished running. When a running process finishes and transitions to a waiting state,
resources are switched.
• Preemptive: In this case, the OS assigns resources to a process for a predetermined
period. The process switches from running state to ready state or from waiting for state
to ready state during resource allocation. This switching happens because the CPU may
give other processes priority and substitute the currently active process for the higher
priority process.
Types of Process Schedulers
There are three types of process schedulers:
1. Long Term or Job Scheduler
It brings the new process to the ‘Ready State’. It controls the Degree of Multi-programming, i.e.,
the number of processes present in a ready state at any point in time. It is important that the
long-term scheduler make a careful selection of both I/O and CPU-bound processes. I/O-bound
tasks are which use much of their time in input and output operations while CPU-bound
processes are which spend their time on the CPU. The job scheduler increases efficiency by
maintaining a balance between the two. They operate at a high level and are typically used in
batch-processing systems.
2. Short-Term or CPU Scheduler
It is responsible for selecting one process from the ready state for scheduling it on the running
state. Note: Short-term scheduler only selects the process to schedule it doesn’t load the
process on running. Here is when all the scheduling algorithms are used. The CPU scheduler is
responsible for ensuring no starvation due to high burst time processes.
It controls the degree of It gives less control over how It reduces the degree of
multiprogramming much multiprogramming is multiprogramming.
done.
It is barely present or It is a minimal time-sharing It is a component of systems for
nonexistent in the time-sharing system. time sharing.
system.
It can re-enter the process into It selects those processes It can re-introduce the process
memory, allowing for the which are ready to execute into memory and execution can
continuation of execution. be continued.
Two-State Process Model Short-Term
The terms “running” and “non-running” states are used to describe the two-state process
model.
1. Running: A newly created process joins the system in a running state when it is created.
2. Not running: Processes that are not currently running are kept in a queue and await
execution. A pointer to a specific process is contained in each entry in the queue. Linked
lists are used to implement the queue system. This is how the dispatcher is used. When
a process is stopped, it is moved to the back of the waiting queue. The process is
discarded depending on whether it succeeded or failed. The dispatcher then chooses a
process to run from the queue in either scenario.
Context Switching
In order for a process execution to be continued from the same point at a later time, context
switching is a mechanism to store and restore the state or context of a CPU in the Process
Control block. A context switcher makes it possible for multiple processes to share a single CPU
using this method. A multitasking operating system must include context switching among its
features.The state of the currently running process is saved into the process control block when
the scheduler switches the CPU from executing one process to another. The state used to set
the computer, registers, etc. for the process that will run next is then loaded from its own PCB.
After that, the second can start processing.
Context Switching
In order for a process execution to be continued from the same point at a later time, context
switching is a mechanism to store and restore the state or context of a CPU in the Process
Control block. A context switcher makes it possible for multiple processes to share a single CPU
using this method. A multitasking operating system must include context switching among its
features.
• Program Counter
• Scheduling information
• The base and limit register value
• Currently used register
• Changed State
• I/O State information
• Accounting information
Process Synchronization:
Process Synchronization was introduced to handle problems that arose while multiple process
executions. It is the task phenomenon of coordinating the execution of processes in such a way
that no two processes can have access to the same shared data and resources.
Process is categorized into two types on the basis of synchronization, and these are given
below:
Independent Process
Two processes are said to be independent if the execution of one process does not affect the
execution of another process.
Cooperative Process
Two processes are said to be cooperative if the execution of one process affects the execution of another
process. These processes need to be synchronized so that the order of execution can be guaranteed.
Race Condition
At the time when more than one process is either executing the same code or accessing the
same memory or any shared variable; In that condition, there is a possibility that the output or
the value of the shared variable is wrong so for that purpose all the processes are doing the race
to say that my output is correct. This condition is commonly known as a race condition.
Critical Section:
A Critical Section is a code segment that accesses shared variables and has to be executed as
an atomic action. It means that in a group of cooperating processes, at a given point of time,
only one process must be executing its critical section. If any other process also wants to
execute its critical section, it must wait until the first one finishes. The entry to the critical
section is mainly handled by wait() function while the exit from the critical section is controlled
by the signal() function.
Mainly race condition is a situation that may occur inside the critical section. Race condition
in the critical section happens when the result of multiple thread execution differs according to
the order in which the threads execute. But this condition is critical sections can be avoided if
the critical section is treated as an atomic instruction.
A solution to the critical section problem must satisfy the following three conditions:
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a given
point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical
section then any one of these threads must be allowed to get into its critical section.
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for how many
other processes can get into their critical section, before this process's request is granted. So
after the limit is reached, the system must grant the process permission to get into its critical
section.
These problems are used for testing nearly every newly proposed synchronization scheme. The
following problems of synchronization are considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
This is also called the producer-consumer problem. This problem is generalized in terms of the
Producer-Consumer problem. The solution to this problem is, to create two counting
semaphores “full” and “empty” to keep track of the current number of full and empty buffers
respectively. Producers produce a product and consumers consume the product, but both use
of one of the containers each time.
Problem : To make sure that the producer won’t try to add data into the buffer if it’s full and that
the consumer won’t try to remove data from an empty buffer.
Solution : The producer is to either go to sleep or discard data if the buffer is full. The next time
the consumer removes an item from the buffer, it notifies the producer, who starts to fill the
buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty.
The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
Schedulers
• Short-term scheduler (or CPU scheduler) – selects which process should be
executed next and allocates CPU
o Sometimes the only scheduler in a system
o Short-term scheduler is invoked frequently (milliseconds) (must be fast)
• Long-term scheduler (or job scheduler) – selects which processes should be
brought into the ready queue
o Long-term scheduler is invoked infrequently (seconds, minutes) (may be
slow)
o The long-term scheduler controls the degree of multiprogramming
• Processes can be described as either:
o I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts
o CPU-bound process – spends more time doing computations; few very long
CPU bursts
• Long-term scheduler strives for good process mix
3. Process Synchronization: Ensuring Data Consistency
Process synchronization is a vital mechanism that controls access to shared resources (e.g.,
data structures, memory locations) by multiple processes. This ensures data consistency and
prevents issues like race conditions.
• Race Condition: A scenario where the outcome of a program depends on the
unpredictable timing of process execution. For instance, imagine two processes
updating the same counter variable simultaneously. Without synchronization, one
process's update might be overwritten by the other, leading to an incorrect final value.
Process synchronization aims to achieve:
• Mutual Exclusion: Guarantees that only one process can be in its critical section (code
that modifies shared resources) at a time. This prevents race conditions.
• Orderly Access: Ensures controlled and predictable access to shared resources by
processes.
• Data Consistency: Maintains the integrity of shared data by preventing processes from
interfering with each other's modifications.
Related Topics:
• Critical Section: A specific section of a process's code that accesses shared resources.
Only one process can be in its critical section at any given time.
• Mutual Exclusion Mechanisms: Techniques like semaphores, mutexes, and monitors
are used to implement mutual exclusion. These mechanisms allow processes to
acquire and release locks on shared resources.
• Classical Synchronization Problems: Well-defined problems like the Producer-
Consumer Problem and Reader-Writer Problem showcase the complexities of
synchronization and solutions. These problems illustrate the challenges of coordinating
access to shared resources between multiple processes.
4. Process Scheduling: Optimizing CPU Utilization
Process scheduling is the act of allocating CPU time to processes within an OS. It's a critical
function that determines the order in which processes are executed, impacting system
performance metrics like throughput (amount of work done) and response time (time taken to
respond to user requests).
Process Transitions:
Processes move between states based on events (e.g., I/O completion, resource availability)
and scheduling decisions. For example, a process transitions from running to waiting if it
encounters an I/O wait; it transitions from ready to running when the scheduler assigns it CPU
time.
5. Scheduling Algorithms: Prioritizing Process Execution
• First-Come, First-Served (FCFS) (Continued):
o While simple to implement and fair for short processes, FCFS can lead to
starvation for shorter processes if longer ones arrive first. Processes with long
execution times can monopolize the CPU, causing longer waiting times for
subsequent processes.
• Shortest Job First (SJF):
o The process with the shortest estimated execution time is given priority. The
scheduler attempts to pick the process that will finish the fastest, minimizing the
overall average waiting time.
o While this can improve average waiting time, SJF relies on accurate estimations
of process execution times, which might not always be available. It can also lead
to starvation for longer processes if a stream of short processes keeps entering
the ready queue. Scheduling overhead can be high due to the need to constantly
evaluate process execution times.
• Priority Scheduling:
o Processes are assigned priorities. The process with the highest priority gets CPU
time first. Priorities can be static (predefined) or dynamic (adjusted based on
process behavior).
o This approach allows prioritizing critical processes that require faster execution.
However, it can lead to starvation for lower-priority processes if higher-priority
processes are continuously submitted. Improper priority assignment can lead to
unfairness.
• Round-Robin (RR):
o Processes are allocated CPU time in short slices (time quanta). Once a process
uses its allocated time quantum, it is preempted (paused) and placed at the
back of the ready queue. The scheduler then moves on to the next process in the
queue. If a process finishes its execution before its time quantum expires, it
relinquishes the CPU voluntarily.
o RR ensures fairness by giving all processes a chance to run and provides a quick
turnaround time for processes, making it responsive to user interaction.
However, frequent context switching between processes can add overhead.
Processes with long execution times might not be able to finish within a single
time quantum, leading to longer overall execution times.
Choosing the Right Algorithm:
The optimal scheduling algorithm depends on the specific needs of the system and the types of
processes it runs. Here are some factors to consider:
• Response time: If low response time is critical (e.g., interactive systems), RR or priority
scheduling with high priority for interactive processes might be suitable.
• Throughput: If maximizing the number of completed processes per unit time is the goal
(e.g., batch processing systems), SJF or priority scheduling with high priority for short
processes might be preferable.
• Fairness: If ensuring fair allocation of CPU time to all processes is important, FCFS or
RR might be better choices.
Additional Considerations:
• Multilevel Queue Scheduling: Combines multiple scheduling algorithms at different
priority levels. For example, a system might use RR for short processes in one queue and
priority scheduling for longer processes in another.
• Real-Time Scheduling: Used in systems with strict timing constraints (e.g., embedded
systems). These algorithms prioritize processes based on deadlines to ensure timely
completion of critical tasks.
6. Interprocess Communication (IPC): Enabling Process Collaboration
IPC mechanisms allow processes to communicate and exchange data with each other. This is
essential for tasks like:
• Sharing data between processes (e.g., passing data between a web server and a
database process)
• Coordinating activities between processes (e.g., a parent process spawning a child
process and waiting for it to finish)
• Building modular systems (e.g., processes acting as independent components that
communicate to achieve a larger goal)
Common IPC mechanisms include:
• Shared Memory: Processes can access and modify a shared region of memory.
• Pipes: Processes can write data to one end of a pipe, and another process can read
data from the other end.
• Message Queues: Processes can send messages to a queue, and other processes can
receive messages from the queue.
• Semaphores: Used for synchronization and to signal events between processes.
7. Threads and Their Management:
Threads are lightweight units of execution within a process. A single process can have multiple
threads, allowing it to perform multiple tasks concurrently. Threads share the same memory
space and resources of the process they belong to. This makes them more efficient for certain
tasks compared to creating separate processes.
Thread Management:
• OS manages thread creation, scheduling, and synchronization.
• Threads within a process can cooperate and share data easily due to their shared
memory space.
• Synchronization mechanisms like mutexes are still necessary to prevent race conditions
when multiple threads within a process access shared data.
8. Security Issues in Process Management :
• Unauthorized Process Creation: Malicious programs might attempt to create
unauthorized processes to gain access to system resources or perform harmful actions.
• Privilege Escalation: A process might exploit vulnerabilities to elevate its privileges and
gain unauthorized access to sensitive system resources.
• Resource Starvation: A malicious process might consume excessive resources (CPU,
memory) and starve legitimate processes, degrading system performance.
• Information Leaks: Processes might inadvertently leak sensitive information through
shared memory or IPC mechanisms if not properly secured.
Security Measures in Process Management:
• Access Control Mechanisms: The OS can enforce access control mechanisms to
restrict which users or processes can create new processes and what resources they
can access.
• User Isolation: Techniques like user accounts and virtual memory can isolate
processes from each other, preventing them from accessing each other's resources or
modifying system files.
• Least Privilege Principle: Processes should be granted only the minimum privileges
necessary for them to function correctly. This minimizes the potential damage if a
process is compromised.
• Security Audits and Monitoring: Regularly monitor system activity and perform
security audits to identify suspicious process behavior that might indicate security
threats.
Module III CPU Scheduling
CPU Scheduling Criteria
CPU scheduling is essential for the system’s performance and ensures that processes are
executed correctly and on time. Different CPU scheduling algorithms have other properties and
the choice of a particular algorithm depends on various factors. Many criteria have been
suggested for comparing CPU scheduling algorithms.
What is CPU scheduling?
CPU Scheduling is a process that allows one process to use the CPU while another process is
delayed due to unavailability of any resources such as I / O etc, thus making full use of the CPU.
In short, CPU scheduling decides the order and priority of the processes to run and allocates
the CPU time based on various parameters such as CPU usage, throughput, turnaround, waiting
time, and response time. The purpose of CPU Scheduling is to make the system more efficient,
faster, and fairer.
Criteria of CPU Scheduling
CPU Scheduling has several criteria. Some of them are mentioned below.
1. CPU utilization
The main objective of any CPU scheduling algorithm is to keep the CPU as busy as possible.
Theoretically, CPU utilization can range from 0 to 100 but in a real-time system, it varies from 40
to 90 percent depending on the load upon the system.
2. Throughput
A measure of the work done by the CPU is the number of processes being executed and
completed per unit of time. This is called throughput. The throughput may vary depending on
the length or duration of the processes.
4. Waiting Time
A scheduling algorithm does not affect the time required to complete the process once it starts
execution. It only affects the waiting time of a process i.e. time spent by a process waiting in the
ready queue.
Waiting Time = Turnaround Time – Burst Time.
5. Response Time
In an interactive system, turn-around time is not the best criterion. A process may produce
some output fairly early and continue computing new results while previous results are being
output to the user. Thus another criterion is the time taken from submission of the process of
the request until the first response is produced. This measure is called response time.
Response Time = CPU Allocation Time(when the CPU was allocated for the first) – Arrival
Time
6. Completion Time
The completion time is the time when the process stops executing, which means that the
process has completed its burst time and is completely executed.
7. Priority
If the operating system assigns priorities to processes, the scheduling mechanism should favor
the higher-priority processes.
8.Round Robin scheduling algorithm works well in a time-sharing system where tasks have to
be completed in a short period of time. SJF scheduling algorithm works best in a batch
processing system where shorter jobs have to be completed first in order to increase
throughput.Priority scheduling algorithm works better in a real-time system where certain tasks
have to be prioritized so that they can be completed in a timely manner.
Factors Influencing CPU Scheduling Algorithms
There are many factors that influence the choice of CPU scheduling algorithm. Some of them
are listed below.
• The number of processes.
• The processing time required.
• The urgency of tasks.
• The system requirements.
Selecting the correct algorithm will ensure that the system will use system resources efficiently,
increase productivity, and improve user satisfaction.
CPU Scheduling Algorithms
There are several CPU Scheduling Algorithms, that are listed below.
• First Come First Served (FCFS)
• Shortest Job First (SJF)
• Longest Job First (LJF)
• Priority Scheduling
• Round Robin (RR)
• Shortest Remaining Time First (SRTF)
• Longest Remaining Time First (LRTF)
1.Fundamental Scheduling Concepts:
• Process: An active entity representing a distinct unit of work within the OS. Processes
consist of code, data, and a program counter that tracks execution progress. They
compete for various resources, particularly CPU time, to complete their assigned tasks.
• CPU Burst: The amount of CPU time a process requires to execute continuously without
interruption. This burst time represents the period the process actively uses the CPU for
computations.
• Waiting Time: The duration a process spends in the ready queue, awaiting its turn to be
allocated the CPU. Processes in the ready queue are prepared to run but are stalled due
to unavailability of the CPU.
• Turnaround Time: The total time it takes for a process to complete execution, from the
moment it's submitted to the OS until its completion. It encompasses the time spent
waiting in queues (ready, I/O), performing computations on the CPU, and any I/O
operations.
• Response Time: The time it takes for the OS to begin responding to a process's request
for CPU time after submission. This includes the time for the process to enter the ready
queue and potentially the time spent waiting in the queue before acquiring the CPU.
• Context Switching: The overhead associated with switching the CPU from one running
process to another. This involves saving the state of the previous process (registers,
memory) and restoring the state of the new process to prepare it for execution.
2.Scheduling Techniques:
• Preemptive Scheduling: The OS can dynamically reclaim the CPU from a running
process with lower priority and grant it to another process with higher priority. This
allows for flexible prioritization based on process needs. The preempted process is then
added back to the ready queue and will get another chance to run when its turn comes
again.
• Non-Preemptive Scheduling: Once allocated the CPU, a process retains control until it
finishes its CPU burst or voluntarily relinquishes the CPU through system calls or I/O
wait. This approach offers simplicity but might lead to starvation for lower-priority
processes if a high-priority process has a long CPU burst.
3.Techniques of Scheduling:
Definition: Techniques of scheduling refer to the different strategies and algorithms used to
schedule processes for execution on the CPU.
• Preemptive Scheduling: Scheduling algorithms that allow the operating
system to interrupt a currently executing process to start or resume
another process. Examples include Round Robin and Shortest
Remaining Time First.
• Non-Preemptive Scheduling: Scheduling algorithms where the
currently executing process cannot be interrupted until it completes its
CPU burst. Examples include First-Come-First-Serve and Shortest Job
First.
• Multiprogramming: Technique where multiple processes are loaded into
main memory simultaneously for concurrent execution. It increases CPU
utilization and throughput by keeping the CPU busy with useful work.
• Multitasking: Technique allowing multiple processes to execute
concurrently by rapidly switching between them on the CPU. It gives the
illusion of parallel execution to users and improves system
responsiveness.
Main Memory
What is Memory Management?
In a multiprogramming computer, the Operating System resides in a part of memory,
and the rest is used by multiple processes. The task of subdividing the memory among
different processes is called Memory Management. Memory management is a method
in the operating system to manage operations between main memory and disk during
process execution. The main aim of memory management is to achieve efficient
utilization of memory.
Why Memory Management is Required?
• Allocate and de-allocate memory before and after process execution.
• To keep track of used memory space by processes.
• To minimize fragmentation issues.
• To proper utilization of main memory.
• To maintain data integrity while executing of process.
Now we are discussing the concept of Logical Address Space and Physical Address
Space
Logical and Physical Address Space
• Logical Address Space: An address generated by the CPU is known as a
“Logical Address”. It is also known as a Virtual address. Logical address space
can be defined as the size of the process. A logical address can be changed.
• Physical Address Space: An address seen by the memory unit (i.e the one
loaded into the memory address register of the memory) is commonly known as
a “Physical Address”. A Physical address is also known as a Real address. The set
of all physical addresses corresponding to these logical addresses is known as
Physical address space. A physical address is computed by MMU. The run-time
mapping from virtual to physical addresses is done by a hardware device
Memory Management Unit(MMU). The physical address always remains
constant.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different
types of loading :
• Static Loading: Static Loading is basically loading the entire program into a fixed
address. It requires more memory space.
• Dynamic Loading: The entire program and all data of a process must be in
physical memory for the process to execute. So, the size of a process is limited
to the size of physical memory. To gain proper memory utilization, dynamic
loading is used. In dynamic loading, a routine is not loaded until it is called. All
routines are residing on disk in a relocatable load format. One of the advantages
of dynamic loading is that the unused routine is never loaded. This loading is
useful when a large amount of code is needed to handle it efficiently.
Static and Dynamic Linking
To perform a linking task a linker is used. A linker is a program that takes one or more
object files generated by a compiler and combines them into a single executable file.
• Static Linking: In static linking, the linker combines all necessary program
modules into a single executable program. So there is no runtime dependency.
Some operating systems support only static linking, in which system language
libraries are treated like any other object module.
• Dynamic Linking: The basic concept of dynamic linking is similar to dynamic
loading. In dynamic linking, “Stub” is included for each appropriate library
routine reference. A stub is a small piece of code. When the stub is executed, it
checks whether the needed routine is already in memory or not. If not available
then the program loads the routine into memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a process of
swapping a process temporarily into a secondary memory from the main memory,
which is fast compared to secondary memory. A swapping allows more processes to be
run and can be fit into memory at one time. The main part of swapping is transferred
time and the total time is directly proportional to the amount of memory swapped.
Swapping is also known as roll-out, or roll because if a higher priority process arrives
and wants service, the memory manager can swap out the lower priority process and
then load and execute the higher priority process. After finishing higher priority work, the
lower priority process swapped back in memory and continued to the execution
process.
swapping in memory management
Memory Management with Monoprogramming (Without Swapping)
This is the simplest memory management approach the memory is divided into two
sections:
• One part of the operating system
• The second part of the user program
Fence Register
operating system user program
• In this approach, the operating system keeps track of the first and last location
available for the allocation of the user program
• The operating system is loaded either at the bottom or at top
• Interrupt vectors are often loaded in low memory therefore, it makes sense to
load the operating system in low memory
• Sharing of data and code does not make much sense in a single process
environment
• The Operating system can be protected from user programs with the help of a
fence register.
Advantages of Memory Management
• It is a simple management approach
Disadvantages of Memory Management
• It does not support multiprogramming
• Memory is wasted
Multiprogramming with Fixed Partitions (Without Swapping)
• A memory partition scheme with a fixed number of partitions was introduced to
support multiprogramming. this scheme is based on contiguous allocation
• Each partition is a block of contiguous memory
• Memory is partitioned into a fixed number of partitions.
• Each partition is of fixed size
Example: As shown in fig. memory is partitioned into 5 regions the region is reserved for
updating the system the remaining four partitions are for the user program.
Fixed Size Partitioning
Operating System
p1
p2
p3
p4
Partition Table
Once partitions are defined operating system keeps track of the status of memory
partitions it is done through a data structure called a partition table.
Sample Partition Table
Starting Address of Partition Size of Partition Status
0k 200k allocated
200k 100k free
300k 150k free
450k 250k allocated
Logical vs Physical Address
An address generated by the CPU is commonly referred to as a logical address. the
address seen by the memory unit is known as the physical address. The logical address
can be mapped to a physical address by hardware with the help of a base register this is
known as dynamic relocation of memory references.
Contiguous Memory Allocation
The main memory should accommodate both the operating system and the different
client processes. Therefore, the allocation of memory becomes an important task in
the operating system. The memory is usually divided into two partitions: one for the
resident operating system and one for the user processes. We normally need several
user processes to reside in memory simultaneously. Therefore, we need to consider
how to allocate available memory to the processes that are in the input queue waiting to
be brought into memory. In adjacent memory allotment, each process is contained in a
single contiguous segment of memory.
First Fit
Here, in this diagram, a 40 KB memory block is the first available free hole that can store
process A (size of 25 KB), because the first two blocks did not have sufficient memory
space.
Best Fit
In the Best Fit, allocate the smallest hole that is big enough to process requirements.
For this, we search the entire list, unless the list is ordered by size.
Best Fit
Here in this example, first, we traverse the complete list and find the last hole 25KB is
the best suitable hole for Process A(size 25KB). In this method, memory utilization is
maximum as compared to other memory allocation techniques.
Worst Fit
In the Worst Fit, allocate the largest available hole to process. This method produces
the largest leftover hole.
Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest available memory
block which is 60KB. Inefficient memory utilization is a major issue in the worst fit.
Fragmentation
Fragmentation is defined as when the process is loaded and removed after execution
from memory, it creates a small free hole. These holes can not be assigned to new
processes because holes are not combined or do not fulfill the memory requirement of
the process. To achieve a degree of multiprogramming, we must reduce the waste of
memory or fragmentation problems. In the operating systems two types of
fragmentation:
1. Internal fragmentation: Internal fragmentation occurs when memory blocks are
allocated to the process more than their requested size. Due to this some
unused space is left over and creating an internal fragmentation
problem.Example: Suppose there is a fixed partitioning used for memory
allocation and the different sizes of blocks 3MB, 6MB, and 7MB space in
memory. Now a new process p4 of size 2MB comes and demands a block of
memory. It gets a memory block of 3MB but 1MB block of memory is a waste,
and it can not be allocated to other processes too. This is called internal
fragmentation.
2. External fragmentation: In External Fragmentation, we have a free memory
block, but we can not assign it to a process because blocks are not
contiguous. Example: Suppose (consider the above example) three processes
p1, p2, and p3 come with sizes 2MB, 4MB, and 7MB respectively. Now they get
memory blocks of size 3MB, 6MB, and 7MB allocated respectively. After
allocating the process p1 process and the p2 process left 1MB and 2MB.
Suppose a new process p4 comes and demands a 3MB block of memory, which
is available, but we can not assign it because free memory space is not
contiguous. This is called external fragmentation.
Both the first-fit and best-fit systems for memory allocation are affected by external
fragmentation. To overcome the external fragmentation problem Compaction is used. In
the compaction technique, all free memory space combines and makes one large
block. So, this space can be used by other processes effectively.
Another possible solution to the external fragmentation is to allow the logical address
space of the processes to be noncontiguous, thus permitting a process to be allocated
physical memory wherever the latter is available.
Paging
Paging is a memory management scheme that eliminates the need for a contiguous
allocation of physical memory. This scheme permits the physical address space of a
process to be non-contiguous.
• Logical Address or Virtual Address (represented in bits): An address
generated by the CPU.
• Logical Address Space or Virtual Address Space (represented in words or
bytes): The set of all logical addresses generated by a program.
• Physical Address (represented in bits): An address actually available on a
memory unit.
• Physical Address Space (represented in words or bytes): The set of all
physical addresses corresponding to the logical addresses.
Example:
• If Logical Address = 31 bits, then Logical Address Space = 231 words = 2 G words
(1 G = 230)
• If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address =
log2 227 = 27 bits
• If Physical Address = 22 bits, then Physical Address Space = 222 words = 4 M
words (1 M = 220)
• If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address
= log2 224 = 24 bits
The mapping from virtual to physical address is done by the memory management unit
(MMU) which is a hardware device and this mapping is known as the paging technique.
• The Physical Address Space is conceptually divided into several fixed-size
blocks, called frames.
• The Logical Address Space is also split into fixed-size blocks, called pages.
• Page Size = Frame Size
Let us consider an example:
• Physical Address = 12 bits, then Physical Address Space = 4 K words
• Logical Address = 13 bits, then Logical Address Space = 8 K words
• Page size = frame size = 1 K words (assumption)
Paging
The address generated by the CPU is divided into:
• Page Number(p): Number of bits required to represent the pages in Logical
Address Space or Page number
• Page Offset(d): Number of bits required to represent a particular word in a page
or page size of Logical Address Space or word number of a page or page offset.
Physical Address is divided into:
• Frame Number(f): Number of bits required to represent the frame of Physical
Address Space or Frame number frame
• Frame Offset(d): Number of bits required to represent a particular word in a
frame or frame size of Physical Address Space or word number of a frame or
frame offset.
The hardware implementation of the page table can be done by using dedicated
registers. But the usage of the register for the page table is satisfactory only if the page
table is small. If the page table contains a large number of entries then we can use
TLB(translation Look-aside buffer), a special, small, fast look-up hardware cache.
• The TLB is an associative, high-speed memory.
• Each entry in TLB consists of two parts: a tag and a value.
• When this memory is used, then an item is compared with all tags
simultaneously. If the item is found, then the corresponding value is returned.
File Type
File type refers to the ability of the operating system to distinguish different types of file such
as text files source files and binary files etc.
Ordinary files
• These are the files that contain user information.
• These may have text, databases or executable program.
• The user can apply various operations on such files like add, modify, delete or even
remove the entire file.
Directory files
• These files contain list of file names and other information related to these files.
Special files
• These files are also known as device files.
• These files represent physical device like disks, terminals, printers, networks, tape drive
etc.
These files are of two types −
• Character special files − data is handled character by character as in case of terminals
or printers.
• Block special files − data is handled in blocks as in the case of disks and tapes.
File Access Mechanisms
File access mechanism refers to the manner in which the records of a file may be accessed.
There are several ways to access files −
• Sequential access
• Direct/Random access
• Indexed sequential access
Sequential access
A sequential access is that in which the records are accessed in some sequence, i.e., the
information in the file is processed in order, one record after the other. This access method is
the most primitive one. Example: Compilers usually access files in this fashion.
Direct/Random access
• Random access file organization provides, accessing the records directly.
• Each record has its own address on the file with by the help of which it can be directly
accessed for reading or writing.
• The records need not be in any sequence within the file and they need not be in adjacent
locations on the storage medium.
Indexed sequential access
• This mechanism is built up on base of sequential access.
• An index is created for each file which contains pointers to various blocks.
• Index is searched sequentially and its pointer is used to access the file directly.
Space Allocation
Files are allocated disk spaces by operating system. Operating systems deploy following
three main ways to allocate disk space to files.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
Contiguous Allocation
• Each file occupies a contiguous address space on disk.
• Assigned disk address is in linear order.
• Easy to implement.
• External fragmentation is a major issue with this type of allocation technique.
Linked Allocation
• Each file carries a list of links to disk blocks.
• Directory contains link / pointer to first block of a file.
• No external fragmentation
• Effectively used in sequential access file.
• Inefficient in case of direct access file.
Indexed Allocation
• Provides solutions to problems of contiguous and linked allocation.
• A index block is created having all pointers to files.
• Each file has its own index block which stores the addresses of disk space occupied
by the file.
• Directory contains the addresses of index blocks of files.
Module-V
Directory Structure
A directory is a container that is used to contain folders and files. It organizes files and
folders in a hierarchical manner.
There are several logical structures of a directory, these are given below.
• Single-level directory –
The single-level directory is the simplest directory structure. In it, all files are
contained in the same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of
files increases or when the system has more than one user. Since all the files are in the
same directory, they must have a unique name. if two users call their dataset test, then
the unique name rule violated.
Advantages:
Disadvantages:
• There may chance of name collision because two files can have the same name.
• Searching will become time taking if the directory is large.
• This can not group the same type of files together.
Two-level directory –
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each user.
In the two-level directory structure, each user has their own user files directory
(UFD). The UFDs have similar structures, but each lists only the files of a single user.
system’s master file directory (MFD) is searched whenever a new user id is Correct.
I/O Device
One of the important jobs of an Operating System is to manage various I/O devices including
mouse, keyboards, touch pad, disk drives, display adapters, USB devices, Bit-mapped screen,
LED, Analog-to-digital converter, On/off switch, network connections, audio I/O, printers etc.
An I/O system is required to take an application I/O request and send it to the physical device,
then take whatever response comes back from the device and send it to the application. I/O
devices can be divided into two categories −
• Block devices − A block device is one with which the driver communicates by sending
entire blocks of data. For example, Hard disks, USB cameras, Disk-On-Key etc.
• Character devices − A character device is one with which the driver communicates by
sending and receiving single characters (bytes, octets). For example, serial ports,
parallel ports, sounds cards etc
Device Controllers
Device drivers are software modules that can be plugged into an OS to handle a particular
device. Operating System takes help from device drivers to handle all I/O devices.
The Device Controller works like an interface between a device and a device driver. I/O units
(Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic
component where electronic component is called the device controller.
There is always a device controller and a device driver for each device to communicate with
the Operating Systems. A device controller may be able to handle multiple devices. As an
interface its main task is to convert serial bit stream to block of bytes, perform error correction
as necessary.
Any device connected to the computer is connected by a plug and socket, and the socket is
connected to a device controller. Following is a model for connecting the CPU, memory,
controllers, and I/O devices where CPU and device controllers all use a common bus for
communication.
While using memory mapped IO, OS allocates buffer in memory and informs I/O device to use
that buffer to send data to the CPU. I/O device operates asynchronously with CPU, interrupts
CPU when finished.
The advantage to this method is that every instruction which can access memory can be used
to manipulate an I/O device. Memory mapped IO is used for most high-speed I/O devices like
disks, communication interfaces.
Direct Memory Access (DMA)
Slow devices like keyboards will generate an interrupt to the main CPU after each byte is
transferred. If a fast device such as a disk generated an interrupt for each byte, the operating
system would spend most of its time handling these interrupts. So a typical computer uses
direct memory access (DMA) hardware to reduce this overhead.
Direct Memory Access (DMA) means CPU grants I/O module authority to read from or write
to memory without involvement. DMA module itself controls exchange of data between main
memory and the I/O device. CPU is only involved at the beginning and end of the transfer and
interrupted only after entire block has been transferred.
Direct Memory Access needs a special hardware called DMA controller (DMAC) that manages
the data transfers and arbitrates access to the system bus. The controllers are programmed with
source and destination pointers (where to read/write the data), counters to track the number of
transferred bytes, and settings, which includes I/O and memory types, interrupts and states for
the CPU cycles.
Directory Structure
A directory is a container that is used to contain folders and files. It organizes files and
folders in a hierarchical manner.
There are several logical structures of a directory, these are given below.
• Single-level directory –
The single-level directory is the simplest directory structure. In it, all files are
contained in the same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of
files increases or when the system has more than one user. Since all the files are in the
same directory, they must have a unique name. if two users call their dataset test, then
the unique name rule violated.
Advantages:
Disadvantages:
• There may chance of name collision because two files can have the same name.
• Searching will become time taking if the directory is large.
• This can not group the same type of files together.
Two-level directory –
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each user.
In the two-level directory structure, each user has their own user files directory
(UFD). The UFDs have similar structures, but each lists only the files of a single user.
system’s master file directory (MFD) is searched whenever a new user id is Correct.
3) Tree Structure/ Hierarchical Structure:
Tree directory structure of operating system is most commonly used in our personal
computers. User can create files and subdirectories too, which was a disadvantage in the
previous directory structures.
This directory structure resembles a real tree upside down, where the root directory is at
the peak. This root contains all the directories for each user. The users can create
subdirectories and even store files in their directory.