Os Note
Os Note
Operating System:
An operating system (OS) is a software program that manages the
hardware and software resources of a computer.
A program that acts as an intermediary between a user of a
computer and the computer hardware.
The operating system controls and coordinates the use of the
hardware among the various application program for the various
users.
Examples: Windows, Linux, Unix and Mac OS, etc.,
Operating system goals:
Execute user programs and make solving user problems easier.
Make the computer system convenient to use.
Use the computer hardware in an efficient manner.
Computer System Components:
A computer system can be divided roughly into four components:
the hardware, the operating system, the application
1
program, and the users as shown in the fig below.
OPERATING SYSTEM OVERVIEW
1. Hardware – provides basic computing resources (CPU,
memory, I/O devices).
2. Operating system – controls and coordinates the use
of the hardware among the various application programs
for the various users.
3. Applications programs – Define the ways in which the
system resources are used to solve the computing problems
of the users (compilers, database systems, video games,
business programs).
4. Users (people, machines, other computers).
2
OPERATING SYSTEM OVERVIEW
3
OPERATING SYSTEM OVERVIEW
An operating system is similar to a government. Like a government, it performs
no useful function by itself.
It simply provides an environment within which other programs can do useful
work.
Hence, an operating system is a program that controls the execution of
application programs and acts as an interface between the user of a computer and
the computer hardware, in other words, “The software that controls the
hardware.” Some examples of operating systems are UNIX, Mach, MS-DOS,
MS-Windows, Windows/NT, OS/2,MacOS, and VMS etc
Components of OS: Shell and the kernel are the parts of this Operating system.
These both parts are used for performing any operation on the system.
i)Shell: Shell is the outermost layer of the operating system that manages the
interaction between user and operating system.
When a user gives his command for performing any operation, then the request
will goes to the shell parts,(also known as interpreter) which translate the
human program into the machine language and then the request will be
transferred to the kernel.
The Shell manages the interaction between user and operating system by: 4
OPERATING SYSTEM OVERVIEW
Prompting the user to give input
Interpreting the input for the operating system
Handling the output from the operating system.
Shell provides a way to communicate with the operating system by either
taking the input from the user or the shell script.
A shell script is a sequence of system commands that are stored in a file.
5
o (ii)Kernel: The kernel is the core component of an operating system and it is also
known as heart of operating system which acts as an interface between shell,
and the data is processed at the hardware level.
o Every operation is performed by using the kernel, when the kernel
receives the request from the shell then this will process the request and
display the results on the screen.
When an OS is loaded into memory, the kernel is loaded first and remains in memory
until the OS is shut down. After that, the kernel provides and manages the computer
resources and allows other programs to run and use these resources. The kernel is
responsible for performing the following tasks:
Input-Output management
Memory Management
Process Management for application execution.
Device Management
System calls control
Types of Kernel:
1. Monolithic Kernel: A monolithic kernel is a single large program that contains all
operating system components.
The entire kernel executes in the processor’s privileged mode and provides full
access to the system’s hardware.
6
Monolithic kernels are faster than microkernel because they don’t have the overhead
of message passing. This type of kernel is generally used in embedded systems and
real-time operating systems.
OPERATING SYSTEM OVERVIEW
2. Microkernel: A microkernel is a kernel that contains only the essential
components required for the basic functioning of the operating system.
All other components are removed from the kernel and implemented as
user-space processes.
The microkernel approach provides better modularity, flexibility, and
extensibility. It is also more stable and secure than monolithic kernels.
Two views of the Operating System:
Operating System as an Extended Machine or Virtual Machine (or As a
User/computer interface):
The operating system masks or hides the details of the hardware form the
programmers and general users and provides a convenient interface for
using the system.
The program that hides the truth about the hardware from the user and
presents a nice simple view of named files that can be read and written is of
course the operating system.
A major function of OS is to hide all the complexity presented by the
underlying hardware and gives the programmer a more convenient set of 7
instructions to work with.
OPERATING SYSTEM OVERVIEW
Just as the operating system prevent the user from directly accessing the
hardware and presents a simple file oriented interface.
It also conceals a lot of unpleasant business concerning interrupts, timers,
memory management and other low level features.
In this view, the function of OS is to present the user with the equivalent of
an extended machine or virtual machine that is easier to program than
underlying hardware.
8
OPERATING SYSTEM OVERVIEW
9
OPERATING SYSTEM OVERVIEW
11
OPERATING SYSTEM OVERVIEW
14
OPERATING SYSTEM OVERVIEW
15
OPERATING SYSTEM OVERVIEW
Types of Operating System:
Serial processing
Batch processing
Multiprogramming
Multitasking or time sharing System
Multiprocessor Operating System
Real Time Operating System
Network Operating system
Distributed Operating system
Serial processing:
At early stage the programmer interacted directly with the computer
hardware, because there was no operating system
A single group of people designed, built, programmed, operated and
maintained each machine
The serial processing operating system are those which performs all the
instruction in a sequential manner.
The instruction given by user will be executed by using FIFO manner
means first in first out.
The program counter will determine which instruction is going to execute 16
and which instruction will be executed after that the punch cards are used
for this.
OPERATING SYSTEM OVERVIEW
In this, all the jobs are firstly prepared and store on the card and after that all
the instruction are executed one by one .
But the main problem is that user doesn’t interact with the system while
working on the system i.e. the user can’t be able to enter data for execution.
CPU is often idle, because the speed of the mechanical I/O devices
is slower than the CPU.
Batch Processing:
Batch is defined as a group of jobs with similar needs. The operating system
allows users to form batches.
Computer executes each batch sequentially, processing all jobs of a batch
considering them as a single process called batch processing
This type of operating system does not interact with the computer directly.
The central idea behind the simple batch-processing scheme is the use of a
piece of software known as monitor or operator.
The user submits the job on cards or tape to a computer operator, who
batches the jobs together sequentially and places the entire batch on an input
device, for use by the monitor.
17
OPERATING SYSTEM OVERVIEW
The user submits the job on cards or tape to a computer operator,
who batches the jobs together sequentially and places the entire
batch on an input device, that is used by the monitor.
Each program is constructed to branch back to the monitor when
it completes processing, at which point the monitor automatically
begins loading the next program.
With a batch operating system, processor time alternates between
execution of user programs and execution of the monitor.
Batch operating systems are used for tasks such as managing
payroll systems, data entry and bank statements.
18
OPERATING SYSTEM OVERVIEW
Advantages are:
Many users can share batch systems.
There is little idle time for batch operating systems.
It becomes possible to manage large workloads.
Disadvantages are:
Batch operating systems are challenging to debug.
Any failure of the system creates a backlog.
It may be costly to install and maintain good batch operating
systems.
19
OPERATING SYSTEM OVERVIEW
20
OPERATING SYSTEM OVERVIEW
21
OPERATING SYSTEM OVERVIEW
Advantages:
High CPU utilization.
Shorter response time.
Shorter waiting time.
Increased throughput.
Disadvantages:
Managing all processes and tasks can be a challenging task.
CPU scheduling is required.
Memory management is required in the operating system as all
types of tasks are stored in the main memory.
22
OPERATING SYSTEM OVERVIEW
23
OPERATING SYSTEM OVERVIEW
24
OPERATING SYSTEM OVERVIEW
25
multiprogramming multitasking:
Non preemptive Preemptive
It was early used It used now a days in Laptops and PC
Less Context Switching due to non- Highly Context Switching due to preemptive
preemptive mode mode
Non Responsive Highly Responsive
No time sharing time sharing
Main target: CPU utilization because of less Main target: Responsive because of Highly
Context Switching. Context Switching
26
OPERATING SYSTEM OVERVIEW
Multiprocessing system:
Multiprocessor operating systems are also known as parallel OS or tightly
coupled OS.
Such operating systems have more than one processor in close
communication that sharing the computer bus, the clock and sometimes
memory and peripheral devices. It executes multiple jobs at same time and
makes the processing faster.
Multiprocessor systems have three main advantages:
Increased throughput: By increasing the number of processors, the system
performs more work in less time. The speed-up ratio with N processors is
less than N.
Economy of scale: Multiprocessor systems can save more money than
multiple single-processor systems because they can share peripherals, mass
storage, and power supplies.
Increased reliability: If one processor fails to done its task, then each of
the remaining processors must pick up a share of the work of the failed
processor. The failure of one processor will not halt the system, only slow it 27
down.
OPERATING SYSTEM OVERVIEW
Disadvantages:
If one processor fails then it will affect in the speed
Multiprocessor systems are expensive
Complex OS is required
28
OPERATING SYSTEM OVERVIEW
29
OPERATING SYSTEM OVERVIEW
30
OPERATING SYSTEM OVERVIEW
31
OPERATING SYSTEM OVERVIEW
Advantages:
Highly stable centralized servers
Security concerns are handled through servers
New technologies and hardware up-gradation are easily integrated into the
system
Server access is possible remotely from different locations and types of
systems
Disadvantages:
Servers are costly
User has to depend on a central location for most operations
Maintenance and updates are required regularly
32
DOS NOS
The main purpose of distributed operating The main purpose of a network operating
systems is to manage hardware resources. system is to provide services to remote
clients.
In this, communication is done on the basis In this, communication is done on the basis
of message and shared memory. of files.
Distributed Operating System is less Network Operating System is more scalable
scalable than Network Operating System than Distributed Operating System.
Ease of implementation in Distributed Ease of implementation in Network
Operating System is less. Operating System is also high.
In DOS, All nodes have same operating In NOS, All nodes can have different
system. operating system.
In distributed Operating System, fault In Network Operating System, fault
tolerance is high. tolerance is less.
An operating system that manages a group A special operating system that provide
of distinct computers and makes them network-based functionalities.
appear to be a single computer
Helps to share resources and collaborate via Helps to manage data, users, groups,
a shared network to accomplish tasks security and other network related 33
functionalities.
OPERATING SYSTEM OVERVIEW
34
OPERATING SYSTEM OVERVIEW
This type of system can never miss its deadline. Missing the
deadline may have disastrous consequences.
Example: Flight controller system, missile launching system,
satellite system, Air bag control system, Pacemakers etc
Soft real-time system:
Soft real-time system in which the meeting of deadline is not
compulsory for every time for every task but process should get
processed and the result.
This type of system can miss its deadline occasionally with some
acceptably low probability.
Missing the deadline has no disastrous consequences.
The usefulness of results produced by a soft real-time system
decreases gradually with an increase in processing delay.
Example: personal computer, online transaction system, Weather
Monitoring Systems,Telephone switches , online transaction etc
35
OPERATING SYSTEM OVERVIEW
For example:
When system is Turn ON then CPU is in kernel mode, by default, because
it load OS into main memory and OS exist in Kernel mode. After log in,
CPU switches to user mode to run user applications.
Similarly, To open MS WORD we have to double click on that particular
file. As, that file exist in main memory and main memory is a hardware.
Since, User cannot access the hardware in user mode. User have to
request through system call to open the file in main memory.
So, OPEN () system call is generated and CPU switch to Kernel mode.
Now OS is in kernel mode, it opens that particular file from main
memory.
After opening the file, CPU switches back to user mode. Now we are
writing some text in user mode.
When user want to save the file then WRITE () system call is again
generated. CPU switches to kernel mode to save the file in main memory.
37
OPERATING SYSTEM OVERVIEW
System call:
A system call is a request made by a user-level program to the OS to perform
a privileged operation, such as reading from or writing to a file or allocating
memory.
When System Call is generated then CPU switch form user mode to kernel
mode and performs that particular task and turns back to user mode again.
The system calls are used to perform following operations.
Creation of new processes.
38
OPERATING SYSTEM OVERVIEW
39
OPERATING SYSTEM OVERVIEW
Device management:
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
Information maintenance
• get time or date, set time or date
• get system data, set system data
• get process, file, or device attributes
• set process, file, or device attributes
Communications
• create, delete communication connection
• send, receive messages
• transfer status information
• attach or detach remote devices
40
OPERATING SYSTEM OVERVIEW
41
OPERATING SYSTEM OVERVIEW
42
OPERATING SYSTEM OVERVIEW
Monolithic structure:
The monolithic operating system controls all aspects of the
operating system's operation, including file management,
memory management, device management, and
operational operations.
Kernel delivers basic services to all other elements of the
system.
It serves as the primary interfaces between the operating
system and the hardware.
An operating system is built into a single piece of
hardware, such as a keyboard or mouse and the kernel can
directly access all of its resources.
45
OPERATING SYSTEM OVERVIEW
Advantages:
A process runs completely in single address space in the
monolithic kernel.
The monolithic kernel is a single binary file.
Disadvantages:
If any service fails in the monolithic kernel, it leads to the
failure of the entire system.
The entire operating system needs to be modified by the
user to add any new service.
Layered approach:
Layered structure of OS is the operating system structure in
which whole functionality of OS is divided in to number of layers
with each layer having a well defined specific function.
The bottom layer (layer 0) is the hardware, and the topmost layer
(layer N) is the user interface.
These layers are designed such that each layer uses the functions 47
of the lower-level layers.
OPERATING SYSTEM OVERVIEW
48
OPERATING SYSTEM OVERVIEW
Layer Function
5 User interface
4 I/O management
3 Process management
0 Hardware
50
OPERATING SYSTEM OVERVIEW
Disadvantages:
Increased complexity due to the number of layers
This architecture can result in slower performance due to
communication overhead between layers
Difficult to enforce strict separation between layers, which
can lead to coupling and reduced modularity
Microkernel structure of os:
A microkernel is an operating system structure that aims
to keep the kernel as small and lightweight as possible.
It provides only essential services, such as process
scheduling and inter-process communication, while moving
most non-essential services, like device drivers, into user
space.
Example: Mac OS X by apple.
51
OPERATING SYSTEM OVERVIEW
Microkernel structure:
A microkernel is an operating system structure that aims
to keep the kernel as small and lightweight as possible.
It provides only essential services, such as process
scheduling and inter-process communication, while moving
most non-essential services, like device drivers, into user
space.
Example: Mac OS X by apple.
Advantages:
easier to extend a microkernel
53
OPERATING SYSTEM OVERVIEW
54
OPERATING SYSTEM OVERVIEW
Virtual Machine structure:
First appeared commercially in IBM mainframes in 1972
A virtual machine is an execution of a computer system within
our physical personal computer.
A virtual machine allows users to install and run multiple
operating systems in a single PC there by making an illusion that
each separate execution environment is running its own private
computer.
A virtual machine provides an interface identical to the
underlying bare hardware
Here resources of computer system are shared to each OS running
by virtual machine.
Advantages
The virtual-machine concept provides complete protection of
system resources.
This isolation, however, permits no direct sharing of resources.
Perfect for OS research and development.
System development is done on the virtual machine, instead of on 55
a physical machine and so does not disrupt normal system
operation.
OPERATING SYSTEM OVERVIEW
Disadvantages
The virtual machine concept is difficult to implement due to the
effort required to provide an exact duplicate to the underlying
machine
56
Fig 1 virtual machine structure of os
OPERATING SYSTEM OVERVIEW
Q. What is a trap instruction? Explain its use in OS.
A trap is an interrupt generated by the CPU when a user-level
program attempts to execute a privileged mode instruction or
encounters an error.
When a trap occurs, the CPU transfers control to the kernel mode
and execute a trap handler.
The trap handler checks the type of trap and takes appropriate
action (such as such as terminating the program or performing a
privileged operation on behalf of the program).
To obtain services from the operating system, a user program
must make a system call, which traps in to the kernel and invokes
the operating system.
When the work has been completed, control is returned to the user
program at the instruction following the system call.
If a process is running a user program in user mode and needs
system services, such as reading data from a file, it has to execute
a trap instruction to transfer control to the operating system.
57
OPERATING SYSTEM OVERVIEW
58
Fig. trap instruction execution
OPERATING SYSTEM OVERVIEW
Explanation:
C and C++ compilers push the parameters on to the stack in
reverse order for historical reasons (having to do with making the
first parameter to printf, the format string, appear on top of the
stack).
The first and third parameters are called by value, but second
parameter is passed by reference, meaning that the address of the
buffer is passed, not the contents of the buffer.
Then comes the actual call to the library procedure (step 4).
This instruction is the normal procedure call instruction used to
call all procedures.
The library procedure, possibly written in assembly language,
typically puts the system call number in a place where the
operating system expects it, such as a register (step 5).
Then it executes a TRAP instruction to switch from user mode to
kernel mode and start execution at a fixed address within the
kernel (step 6).
59
OPERATING SYSTEM OVERVIEW
The kernel code that starts following the TRAP examines the
system call number and then dispatches to the correct system call
handler, usually via a table of pointers to system call handlers
indexed on system call number (step 7).
At that point the system call handler runs (step 8).
Once it has completed its work, control may be returned to the
user space library procedure at the instruction following the TRAP
instruction (step 9).
This procedure then returns to the user program in the usual way
procedure call return (step 10).
To finish the job, the user program has to clean up the stack, as it
does after any procedure call (step 11).
Note: Traps and system calls are similar in that they both involve
transferring control to the kernel to perform privileged operations.
However, traps are usually generated automatically by the CPU
when a program encounters an error or attempts to execute a
privileged instruction, while system calls are initiated by the
program itself to request privileged operations.
60
OPERATING SYSTEM OVERVIEW
The History and evolution of Operating Systems
Operating systems have evolved through a number of distinct
phases or generations, which corresponds roughly to the decades.
The 1940's - First Generations
The earliest electronic digital computers had no operating
systems. Machines of the time were so primitive that programs
were often entered one bit at time on rows of mechanical switches
(plug boards).
Programming languages were unknown (not even assembly
languages). Operating systems were unheard of.
The 1950's - Second Generation:
By the early 1950's, the routine had improved somewhat with the
introduction of punch cards.
The General Motors Research Laboratories implemented the first
operating systems in early 1950's for their IBM 701. The system of
the 50's generally ran one job at a time.
These were called single-stream batch processing systems because
programs and data were submitted in groups or batches. 61
OPERATING SYSTEM OVERVIEW
The 1960's - Third Generation
The systems of the 1960's were also batch processing systems, but they
were able to take better advantage of the computer's resources by
running several jobs at once.
So operating systems designers developed the concept of
multiprogramming in which several jobs are in main memory at once;
a processor is switched from job to job as needed to keep several jobs
advancing while keeping the peripheral devices in use.
For example, on the system with no multiprogramming, when the
current job paused to wait for other I/O operation to complete, the CPU
simply sat idle until the I/O finished.
The solution for this problem that evolved was to partition memory
into several pieces, with a different job in each partition. While one job
was waiting for I/O to complete, another job could be using the CPU.
Another major feature in third-generation operating system was the
technique called SPOOLing (simultaneous peripheral operations on
line).
In spooling, a high-speed device like a disk interposed between a
running program and a low-speed deviceinvolved with the program in 62
input/output.
OPERATING SYSTEM OVERVIEW
Instead of writing directly to a printer, for example, outputs are
written to the disk. Programs can run to completion faster, and other
programs can be initiated sooner when the printer becomes available,
the outputs may be printed.
Another feature present in this generation was time-sharing
technique, a variant of multiprogramming technique, in which each
user has an on-line (i.e., directly connected) terminal.
Because the user is present and interacting with the computer, the
computer system must respond quickly to user requests, otherwise
user productivity could suffer.
Timesharing systems were developed for multiprogramming large
number of simultaneous interactive users.
Fourth Generation:
With the development of LSI (Large Scale Integration) circuits, chips,
operating system entered in the system entered in the personal
computer and the workstation.
Microprocessor technology evolved to the point that it became possible
to build desktop computers as powerful as the mainframes of the
1970s. 63
OPERATING SYSTEM OVERVIEW
Two operating systems have dominated the personal computer scene:
MS-DOS, written by Microsoft, Inc. for the IBMPC and other machines
using the Intel 8088 CPU and its successors, and UNIX, which is
dominant on the large personal computers using the Motorola 6899
CPU family.
64
Process:
A process is an instance of a program in execution. A process is always stored in the main
memory also termed as the primary memory or random access memory. Therefore, a process is
termed as an active entity. It disappears if the machine is rebooted. Several processes may be
associated with a same program. For e.g., in Windows, if we edit two text files, simultaneously,
in notepad, then it means we are implementing two different instances of the same program. On
a multiprocessor system, multiple processes can be executed in parallel. On a uni-processor
system, a process scheduling algorithm is applied and the processor is scheduled to execute
each process one at a time yielding an illusion of concurrency. For an operating system, these
two instances are separate processes of the same application.
Resources held by process include hardware state, memory, CPU etc. When a program is
loaded into the memory and it becomes a process, it can be divided into four sections: stack,
heap, text and data. The following image shows a simplified layout of a process inside main
memory.
1
Difference between process and program:
process program
A process is an instance of a program in A program is a group instruction to
execution. perform a specific task.
The resource requirement is quite high in The program only needs memory for
case of a process. storage.
Processes have considerable overhead No significant overhead cost.
The process has a shorter and very limited A program has a longer lifespan as it is
lifespan as it gets terminated after the stored in the memory until it is not
completion of the task. manually deleted.
New processes require duplication of the No such duplication is needed.
parent process.
Process holds resources like CPU, memory The program is stored on disk in some
address, disk, I/O, etc. file and does not require any other
resources.
A process is an active entity. A program is a passive entity.
• A process goes through a series of process states for performing its task.
2
to be assigned. i.e. the processes which are ready for the execution reside in the main memory
are called ready state processes.
Running: One of the processes from the ready state will be chosen by the OS depending upon
the scheduling algorithm. Hence, if we have only one CPU in our system, the number of running
processes for a particular time will always be one. The process whose instructions are being
executed is called running process.
Waiting (Block): When a process waits for a certain resource to be assigned or for the input
from the user then the OS move this process to the block or wait state and assigns the CPU to the
other processes.
Terminated (or completed): The process completed its execution. When a process finishes its
execution, it comes in the termination state. All the context of the process (Process Control
Block) will also be deleted the process will be terminated by the Operating system.
Process state diagram with suspended ready state and suspended blocked state:
3
secondary memory and makes room for the higher priority process. These processes complete
their execution once the main memory gets available and their wait is finished.
• Process Control Block (PCB) is a data structure used by operating system to store all the
information about a process.
• It is also known as Process Descriptor.
• When a process is created, the operating system creates a corresponding PCB.
• Information in a PCB is updated during the transition of process states.
• When a process terminates, its PCB is released.
• Each process has a single PCB.
• Process Number: Each process is allocated a unique number for the purpose of
identification.
• Process State: It specifies the current state of a process.
• Program Counter: It indicates the address of next instruction to be executed.
• Registers: These hold the data or result of calculations. The content of these registers is
saved so that a process can be resumed correctly later on.
• Memory Limits: It stores the amount of memory units allocated to a process.
• List of Open Files: It stores the list of open files and there access rights.
Roles of PCB:
• The PCB is most important and central data structure in an OS. Each PCB contains all the
information about a process that is needed by the OS.
4
• The blocks are read and/or modified by virtually every module in the OS, including those
involved with scheduling, resource allocation, interrupt processing and performance
monitoring and analysis that mean PCB defines the state of OS.
• When CPU switches from one process to another, the operating system uses the Process
Control Block (PCB) to save the state of process and uses this information when control
returns back process is terminated, the Process Control Block (PCB) released from the
memory.
• A context switch is the computing process of storing and restoring the state (context) of a
CPU such that multiple processes can share a single CPU resource. The context switch is
an essential feature of a multitasking operating system.
• Context switches are usually computationally intensive and much of the design of
operating systems is to optimize the use of context switches. There are three scenarios
where a context switch needs to occur: multitasking, interrupt handling, user and kernel
mode switching.
• In a context switch, the state of the first process must be saved so that, when the
scheduler gets back to the execution of the first process, it can restore this state and
continue.
• The state of the process includes all the registers that the process may be using,
especially the program counter, plus any other operating system specific data that may be
necessary. Often, all the data that is necessary for state is stored in one data structure,
called a process control block.
Context switching:
Switching the CPU from one process to another process by saving the current state of running
process in PCB and loading the saved state from PCB for new process is called context
switching. Or the process of switching from one process to another is called context
switching.
Switching steps are:
• Save the context of running (First) in PCB.
• Place the PCB of the next (2nd) process, which has to execute, into the relevant queue i.e.
ready queue, I/O queue etc.
• Select a new (2nd) process for execution.
• Update the PCB of 2nd process by changing the state to running in PCB.
• Update the memory management data if required.
5
• After the execution of second process, restore the context of the first process on CPU.
Thread:
• A thread is a lightweight process or it is the entity within a process that can be scheduled
for execution.
• A thread is a subset of the process.
• A process is always stored in the main memory also termed as the primary memory or
random access memory.
• All threads of a process share its virtual address space and system resources and process
attributes.
• In addition, each thread maintains exception handlers, a scheduling priority, thread local
storage, a unique thread identifier, and a set of structures the system will use to save the
thread context until it is scheduled.
• The thread context includes the thread's set of machine registers, the kernel stack, a
thread environment block, and a user stack in the address space of the thread's process.
• On a uni-processor system, a thread scheduling algorithm is applied and the processor is
scheduled to run each thread one at a time.
Why Threads?
• Process with multiple threads makes a great server (e.g. print server)
• Increase responsiveness, i.e. with multiple threads in a process, if one threads blocks
then other can still continue executing
• Sharing of common data reduce requirement of inter-process communication
6
• Proper utilization of multiprocessor by increasing concurrency
• Threads are cheap to create and use very little resources
• Context switching is fast (only have to save/reload PC, Stack, SP, Registers)
7
Difference between process and thread:
Each process has their own address space. Threads share the address space of the process
that created it.
Each process has their own copy of the data, Threads share the same copy of data, code
code and file segment. and file segment.
Processes must use inter process Threads can directly communicate with other
communication to communicate with sibling threads of its process;
processes.
More resources are required Fewer resources are required
New processes require duplication of the New threads are easily created
parent process
Not suitable for parallelism Suitable for parallelism
A change to the parent process does not affect Changes to the main thread (cancellation,
child processes.(blocking the process does not priority change, etc.) may affect the behavior
affect the other process) of the other threads of the process;
System call is involved in process. (for example There is no system call is involved in thread.
(is api which create thread)
fork())
Context switching is slow Context switching is faster.
Threads Implementation
Threads are implemented in following two ways:
• User Level Threads -- User managed threads
• Kernel Level Threads -- Operating System managed threads acting on kernel, an
operating system core.
8
Advantages:
• The user threads can be easily implemented than the kernel thread.
• User-level threads can be applied to such types of operating systems that do not support
threads at the kernel-level.
• It is faster and efficient.
• Context switch time is shorter than the kernel-level threads.
• It does not require modifications of the operating system.
Disadvantages:
• User-level threads lack coordination between the thread and the kernel.
• If a thread causes a page fault, the entire process is blocked.
Kernel-Level Threads:
In this method, the kernel knows about and manages the threads. Instead of process table in each
process, the kernel has a thread table that keeps track of all threads in the system. Scheduling by
the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and
management in Kernel space. Kernel threads are generally slower to create and manage than the
user threads.
Advantages:
• The kernel-level thread is fully aware of all threads.
9
• The scheduler may decide to spend more CPU time in the process of threads being large
numerical.
• The kernel-level thread is good for those applications that block the frequency.
Disadvantages:
• The kernel thread manages and schedules all threads.
• The implementation of kernel threads is difficult than the user thread.
• The kernel-level thread is slower than user-level threads.
Hybrid thread:
Some operating system provides a combined user level thread and Kernel level thread facility.
Solaris is a good example of this combined approach. In a combined system, multiple threads
within the same application can run in parallel on multiple processors and a blocking system call
need not block the entire process. For example: Solaris
User threads are implemented by users. Kernel threads are implemented by OS.
OS doesn’t recognize user level threads. Kernel threads are recognized by OS.
If one user level thread performs blocking If one kernel thread performs blocking
operation then entire process will be operation then another thread can continue
blocked. execution.
User level thread is generic and can run on Kernel level thread is specific to the
any operating system. operating system.
10
Multithreading:
The use of multiple threads in a single program, all running at the same time and performing
different tasks is known as multithreading. Multithreading is a type of execution model that
allows multiple threads to exist within the context of a process such that they execute
independently but share their process resources. It enables the processing of multiple threads at
one time, rather than multiple processes.
Multithreading Model:
A relationship must exist between user threads and kernel threads. There are three models related
to user and kernel and they are:
➢ Many to many relationships: it maps many user threads to single kernel threads
➢ Many to one relationship: it maps many user threads to single kernel threads.
➢ One to one relationship: it mach each user thread to a corresponding
Kernel thread
Many -To -many model.
• The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads, combining the best features of the one-to-one and
many-to-one models.
• Users have no restrictions on the number of threads created.
• Blocking kernel system calls do not block the entire process.
• Processes can be split across multiple processors.
• Individual processes may be allocated variable numbers of kernel threads, depending on
the number of CPUs present and other factors.
• IRIX, HP-UX,andTru64 UNIX (Solaris prior to v9) uses Many-to-Many
Many-To-One Model:
• In the many-to-one model, many user-level threads are all mapped onto a single kernel
thread.
• Thread management is handled by the thread library in user space, which is very
efficient.
• However, if a blocking system call is made, then the entire process blocks, even if the
other user threads would otherwise be able to continue.
• Because a single kernel thread can operate only on a single CPU, the many-to-one model
does not allow individual processes to be split across multiple CPUs.
• Green threads for Solaris and GNU Portable Threads implement the many-to-one model
in the past, but few systems continue to do so today.
• Solaris uses Many-to-One
11
One-To-One Model:
• The one-to-one model creates a separate kernel thread to handle each user thread.
• This model provides more concurrency than the many to one model. It has also another
thread to run when a thread makes a blocking system call.
• It supports multiple threads to execute in parallel on microprocessors.
• One-to-one model overcomes the problems listed above involving blocking system calls
and the splitting of processes across multiple CPUs.
• However the overhead of managing the one-to-one model is more significant, involving
more overhead and slowing down the system.
• Linux, OS/2, Windows NT and windows 2000 use one to one relationship model.
Process Scheduling:
Scheduling: Scheduling refers to a set of policies and mechanism built in to the operating
system that govern the order in which the work to be done by a computer system.
This method of selecting a process to be allocated to CPU is called Process Scheduling.
Technical terms used in scheduling:
Ready queue: The processes waiting to be assigned to a processor are put in a queue, called
ready queue.
Burst time: The time for which a process holds the CPU is known as burst time.
Arrival time: Arrival Time is the time at which a process arrives at the ready queue.
Turnaround time: The interval from the time of submission of a process to the time of
12
completion is the turnaround time. Or Time Difference between completion time and arrival
time. i.e. TAT= CT - AT
Waiting time: Waiting time is the amount of time a process has been waiting in the ready queue
or Time Difference between turnaround time and burst time. i.e.WT = TAT - BT
Response Time: Time between submission of requests and first response to the request.
Throughput: number of processes completed per unit time.
Dispatch latency – It is the time it takes for the dispatcher to stop one process and start another
running
Context switch: A context switch is the computing process of storing and restoring the state
(context) of a CPU so that execution can be resumed from the same point at a later time. This
enables multiple processes to share a single CPU.
Optimal scheduling algorithm will have minimum waiting time, minimum turnaround time and
minimum number of context switches.
Schedule:
Schedulers are special system software which handles process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to run.
Types of Scheduler
➢ Long term scheduler
➢ Mid - term scheduler
➢ Short term scheduler
Long term scheduler:
Long term scheduler is also known as job scheduler.
The duty of the long-term scheduler is to bring the processes from the job pool (secondary
memory) and keeps them in the ready queue maintained in the primary memory for execution.
So, the long-term scheduler decides which process is to be created to put into the ready state. The
purpose of long term scheduler is to choose a perfect mix of IO bound (a process is said to be I/O
bound if the majority of the time is spent on the I/O operation) and CPU bound (a process is said
to be CPU bound if the majority of the time is spent on the CPU) processes among the jobs
present in the pool. If the job scheduler chooses more IO bound processes then all of the jobs
may reside in the blocked state all the time and the CPU will remain idle most of the time.
13
Short term scheduler:
Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the ready
queue and dispatch to the CPU for the execution.
A scheduling algorithm is used to select which job is going to be dispatched for the execution.
The Job of the short term scheduler can be very critical in the sense that if it selects job whose
CPU burst time is very high then all the jobs after that, will have to wait in the ready queue for a
very long time.
This problem is called starvation which may arise if the short term scheduler makes some
mistakes while selecting the job.
Process Queues:
The Operating system manages various types of queues for each of the process states. The PCB
related to the process is also stored in the queue of the same state. There are the following queues
maintained by the Operating system.
Job Queue:
In starting, all the processes get stored in the job queue. It is maintained in the secondary
memory. The long term scheduler (Job scheduler) picks some of the jobs and put them in the
primary memory.
Ready Queue:
Ready queue is maintained in primary memory. The short term scheduler picks the job from the
ready queue and dispatch to the CPU for the execution.
Waiting Queue:
When the process needs some I/O operation in order to complete its execution, OS changes the
state of the process from running to waiting. The context (PCB) associated with the process gets
stored on the waiting queue which will be used by the Processor when the process finishes the
I/O.
14
Scheduling Objectives:
• Fairness: Fairness can be reflected by treating all the processes same and no process
should suffer indefinite postponement.
• Maximum throughput: The throughput of a system is the number of processes (or
threads) that actually complete in a period of time.
• Resource: Scheduling mechanism should keep the resources of the system busy.
• Priorities: Scheduling mechanism should favor the high priorities process.
• Overhead: A certain portion of the system resources invested as overhead can greatly
improve the overall performance of the system.
• Predictable: A given job should run in about the same amount of time and at the cost
irrespective of the load on the system.
Scheduling criteria: The criteria used for comparing these algorithms include the following:
• CPU Utilization: Keep the CPU as busy as possible. It ranges from 0 to 100%.
In practice, it ranges from 40 to 90%.
• Throughput: Throughput is the rate at which processes are completed per unit of time.
• Turnaround time: This is the how long a process takes to execute a process. It is
calculated as the time gap between the submission of a process and its completion.
• Waiting time: Waiting time is the sum of the time periods spent in waiting in the ready
queue.
• Response time: Response time is the time it takes to start responding from submission
time. It is calculated as the amount of time it takes from when a request was submitted
until the first response is produced.
• Fairness: Each process should have a fair share of CPU.
16
• The OS makes sure that CPU usage is the same by all running process.
• In this, the usage of CPU is the same, i.e., all the running processes will make use of CPU
equally.
• This scheduling method also improvises the average response time.
• Preemptive Scheduling is beneficial when we use it for the multi-programming
environment.
CPU Scheduling Algorithm: The rule or set of rules that is followed to schedule the process is
known as process scheduling algorithm.CPU Scheduling deals with the problem of deciding
which of the processes in the ready queue is to be allocated first to the CPU. Following are the
commonly used scheduling algorithms:
▪ First-Come-First-Served (FCFS)
▪ Shortest Job First (SJF)
▪ Round-Robin Scheduling (RR)
▪ Priority Scheduling
▪ Multi-Level Queue Scheduling (MLQ)
▪ Multi-Level Feedback Queue Scheduling (MFQ)
First Come First Serve (FCFS):
• First Come First Serve (FCFS) is an operating system scheduling algorithm that
automatically executes queued requests and processes in order of their arrival.
• In this type of algorithm, processes which request the CPU first, get the CPU allocation
first.
• This is managed with a FIFO queue. As the process enters the ready queue, its PCB
(Process Control Block) is linked with the tail of the queue and, when the CPU becomes
free, it should be assigned to the process at the beginning of the queue.
• FCFS scheduling algorithm is non-preemptive.
• Once the CPU is allocated to a process, that process keeps the CPU until it releases the
CPU, either by terminating or by I/O request.
17
Example: Consider the following example
Using FCFS algorithm find the average waiting time and average turnaround time if the order is
P1, P2, P3, P4.
Sol:
If the process arrived in the order P1, P2, P3, P4 then according to the FCFS the Gantt chart will
Be:
P1 P2 P3 P4
0 3 8 10 14
The waiting time for process P1 = 0, P2 = 3, P3 = 8, P4 = 10 then the turnaround time for
process P1 = 0 + 3 = 3, P2 = 3 + 5 = 8, P3 = 8 + 2 = 10, P4 = 10 + 4 =14.
Then average waiting time = (0 + 3 + 8 + 10)/4 = 21/4 = 5.25
Average turnaround time = (3 + 8 + 10 + 14)/4 = 35/4 = 8.75
The FCFS algorithm is non preemptive means once the CPU has been allocated to a process then
the process keeps the CPU until the release the CPU either by terminating or requesting I/O.
Example1:
Consider the following set of processes that arrive at time 0 with the length of the CPU burst
time in milliseconds:
Now,
Waiting Time for P1 = 0 milliseconds
18
Waiting Time for P2 = 24 milliseconds
Waiting Time for P3 = 27 milliseconds
Average Waiting Time = (Total Waiting Time) / No. of Processes
= (0 + 24 + 27) / 3
= 51 / 3
= 17 millisecond
Suppose that the processes arrive in the order: P2 , P3 , P1
The Gantt chart for the schedule is:
Example3: Consider the set of 5 processes whose arrival time and burst time are given below-
19
If the CPU scheduling policy is FCFS, calculate the average waiting time and average
turnaround time.
Soln:
Gantt Chart-
We know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
If the CPU scheduling policy is FCFS, calculate the average waiting time and average turn
around time.
Soln:
Gantt Chart-
20
time Wt=tat-bt
P1 0 2 2 2-0=2 2-2=0
P2 3 1 4 4-3=1 1-1=0
P3 5 6 11 11-5=6 6-6=0
Now,
Average Turn Around time = (2 + 1 + 6) / 3 = 9 / 3 = 3 unit
Average waiting time = (0 + 0 + 0) / 3 = 0 / 3 = 0 unit
Advantages of FCFS
• The simplest form of a CPU scheduling algorithm
• Easy to program
• First come first served
Disadvantages of FCFS
• It is a Non-Preemptive CPU scheduling algorithm, so after the process has been allocated
to the CPU, it will never release the CPU until it finishes executing.
• The Average Waiting Time is high.
• Short processes that are at the back of the queue have to wait for the long process at the
front to finish.
• Not an ideal technique for time-sharing systems.
• Because of its simplicity, FCFS is not very efficient.
21
Non-Preemptive SJF:
• In non-preemptive scheduling, CPU is assigned to the process with least CPU burst time.
• The process keeps the CPU until it terminates.
Advantage:
• It gives minimum average waiting time for a given set of processes.
Disadvantage:
• It requires knowledge of how long a process will run and this information is usually not
available.
Example of Non-Preemptive:
Consider the following set of processes that arrive at time 0 with the length of the CPU burst
time in milliseconds:
22
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and
average turn around time.
Soln:
Now,
Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit
Example3:
-
23
The Gantt chart for the schedule is:
Example2: Consider the set of 5 processes whose arrival time and burst time are given below
Process Arrival Time Burst Time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average
turn around time.
Soln:
24
P1 3 1 4 4-3=1 1-1=0
P2 1 4 6 6-1=5 5-4=1
P3 4 2 8 8-4=4 4-2=2
P4 0 6 16 16-0=16 16-6=10
P5 2 3 11 11-2=9 9-3=6
Now,
Average Turn Around time = (1 + 5 + 4 + 16 + 9) / 5 = 35 / 5 = 7 unit
Average waiting time = (0 + 1 + 2 + 10 + 6) / 5 = 19 / 5 = 3.8 unit
Example3: Consider the set of 6 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is shortest remaining time first, calculate the average waiting time
and average turn around time.
Arrival
Process Id Burst time
time
P1 0 9
P2 1 4
P3 2 9
If the CPU scheduling policy is SRTF, calculate the average waiting time and average turn
around time.
Soln:
26
• External priorities are set by criteria outside the OS, such as importance of process, the
type and amount of funds being paid for computer user, and other political factors.
Priority scheduling can be either preemptive or non-preemptive.
• When a process arrives at the ready queue, its priority is compared with the priority of
currently running process. A preemptive priority scheduling algorithm will preempt the
CPU if the priority of the newly arrived process is higher than the priority of the currently
running process.
• A non-preemptive priority scheduling algorithm will simply put the new process at the
head of the ready queue.
• A major problem of such scheduling algorithm is indefinite blocking or starvation. A
process that is ready to run but waiting for the CPU can be considered blocked
Example: Non preemptive priority
Consider following set of processes, assumed to have arrived at time 0 in order P1, P2, …, P5
with the length of the CPU burst given in milliseconds
Soln:
Priority scheduling Gantt chart
27
Soln:
Gnatt chart for this is :
28
• Round robin is a pre-emptive algorithm
• The CPU is shifted to the next process after fixed interval time, which is called time
quantum/time slice.
• The process that is preempted is added to the end of the queue.
• Round robin is a hybrid model which is clock-driven
• Time slice should be minimum, which is assigned for a specific task that needs to be
processed. However, it may differ from OS to OS.
• It is a real time algorithm which responds to the event within a specific time limit.
• Round robin is one of the oldest, fairest, and easiest algorithms.
• Widely used scheduling method in traditional OS.
Example of Round Robin Scheduling:
Consider the following set of processes that arrive at time 0 with the length of the CPU burst
time in milliseconds:
P1 P2 P3 P1 P2 P1 P2 P1 P1
0 2 4 6 8 10 12 13 15 17
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
29
Average time: = (P1 + P2 + P3) / 3
= [ (10 – 4) + 4 + 7] / 3
= 17 / 3
= 5.66 milliseconds
Example2. 3. Consider the following set of processes that arrive at given time, with the length of
the CPU burst given in milliseconds. Calculate AWT, ATAT,
TQ= 2
P1 P2 P3 P1 P4 P5 P2 P6 P5 P2 P6 P5
0 2 4 6 8 9 11 13 15 17 18 19 21
ATAT=(18+17+4+6+17+13)/6=12.5 ms
AWT=(4+12+2+5+11+10)/6=
Example 4 Consider the following set of processes that arrive at given time, with the length of
the CPU burst given in milliseconds. Calculate AWT, ATAT,
30
TQ= 20
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
31
• Decreases comprehension
• Lower time quantum results in higher the context switching overhead in the system.
• Finding a correct time quantum is a quite difficult task in this system.
32
Two level scheduling:
Two-level scheduling is an efficient scheduling method that uses two schedulers to perform
process scheduling.
Consider the example:
Suppose a system has 50 running processes all with equal priority and the system’s memory can
only hold 10 processes simultaneously. Thus, 40 processes are always swapped out and written
on virtual memory on the hard disk. To swap out and swap in a process, it takes 50 ms
respectively.
Let us take up the above scenario with straightforward Round-robin scheduling: a process would
need to be swapped in (least recently used processes are swapped in) every time when a context
switch occurs. Swapping in and out costs too much, and the unnecessary swaps waste much time
of scheduler.
33
So the solution to the problem is two-level scheduling. There are two different schedulers in two-
level scheduling
Lower level scheduler –
This scheduler selects which process will run from memory.
Higher level scheduler –
This scheduler focuses on swapping in and swapping out the processes between hard disk and
memory. Swapping takes much time, therefore it does its scheduling much less often. It also
swaps out the processes which are running for a long time in memory and are swapped with
processes on disk that have not run for a long time.
34
Process synchronization and inter process communication:
Principle of Concurrency:
Concurrency is the tendency for things to happen at the same time in a system. Process that
coexists on the memory at a given time is called concurrent process. Processes running
concurrent in the OS may either be independent or cooperating process.
Independent process: The process does not share any kind of information or data with each
other is independent process. If the processes are independent, then execution of one process
does not affect the other process.
Cooperating process: The execution of one process affect the other process because of sharing
something is called cooperating process. The cooperating process may share memory, variable,
code and resource etc.
Process synchronization:
• Process synchronization is the way by which process that share the same memory space
managed in OS.
• Process Synchronization controls the execution of processes running concurrently so as
to produce the consistent results.
• Synchronization mechanisms allow the processes to access critical section in a
synchronized manner to avoid the inconsistent results.
If a person who doesn’t know to play music (gitar) and told him to play then he only create
because of no synchronization. So, if the process is not synchronize properly then it create noise
(create problem such as deadlock in a system).
Race condition:
A race condition is a situation where multiple processes concurrently read and write to a share
memory location and result depends on the order of execution.
For example:
int shared =5
Process P1 Process P2
int x=shared int y = shared
x=x+1 y = y—
sleep (1) sleep (1)
shared = x shared = y
//shared=6 and terminated // shared=4
Here shared= either 6 or 4 (which is not correct) because the process is not synchronize. This
problem is called race condition.
Critical section:
A part of the program, in which race condition can occur, is called critical section. i.e. critical
section is a part of the program where shared resources are accessed by the process.
If multiple processes access the critical section concurrently, then results produced might be
inconsistent. This problem is called as critical section problem.
A diagram that demonstrates the critical section is as follows:
1
Entry Section
• It acts as a gateway for a process to enter inside the critical section.
• It ensures that only one process is present inside the critical section at any time.
• It does not allow any other process to enter inside the critical section if one process is
already present inside it.
Exit Section:
• It acts as an exit gate for a process to leave the critical section.
• When a process takes exit from the critical section, some changes are made so that other
processes can enter inside the critical section.
Any synchronization mechanism proposed to handle the critical section problem or (to
eliminate the race condition) should meet the following criteria:
1. Mutual Exclusion
2. Progress
3. Bounded Wait
4. Architectural Neutral
Mutual Exclusion:
• The processes access the critical section in a mutual exclusive manner.
• Only one process is present inside the critical section at any time.
• No other process can enter the critical section until the process already present inside
it completes.
Progress:
• An entry of a process inside the critical section is not dependent on the entry of
another process inside the critical section.
• A process can freely enter inside the critical section if there is no other process
present inside it.
• A process is not forced to enter inside the critical section if it does not want to enter.
2
• That is, if a process is not using the critical section, then it should not stop any other
process from accessing it. In other words, any process can enter a critical section if it
is free.
Bounded Wait:
• Bounded waiting means that each process must have a limited waiting time. It should
not wait endlessly to access the critical section.
Architectural Neutral:
• It can run on any architecture without any problem.
• There is no dependency on the architecture.
Note1:
• Mutual Exclusion and Progress are the mandatory criteria.
• They must be fulfilled by all the synchronization mechanisms.
Note2:
• Bounded waiting and Architectural neutrality are the optional criteria.
• However, it is recommended to meet these criteria if possible.
Busy waiting: The act of using CPU continuously checking the condition to enter in to the
critical section is called busy looping or spinning or busy looping.
Solution of critical section problem by two approaches:
1. Mechanism with busy waiting for accessing critical section
2. Mechanism with suspension of waiting process
Mechanism with busy waiting includes the following methods:
• Lock variable method
• Turn variable method or strict alternation method
• Peterson’s solution
• TSL (Test and set lock)
Mechanism with suspension of waiting process(sleep and wake up) includes the
following methods:
• Semaphore
• Monitors
Lock variable method:
• It can be used for any number of processes.
• It is a software mechanism implemented in user mode.
• There is no support required from the operating system.
• It uses a lock variable to provide the synchronization among the processes executing
concurrently.
• It is a busy waiting solution which keeps the CPU busy when the process is actually
waiting.
3
The pseudo code of the mechanism looks like:
Working mechanism:
Case 1:
• Process P0 arrives.
• It executes the lock!=0 instruction.
• Since lock value is set to 0, so it returns value 0 to the while loop.
• The while loop condition breaks.
• It sets the lock value to 1 and enters the critical section.
• Now, even if process P0 gets preempted in the middle, no other process can enter the
critical section.
• Any other process can enter only after process P0 completes and sets the lock value to
0.
Case 2:
• Another process P1 arrives.
• It executes the lock!=0 instruction.
• Since lock value is set to 1, so it returns value 1 to the while loop.
• The returned value 1 does not break the while loop condition.
• The process P1 is trapped inside an infinite while loop.
• The while loop keeps the process P1 busy until the lock value becomes 0 and its
condition breaks.
Case 3:
• Process P0 comes out of the critical section and sets the lock value to 0.
• The while loop condition of process P1 breaks.
• It sets the lock value to 1 and enters the critical section.
• Now, even if process P1 gets preempted in the middle, no other process can enter the
critical section.
• Any other process can enter only after process P1 completes and sets the lock value to
0.
Failure mechanism:
4
• the mechanism completely fails to provide the synchronization among the
processes.
• It cannot guaranteed to meet criterion of mutual exclusion
Turn variable or strict alternation:
• Turn Variable is a synchronization mechanism that uses a turn variable to provide the
synchronization among the processes. Processes are executed turn wise one by one.
• It ensures mutual exclusion.
• It ensures bounded waiting since processes are executed turn wise one by one and each process is
guaranteed to get a chance but progress is not guaranteed.
• It is architectural neutral since it does not require any support from the operating system.
It is implemented as:
Case 1:
• Process P0 arrives.
• It executes the turn!=0 instruction.
• Since turn value is set to 0, so it returns value 0 to the while loop.
• The while loop condition breaks.
• Process P0 enters the critical section and executes.
• Now, even if process P0 gets preempted in the middle, process P1 cannot enter the critical section.
• Process P1 cannot enter unless process P0 completes and sets the turn value to 1.
Case 2:
• Process P1 arrives.
• It executes the turn!=1 instruction.
• Since turn value is set to 0, so it returns value 1 to the while loop.
• The returned value 1 does not break the while loop condition.
• The process P1 is trapped inside an infinite while loop.
• The while loop keeps the process P1 busy until the turn value becomes 1 and its condition breaks.
Case 3:
• Process P0 comes out of the critical section and sets the turn value to 1.
5
• The while loop condition of process P1 breaks.
• Now, the process P1 waiting for the critical section enters the critical section and execute.
• Now, even if process P1 gets preempted in the middle, process P0 cannot enter the critical section.
• Process P0 cannot enter unless process P1 completes and sets the turn value to 0.
Critical Section
6
Case 1: Po P1
interest[0]= F,T, F interest[1]= F, T
P0:12345CS
P1:12345(busy)
P0:6
P1: 5CS
Case 2: interest[0]= F,T interest[1]= F, T
P0:123! P1:123! P0:45(busy)!P1:45(busy)!P0:5CS
[note:! Represent the process is preempted after execution the line number 3, 5]
Consider two cooperative processes P0 and P1. Initially, the value of interested variables and turn
variable is 0.
At first, when process P0 arrives and wants to enter into the critical section. It sets its interested
variable to True (instruction line 3) and also sets turn to 1 (line number 4). Since the condition given
in line number 5 is completely satisfied by P0 therefore it will enter in the critical section.
P1 → 1 2 3 4 5 CS
Now if, Process P0 got preempted and process P1 got scheduled. P1 also wants to enter in the critical
section and executes instructions 1, 2, 3 and 4 of entry section. On instruction 5, it got stuck since it
doesn't satisfy the condition.
Therefore it gets into the busy waiting. P2 → 1 2 3 4 5
P0 again got scheduled and finish the critical section by executing the instruction no. 6 (setting
interested variable to false). Now if P1 checks then it are going to satisfy the condition since other
process's interested variable becomes false. P2 will also get enter the critical section.
P1 → 6, P2 → 5 CS
It is implemented as:
int lock=false Sd lock=false
while(TestAndSet( &lock));
{ P1:
CS For p1, return r means return false.
lock=false P2:
} For p2, lock is true, so r=*target =true
boolean TestAndSet(boolean *target) And p2 get stuck in loop.
{
Boolean r=*target
*target=true
return(r)
}
7
Synchronization Mechanism without busy waiting (sleep and wake up):
Busy waiting is not the optimal solution for allocation of resources because it keeps CPU busy
all the time in checking the while loops condition continuously although the process is waiting
for the critical section to become available.
The basic idea behind the sleep and wake mechanism is that if the critical section is not empty
then the process will go and sleep. It will be waked up by the other process which is currently
executing inside the critical section so that the process can get inside the critical section.
There is a popular example called producer consumer problem which is the most popular
problem simulating sleep and wake mechanism.
Semaphore:
Proposed by Dijkstra in 1965.
A semaphore is a simple integer variable used to provide synchronization among the processes
running concurrently. It is guaranteed that once a semaphore operation has started no other process
can access the semaphore until the operation has completed or block.
The Semaphores usually have two operations. These two operations have the capability to decide
the values of the semaphores.
The two Semaphore operations are: wait ( ) and signal ( )
Wait ( ): The wait Operation is used for deciding the condition for the process to enter the
critical state or wait for execution of process. The wait () operation is also known as P()
operation or Down () operation or sleep() operation. If the Semaphore value is greater than zero
or positive then the Process can enter the critical section area. The structure of wait() process is
wait (semaphore s)
{ it is also written as:
s.value = s.value - 1;
Down(semaphore s)
if (s.value < 0) { {
{ while(s<=0);
put process (PCB) in L; s=s-1;
sleep(); }
}
else
return;
}
Signal ( ): The signal operation operation is used to update the value of Semaphore. The signal
operation is also known as wake up() Operation or Up ( ) Operation or V( ) operation. The
Signal Operation is executed only when the process comes out of the critical section. The value
of semaphore cannot be incremented before the exit of process from the critical section. The
structure of signal operation is:
signal (semaphore s)
{ it is also written as:
s.value = s.value + 1; up(semaphore s)
if (s.value <=0 ) { {
{ s=s+1;
select a process (PCB) from L; }
wake up();
8
}
}
There are mainly two types of semaphores:
1. Counting Semaphores
2. Binary Semaphores or Mutexes
Counting Semaphores:
• A counting semaphore has two components-: An integer value and an associated waiting list
(usually a queue).
• The value of counting semaphore may be positive or negative. (-∞ to + ∞).
• In this, semaphore variable that can take more than two values which represent the list of task or
entity.
• Positive value indicates the number of processes that can be present in the critical section
at the same time.
• Negative value indicates the number of processes that are blocked in the waiting list.
• The waiting list of counting semaphore contains the processes that got blocked when trying to
enter the critical section.
• The wait operation is executed when a process tries to enter the critical section. Wait
operation decrements the value of counting semaphore by 1.
o Then, following two cases are possible:
Case1:- If the resulting value of counting semaphore is greater than or equal to
0, process is allowed to enter the critical section.
Case2:
If the resulting value of counting semaphore is less than 0, process is not allowed
to enter the critical section. In this case, process is put to sleep in the waiting list.
• The signal operation is executed when a process takes exit from the critical section.
Signal operation increments the value of counting semaphore by 1. Now, following two
cases are possible:-
Case 1: If the resulting value of counting semaphore is less than or equal to 0, a process
is chosen from the waiting list and wake up to execute.
Case 2: If the resulting value of counting semaphore is greater than 0, no action is taken.
It is implemented as:
wait (semaphore svalue)
{ signal (semaphore svalue)
svalue = svalue - 1; {
if (svalue < 0) svalue = svalue + 1;
{ if (svalue <=0 )
put process (PCB) in SL; {
sleep(); select a process (PCB) from SL;
} wake up(); //select from suspended list and put in to ready
else queue
return; }
} }
Example:
Suppose initial semaphore int s=3;
9
Problem-01: A counting semaphore S is initialized to 10. Then, 6 P operations and 4 V
operations are performed on S. What is the final value of S?
Ans: Final value of semaphore variable S= 10 – (6 x 1) + (4 x 1)=8
Problem-01: A counting semaphore S is initialized to 7. Then, 20 P operations and 15 V operations are
performed on S. What is the final value of S? Ans: 2
Binary semaphores:
In counting semaphore, Mutual exclusion was not provided because there are set of processes which
required executing in the critical section simultaneously. Binary Semaphore strictly provides mutual
exclusion.
Binary semaphore is initialized to 1 and takes only two values 1 and 0 during execution of program. It is
implemented in such way that any attempting to lock a semaphore whose value is zero is blocked until
the value 1, then unlock it and set to 0.
• The wait operation is executed when a process tries to enter the critical section. Then, there are
two cases possible:-
Case 1: If the value of binary semaphore is 1. The value of binary semaphore is set to 0.The
process is allowed to enter the critical section.
Case 2: If the value of binary semaphore is 0, the process is blocked and not allowed to enter the
critical section. The process is put to sleep in the waiting list.
• The signal operation is executed when a process takes exit from the critical section. Then, there
are two cases possible:
Case 1 If the waiting list is empty, the value of binary semaphore is set to 1.
Case 2: If the waiting list is not empty, a process is chosen from the waiting list and wake up to
execute
It is implemented as:
10
Difference between binary semaphore and counting semaphore:
11
Reader writer problem and solution:
The readers-writers problem is a classical problem of process synchronization, it relates to a data
set such as a file that is shared between more than one process at a time. In this type of problem
there are two types of process are used such as reader process and writer process.
The reader process is responsible for only reading and the writer process is responsible for
writing.
This is an important problem of synchronization which has several variations like
• If two or more than two readers want to access the file at the same time there will be no
problem.
• When two writers or one reader and one writer want to access the file at the same point of
time, there may occurs some problem.
• Hence the task is to design the code in such a manner that if one reader is reading then
no writer is allowed to update at the same point of time.
• Similarly, if one writer is writing no reader is allowed to read the file at that point of time
and if one writer is updating a file other writers should not be allowed to update the file at
the same point of time.
• But, multiple readers can access the object at the same time.
12
The structure of a reader process is as follows:
do {
wait(mutex); // Reader wants to enter the critical section
nreadcount++; // The number of readers has now increased by 1
if (nreadcount == 1) //If this is the first reader, lock wrt to block writers
wait (wrt); //this ensure no other writer can enter if there is even one reader
signal(mutex); //Allow other readers to enter by unlocking the mutex so that other readers can also
//enter while this current reader is inside
// current reader performs reading here
wait(mutex); // a reader wants to leave and Lock the mutex to update readcount
nreadcount --; //The number of readers has now decreased by 1
if (nreadcount == 0) // If, no reader is left in the critical section
signal(wrt); // unlock wrt to allow writers can enter
signal(mutex); //Allow other readers or writers to proceed by unlocking the mutex
} while(true);
13
The following are the solution to the problems that might occur in the producer-
consumer:
Solution:
The solution for the producer is to either go to sleep or discard data if the buffer is full.
When the consumer removes an item from the buffer, it must notify the producer who
starts to fill the buffer again.
Similarly, consumer can go to sleep if it finds the buffer empty. The next time the
producer puts data into the buffer, it wakes up the sleeping consumer.
The solution can be implemented using the IPC (inter-process communication).
Pseudocode
#define N 100
int itemcount=0;
void producer()
{ int item
while(true)
{
item=produce_ item();
if(itemcount=N) //buffer full
sleep();
put_item_into_buffer(item);
itemcount++;
if(itemcount=1)
wakeup(consumer);
}
}
void consumer()
{ int item;
while(true)
{
if(itemcount=0) //buffer empty //case for !exe. and preempted
sleep();
item=remove_item(item);
itemcount= itemcount-1;
if(itemcount==N -1)
wakeup(producer);
consume_item(item);
}
}
The producer produces the item and inserts it into the buffer. The value of the global
variable count gets increased at each insertion. If the buffer is filled completely and no
slot is available then the producer will sleep, otherwise it keeps inserting.
On the consumer's process, the value of count got decreased by 1 at each consumption. If
the buffer is empty at any point of time, then the consumer will sleep otherwise, it keeps
consuming the items and decreasing the value of count by 1.
14
The consumer will be waked up by the producer if there is at least 1 item available in the
buffer which is to be consumed. The producer will be waked up by the consumer if there
is at least one slot available in the buffer so that the producer can write that.
Well, the problem arises in the case when the consumer got preempted just before it was
about to sleep. Now, the consumer is neither sleeping nor consuming. Since the producer
is not aware of the fact that consumer is not actually sleeping therefore it keep waking the
consumer while the consumer is not responding since it is not sleeping.
This leads to the wastage of system calls. When the consumer gets scheduled again, it
will sleep because it was about to sleep when it was preempted.
The producer keeps writing in the buffer and it got filled after some time. The producer
will also sleep at that time keeping in the mind that the consumer will wake him up when
there is a slot available in the buffer.
The consumer is also sleeping and not aware with the fact that the producer will wake
him up.
This is a kind of deadlock where neither producer nor consumer is active and waiting for
each other to wake them up. This is a serious problem which needs to be addressed.
The above three problems can be solved with the help of semaphores.
Three semaphore variables used in producer-consumer problem solution using semaphore
are:
1. Semaphore S: This semaphore variable is used to achieve mutual exclusion between
processes. By using this variable, either Producer or Consumer will be allowed to use or
access the shared buffer at a particular time. This variable is set to 1 initially.
2. Semaphore E: This semaphore variable is used to define the empty space in the
buffer. Initially, it is set to the whole space of the buffer i.e. "n" because the buffer is
initially empty.
3. Semaphore F: This semaphore variable is used to define the space that is filled by the
producer. Initially, it is set to "0" because there is no space filled by the producer initially.
The following is the pseudo-code for solution of problem using semaphore that occurs in
producer consumer problem solution using sleep and wake up method:
#define N 100 //maximum slots in buffer
int count=0 //items in the buffer
semaphore S=1, F=0, E=4;
void producer()
{
int item;
while(T) {
item=produce_item();//generates item
wait(E); // decrement empty slot
wait(S); //set S to 0 i.e. enter into CS
append(); //add item to buffer
signal(S); //set S to 1 i.e. informing CS is free
signal(F); //increment full slot
}
}
The above code can be summarized as:
15
• while() is used to produce data, again and again, if it wishes to produce, again and
again.
• produce() function is called to produce data by the producer.
• wait(E) will reduce the value of the semaphore variable "E" by one i.e. when the
producer produces something then there is a decrease in the value of the empty space in
the buffer. If the buffer is full i.e. the value of the semaphore variable "E" is "0",
then the program will stop its execution and no production will be done.
• wait(S) is used to set the semaphore variable "S" to "0" so that no other process can
enter into the critical section.
• append() function is used to append the newly produced data in the buffer.
• signal(s) is used to set the semaphore variable "S" to "1" so that other processes can
come into the critical section now because the production is done and the append
operation is also done.
• signal(F) is used to increase the semaphore variable "F" by one because after adding the
data into the buffer, one space is filled in the buffer and the variable "F" must be
updated.
This is how we solve the produce part of the producer-consumer problem. Now, let's see
the consumer solution.
The following is the code for the consumer:
void consumer()
{
int item;
while(T)
{
wait(F); // decrement full counter
wait(S); //set S to 0 i.e. enter to CS
take(item); // consume item
signal(S); // set S to 1 i.e. informing CS is free
signal(E); // increment empty counter
use(); // make use of removed item if needed by any process
}
}
16
• signal(E) is used to increase the semaphore variable "E" by one because after taking the
data from the buffer, one space is freed from the buffer and the variable "E" must be
increased.
• use () is a function that is used to use the data taken from the buffer by the process to do
some operation
17
thinking ()
}
}
• Although this solution guarantees that not two neighbor philosophers are eating
simultaneously, it has the possibility of deadlock.
• Suppose that all 5 philosophers grab their left chopstick simultaneously then all the
elements of chopstick[5] are equal to 0, so no philosopher will be able to grab his right
chopstick and deadlock occur.
Monitor:
Monitor is defined as a collection of procedures, variables and data structures that are all
grouped together in a special kind of module or package.
Monitors are the process synchronization tool used to manage the access of shared resources
and coordinate the actions of various processes.
It is the high level tool for process synchronization whereas semaphores are primitive and
flexible tool for mutual exclusion.
Code inside monitors can only be executed by one process at a time i.e.only one process may
be active within the monitor at a time
Monitors are supported by Programming languages to achieve process mutual exclusion. Java
synchronized methods, for instance. Java has the constructs wait() and notify().
Condition variables are employed within the monitor to control synchronization and
communication. Two different operations are performed on the condition variables of the
monitor. wait() and signal()
let say we have 2 condition variables:
condition x, y; // Declaring variable
x.wait() : Process performing wait operation on any condition variable are suspended. The
suspended processes are placed in block queue of that condition variable.
18
x.signal(): When a process performs signal operation on condition variable, one of the
blocked processes is given chance.
Then syntax for monitor is:
monitor monitor-name
{
// shared variable declarations
procedure P1 (…)
{ …. }
…
procedure Pn (…)
{……}
Initialization code ( ….)
{…}
…
Mutex:
A mutex is a variable that can be in one of two states: unlocked or locked. Consequently,
only 1 bit is required to represent it, but in practice an integer often is used, with 0 meaning
unlocked and all other values meaning locked.
Mutexes are good only for managing mutual exclusion to some shared resource or piece of
code.
Two procedures are used with mutexes. When a thread (or process) needs access to a critical
region, it calls mutex-lock. If the mutex is currently unlocked (meaning that the critical
region is available), the call succeeds and the calling thread is free to enter the critical
region. On the other hand, if the mutex is already locked, the calling thread is blocked until
the thread in the critical region is finished and calls mutex-unlock. If multiple threads are
blocked on the mutex, one of them is chosen at random and allowed to acquire the lock.
Inter Process Communication (IPC): Inter Process Communication (IPC) is a mechanism
that allows various processes to communicate and synchronize their actions for managing
shared data. It is the mechanism where the operating systems allow various processes to
communicate with each other synchronizing their actions. IPC lets different programs run in
parallel, share data, and communicate with each other. IPC are useful for
• it speeds up the execution of tasks.
• It ensures that the tasks run correctly and in the order that they were executed.
• IPC is essential for the efficient operation of an operating system.
The Need for IPC :
• Data Sharing : Processes often need to share data. For example, a text editor may need to
pass data to a printer process to generate a hard copy.
• Synchronization : Processes may need to synchronize their activities. For instance, in a
multi-threaded environment, threads must coordinate to ensure data consistency.
20
• Communication: Processes might need to communicate for a variety of purposes, such as
exchanging information, signaling, and error handling.
• Resource Sharing: IPC helps manage and share resources, like file access, memory, or
hardware devices among processes.
Two basic communication models for providing IPC are:
• Shared Memory
• Message Passing
Shared Memory:
Communication between processes using shared memory requires processes to share some
variable and it completely depends on how programmer will implement it. This type of
memory should protect each other by synchronizing access between all processes.
Suppose process A wants to communicate with process B then it need to attach its address
space to this shared memory segment. Process A will write a message to the shared memory,
and Process B will read that message from the shared memory. So, processes are responsible
for ensuring synchronization so that both processes do not write to the same location at the
same time.
Message Passing:
Message passing provides a mechanism that allows various processes to communicate and
synchronize their actions without sharing same address space.
Message passing method allows multiple processes to read and write data to the message queue
without being connected to each other. Messages are stored on the queue until their recipient
retrieves them.
It is particularly useful in distributed system environment, where the communicating processes
may reside in machine connected by network. Message passing is slower as compared to shared
memory method.
If processes P and Q want to communicate, they must send messages to and receive messages
from each other through a communication link that must exist between them.
21
Fig. IPC using message passing
Direct Communication: The process which wants to communicate must explicitly name the recipient
or sender of the communication.
send (P, message) – send a message to process P
✦ receive(Q, message) – receive a message from process Q Properties of communication link
✦ Links are established automatically.
✦ A link is associated with exactly one pair of communicating processes
Indirect Communication: Messages are directed and received from mailboxes (also referred to as
ports). ✦ Each mailbox has a unique id. ✦ Processes can communicate only if they share a
mailbox.
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
22
Comparison between the semaphore and monitor are as follows:
23
MEMORY MANAGEMENT
Memory Management:
Program must be brought (from disk) into memory and placed
within a process for it execution
OS loads the program into the memory and then only the CPU
can access the instructions and the data from memory while
executing the process
In a uni-programming system, main memory is divided into two
parts: one part for the operating system and one part for the
program currently being executed.
In a multiprogramming system, the “user” part of the memory
must be further subdivided to accommodate multiple processes
The task of subdivision is carried out dynamically by the
operating system and it is known as memory management
Thus, Memory management is a method in the operating system
manages operations between main memory and disk during
process execution.
1
MEMORY MANAGEMENT
4
MEMORY MANAGEMENT
Basic Hardware:
Main memory and registers the storage are only CPU can access
directly
Registers that are built into the CPU are generally accessible
within one cycle of the CPU clock.
Memory access may take many cycles of the CPU clock to
complete, in which case the processor normally needs to stall,
since it does not have the data required to complete the
instruction that it is executing.
The remedy is to add fast memory between the CPU and main
memory: cache
We must not only be concerned with relative speed of accessing
the physical memory, but also ensure the protection of one user
process from another.
5
MEMORY MANAGEMENT
Basic Hardware:
We need to make sure that each process has a separate memory
space.
For this, we need to determine the range of legal addresses that
the process may access and to ensure that the process can access
only these legal addresses.
This protection is ensured by using two registers, a base register
and a limit register.
The base register holds the smallest legal physical memory
address
Limit register specifies the size of the range.
6
MEMORY MANAGEMENT
For example:
7
MEMORY MANAGEMENT
8
MEMORY MANAGEMENT
Memory management terminology:
Segment: chunk of memory assigned to a process.
Logical address versus Physical Address:
An address generated by the CPU is known as “Logical Address”.
It is also known as a Virtual address.
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.
Logical and physical addresses are the same in compile-time and
load-time address-binding schemes.
Virtual and physical addresses differ in execution-time address-
binding scheme.
Logical-address space and physical address space:
The set of all logical addresses generated by a program is referred
to as a logical address space.
9
MEMORY MANAGEMENT
Memory management terminology:
For example, when 32-bit addressing is in use, addresses can range from 0,
2^31 possible numbers, for a total theoretical size of 2 gigabytes.
The set of all physical addresses corresponding to these logical addresses is
referred to as a physical address space.
For 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 = log2227= 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
Symbolic addresses:
The addresses used in a source code are symbolic address. The variable names,
constants, and instruction labels are the basic elements of the symbolic address
space.
10
Relative addresses:
At the time of compilation, a compiler converts symbolic addresses into relative
addresses.
MEMORY MANAGEMENT
Address binding:
Address binding is the process of converting (symbolic/relative) logical
address in to the physical (absolute) address in main memory. It is done
by MMU.
11
12
MEMORY MANAGEMENT
Address binding method: The binding of instructions and data to
memory addresses can be done by following methods:-
a. Compile time: If the location at which the program will reside
in main memory is known at compile time, then absolute (actual)
physical address is generated at compile time is called compile
time address binding.
The compiler generates the exact physical location in memory
starting from some fixed starting position k.
If, some later time, the starting location changes, then it will be
necessary to recompile this code.
c. Load time: If the location at which the program will reside in
main memory is not known at compile time, then relative address
(reference address) relative to the start of program relocatable
address is generated using which absolute address can be
calculated. It is known as load time address binding.
That is, if the location at which the program will reside in main
memory is not known at compile time, then the compiler must 13
generate relocatable address.
At load time, OS decides the starting position
MEMORY MANAGEMENT
c. Execution time: If the location at which the program will reside
in main memory is not known before the execution, then address
binding will be done during execution time.
That is, if the process can be moved during its execution from one
memory segment to another, then binding must be delayed until
run time.
The run-time mapping from virtual to physical addresses is done
by hardware device called the memory-management unit
(MMU).
This mapping technique is known as the paging technique.
MMU uses following mechanism to convert virtual address to
physical address.
14
MEMORY MANAGEMENT
15
MEMORY MANAGEMENT
Memory management Techniques:
The Memory management Techniques can be classified into
following main categories:
16
MEMORY MANAGEMENT
19
MEMORY MANAGEMENT
Advantages of Fixed Partitioning memory management schemes:
Simple to implement.
Easy to manage and design.
Disadvantages of Fixed Partitioning memory management schemes:
This scheme suffers from internal fragmentation.
The number of partitions is specified at the time of system
generation.
Dynamic Partitioning or variable size partition:
Dynamic partitioning tries to overcome the problems caused by
fixed partitioning.
In this technique, the partition size is not declared initially. The
size and number of partitions are decided during the runtime by
the operating system.
Each process occupies only as much memory as they required.
Partitions are created dynamically to meet the requirements of
each requesting process. Each process occupies only as much
memory as they required.
When a process terminates or is swapped-out, the memory 20
manager can return the vacated space to the pool of free memory
area and can be provided to another process.
MEMORY MANAGEMENT
25
MEMORY MANAGEMENT
Problem-01:
Consider six memory partitions of size 200 KB, 400 KB, 600 KB,
500 KB, 300 KB and 250 KB. These partitions need to be
allocated to four processes of sizes 357 KB, 210 KB, 468 KB and
491 KB in that order.Perform the allocation of processes using-
First Fit Algorithm, Best Fit Algorithm and Worst Fit Algorithm.
Solution:
The main memory has been divided into fixed size partitions as:-
28
MEMORY MANAGEMENT
29
MEMORY MANAGEMENT
30
MEMORY MANAGEMENT
Dealing with fragmentations:
External fragmentation can be removed by compaction
External fragmentation can be prevented by mechanisms such as
segmentation and paging.
Internal fragmentation can be minimized by having partitions of
several sizes and assigning a program based on the best fit.
However, still internal fragmentation is not fully eliminated.
Coalescing:
When a process is dispatched from process queue to store in
memory, OS first searches for the Hole large enough to
accommodate that process.
If none of the available holes are large enough to accommodate
that process then available adjacent free partitions can be merged
together to form a big hole (bigger space) to accommodate that
process.
The process of merging two adjacent holes to form a single larger
hole is called coalescing.
31
MEMORY MANAGEMENT
The process of merging two adjacent holes to form a single larger
hole is called coalescing.
Compaction:
Even when holes are coalesced, no individual hole may be large
enough to hold the job, although the sum of holes is larger than
the storage required for a process.
32
MEMORY MANAGEMENT
It is possible to combine all the holes into one big by moving all
the processes downward as far as possible; this technique is called
memory compaction.
Thus, Compaction is a technique to collect all the free memory
present in the form of fragments into one large chunk of free
memory, which can be used to load other processes.
33
MEMORY MANAGEMENT
Advantages of compaction:
Reduces external fragmentation.
Make memory usage efficient.
Memory becomes contiguous.
Drawback of compaction:
System must stop everything during compaction.
Reallocation info must be maintained.
System efficiency reduces and latency is increased.
Overlay: Overlaying allows various modules to be assigned
the same region of memory with a main program
responsible for switching the modules in and out as needed.
34
MEMORY MANAGEMENT
35
MEMORY MANAGEMENT
Page table:
A page table is the data structure used by a virtual memory
system in a computer operating system to store the mapping
between virtual address and physical address i.e. it is used to
map virtual page number to physical page number.
A page table entry contains several information about the page.
The information contained in the page table entry varies from
operating system to operating system.
The most important information in a page table entry is frame
number.
Size of page table = Number of entries in page table x Page table
entry size
Number of pages the process is divided = Process size / Page size
The information contained in the page table entry varies from
operating system to operating system.
37
MEMORY MANAGEMENT
Frame Number-
Frame number specifies the frame where the page is stored in the
main memory.
The number of bits in frame number depends on the number of
frames in the main memory.
Present / Absent Bit-
This bit is also sometimes called as valid / invalid bit.
This bit specifies whether that page is present in the main
38
memory or not.
MEMORY MANAGEMENT
If the page is not present in the main memory, then this bit is set
to 0 otherwise set to 1.
If the required page is not present in the main memory, then it is
called as Page Fault.
A page fault requires page initialization.
The required page has to be initialized (fetched) from the
secondary memory and brought into the main memory.
Protection Bit-
This bit is also sometimes called as “Read / Write bit“.
This bit is concerned with the page protection.
It specifies the permission to perform read and write operation on
the page.
If only read operation is allowed to be performed and no writing is
allowed, then this bit is set to 0.
If both read and write operation is allowed to be performed, then
this bit is set to 1.
39
MEMORY MANAGEMENT
If the page is not present in the main memory, then this bit is set
to 0 otherwise set to 1.
If the required page is not present in the main memory, then it is
called as Page Fault.
A page to be performed, then this bit is set to 1.
Reference Bit-
Reference bit specifies whether that page has been referenced in
the last clock cycle or not.
If the page has been referenced recently, then this bit is set to 1
otherwise set to 0.
Reference bit is useful for page replacement policy.
A page that has not been referenced recently is considered a good
candidate for page replacement in LRU page replacement policy.
Caching Enabled / Disabled:-
This bit enables or disables the caching of page.
Whenever freshness in the data is required, then caching is
disabled using this bit. 40
If caching of the page is disabled, then this bit is set to 1
otherwise set to 0.
MEMORY MANAGEMENT
Dirty Bit:
This bit is also sometimes called as “Modified bit“.
This bit specifies whether that page has been modified or not.
If the page has been modified, then this bit is set to 1 otherwise
set to 0.
Useful formulas for solving numerical:
For Main Memory:
Physical Address Space = Size of main memory
Size of main memory = Total number of frames x Page size
Frame size = Page size
If number of frames in main memory = 2X, then number of bits in
frame number = X bits
If Page size = 2X Bytes, then number of bits in page offset = X bits
If size of main memory = 2X Bytes, then number of bits in physical
address = X bits
41
MEMORY MANAGEMENT
For Process:
Virtual Address Space = Size of process
Number of pages the process is divided = Process size / Page size
If process size = 2X bytes, then number of bits in virtual address
space = x bits
For Page Table:
Size of page table = Number of entries in page table x Page table
entry size
Number of entries in pages table = Number of pages the process is
divided
Page table entry size = Number of bits in frame number +
Number of bits used for optional fields if any
42
MEMORY MANAGEMENT
= 4 MB
MEMORY MANAGEMENT
Problem 02:
Calculate the number of bits required in the address for
memory having size of 16 GB. Assume the memory is 4-
byte addressable.
Soln:
Given
Let ‘n’ number of bits are required.
Then, Size of memory = 2n x 4 bytes.
Since, the given memory has size of 16 GB,
So, we have,
2n x 4 bytes = 16 GB
2n x 4 = 16 G
2n x 22 = 234
2n = 232
∴ n = 32 bits 44
MEMORY MANAGEMENT
Problem 02:
Calculate the size of memory if its address consists of 22 bits
and the memory is 2-byte addressable.
Soln: Given
= 222 x 2 bytes
= 223 bytes
= 8 MB
Problem 04:
Consider a system with byte-addressable memory, 32 bit
logical addresses, 4 kilobyte page size and page table
entries of 4 bytes each. Calculate the size of the page table
in the system in megabytes.
45
MEMORY MANAGEMENT
= 4 GB / 4 KB 46
= 220 pages
MEMORY MANAGEMENT
47
MEMORY MANAGEMENT
Page size = 4 KB
= 64 MB
= 226 B
= 226 B / 212 B
48
= 214
49
MEMORY MANAGEMENT
50
MEMORY MANAGEMENT
51
Fig Paging hardware
MEMORY MANAGEMENT
Explanation:
Consider, the size of logical memory =16 Byte, size of physical
memory = 32 Byte and page size=4 Byte
Then, size of pages= 4 Bytes = 2 bits
No. of page= 4= 2 bits
The total bits in logical memory address=4 bits.
size of frames= 4 Bytes = 2 bits
No. of frames= 8= 3 bits
The total bits in physical memory address=5 bits.
Suppose the logical address generated by CPU is 1000, that is,
second page of first byte.
Since the page second contains the frame no. 3, so the binary form of
3 is 011 is combined with page offset oo which gives the physical
address 01100 . (in hexadecimal 0x0c).
53
MEMORY MANAGEMENT
55
MEMORY MANAGEMENT
Unlike page table, there exists only one TLB in the system.
So, whenever context switching occurs, the entire content of TLB is
flushed and deleted.
TLB is then again updated with the currently running process.
In a single level paging using TLB, the effective access time is given as:
56
MEMORY MANAGEMENT
Demand paging:
Demand paging is a technique used in virtual memory systems
where pages are brought in main memory only when requested or
demanded by the CPU.
With demand-paged virtual memory, pages are only loaded when
they are demanded during program execution.
That is, pages that are never accessed are thus never loaded into
physical memory
Instead of swapping the entire process into main memory, the lazy
swapper is used.
A lazy swapper brings only the necessary pages into memory. It is
used in the operating system to improve memory usage and system
performance.
Swapping:
Each and every process must be in main memory for execution.
However, a process can be transferred temporarily out of memory
to a backing storage device for certain time period and again
brought back it into main memory for continued execution. 60
Page Replacement:
Page replacement is a process of swapping out an existing page 61
from the frame of a main memory and replacing it with the
required page.
MEMORY MANAGEMENT
Page replacement is required when:
All the frames of main memory are already occupied.
Thus, a page has to be replaced to create a room for the required
page.
Page Replacement Algorithms:
Page replacement algorithms help to decide which page must be
swapped out from the main memory to create a room for the
incoming page.
A good page replacement algorithm is one that minimizes the
number of page faults.
Various page replacement algorithms are:
FIFO Page Replacement Algorithm
LRU Page Replacement Algorithm
Optimal Page Replacement Algorithm
62
MEMORY MANAGEMENT
FIFO Page Replacement Algorithm:
As the name suggests, this algorithm works on the principle of
“First in First out“.
It replaces the oldest page that has been present in the main
memory for the longest period of time.
It is implemented by keeping track of all the pages in a queue.
LRU Page Replacement Algorithm:
As the name suggests, this algorithm works on the principle of
“Least Recently Used“.
It replaces the page that has not been referred by the CPU for the
longest time.
Optimal Page Replacement Algorithm:
This algorithm replaces the page that will not be referred by the CPU in
future for the longest time.
It is practically impossible to implement this algorithm.
This is because the pages that will not be used in future for the longest time
cannot be predicted.
However, it is the best known algorithm and gives the least number of page 63
faults.
Hence, it is used as a performance measure criterion for other algorithms.
MEMORY MANAGEMENT
Problem-01:
A system uses 3 page frames for storing process pages in main
memory. It uses the First in First out (FIFO) page replacement
policy. Assume that all the page frames are initially empty. What is
the total number of page faults that will occur while processing the
page reference string given below:
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Soln:
Total number of references = 10
64
MEMORY MANAGEMENT
Calculating Hit ratio:
Total number of page hits
= Total number of references – Total number of page misses or
page faults
= 10 – 6
=4
Thus, Hit ratio
= Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%
Calculating Miss ratio:
Total number of page misses or page faults = 6
Thus, Miss ratio
= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%
65
Alternatively,
Miss ratio = 1 – Hit ratio= 1 – 0.4= 0.6 or 60%
MEMORY MANAGEMENT
Problem-02:
A system uses 3 page frames for storing process pages in main
memory. It uses the Least Recently Used (LRU) page replacement
policy. Assume that all the page frames are initially empty. What is
the total number of page faults that will occur while processing the
page reference string given below: 4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Soln:
Total number of references = 10
68
MEMORY MANAGEMENT
Segmentation:
Segmentation is a non contiguous memory management technique
in which the program is divided into the variable size parts. Each
part is known as a segment which can be allocated to a process.
The details about each segment are stored in a table called a
segment table. Segment table is stored in one (or many) of the
segments.
Segmentation supports user view of memory
A segment is a logical unit such as main program, procedure,
function, stacks, common blocks etc.
Segment table contains mainly two information about segment:
Base: It is the base address of the segment
Limit: It is the length of the segment. Where segment base
contains starting physical address where the segment resides in
memory, where as segment specifies the length of the segment.
69
MEMORY MANAGEMENT
Hardware support:
CPU generates logical address consisting of segment number and
offset
Use the segment number as index to segment table.
The offset should be between zero and limit value of that
particular segment(else it trap)
Then, segment offset is added with the base address of the
segment. 70
The result obtained after addition is the address of the memory
location storing the required word.
MEMORY MANAGEMENT
Hardware support:
CPU generates logical address consisting of segment number and
offset
Use the segment number as index to segment table.
The offset should be between zero and limit value of that
particular segment(else it trap)
Then, segment offset is added with the base address of the
segment.
The result obtained after addition is the address of the memory
location storing the required word.
71
MEMORY MANAGEMENT
Example conversion of logical address in to physical address:
72
MEMORY MANAGEMENT
Advantages of segmentation:
No internal fragmentation
Average Segment Size is larger than the actual page size.
Less overhead
It is easier to relocate segments than entire address space.
The segment table is of lesser size as compared to the page table in
paging.
Disadvantages of segmentation:
it can have external fragmentation.
it is difficult to allocate contiguous memory to variable sized
partition.
Costly memory management algorithms.
73
MEMORY MANAGEMENT
Difference between paging and segmentation:
Paging Segmentation
A page is of the fixed block size. A segment is of variable size.
It may lead to internal fragmentation. It may lead to external fragmentation.
In Paging, the hardware decides the page size. The segment size is specified by the user.
A process address space is broken into fixed-sized A process address space Is broken in
blocks, which is called pages. differing sized blocks called sections.
The paging technique is faster for memory access. Segmentation is slower than paging
method.
Page table stores the page data Segmentation table stores the segmentation
data.
Paging does not facilitate any sharing of Segmentation allows for the sharing of
procedures. procedures.
Paging fails to distinguish and secure procedures Segmentation can be able to separate
and data separately. secure procedures and data.
Paging address space is one dimensional In segmentation, there is the availability of
many independent address spaces
In paging, the user just provides a single integer as In the segmentation method, the user
the address, that is divided by the hardware into a specifies the address in two quantities
page number and offset. 1)segment number 2)offset.
74
MEMORY MANAGEMENT
75
MEMORY MANAGEMENT
76
MEMORY MANAGEMENT
How to get address?
The segment number indexes into the segment table which yields
the base address of the page table for that segment.
Check the remainder of the address (page number and offset)
against the limit of the segment.
Use the page number to index the page table. The entry is the
frame. (The rest of this is just like paging.)
Concatenate the frame and the offset to get the physical address.
77
MEMORY MANAGEMENT
Advantages
Reduces memory usage as opposed to pure paging
Page table size limited by segment size
Segment table has only one entry per actual segment
Share individual pages by copying page table entries.
Share whole segments by sharing segment table entries, which
is the same as sharing the page table for that segment.
Most advantages of paging still hold
Simplifies memory allocation
78
MEMORY MANAGEMENT
Allocation of frame:
Frame allocation algorithms are used if we have multiple
processes; it help to decide how many frames to allocate to each
process.
There two methods for allocation of frame using algorithms are as:
Equal allocation and proportional allocation.
Equal allocation: In a system with x frames and y processes, each
process gets equal number of frames, i.e. x/y. for example, if the
system has 48 frames and 9 processes, each process will get 5
frames. The three frames which are not allocated to any process
can be used as a free-frame buffer pool.
To remove the disadvantage of equal allocation we use proportional
allocation. The available memory is allocated to each process
according to its size.
79
MEMORY MANAGEMENT
Proportional allocation:
Frames are allocated to each process according to the process size.
For a process pi of size si, the number of allocated frames is
ai = (si/S)*m, where S is the sum of the sizes of all the processes
and m is the number of frames in the system.
For examle, in a system with 62 frames, if there is a process of
10KB and another process of 127KB, then the first process will be
allocated (10/137)*62 = 4 frames and the other process will get
(127/137)*62 = 57 frames.
80
MEMORY MANAGEMENT
Resident monitor:
Bare machine is logical hardware which is used to execute the
program in the processor without using the operating system.
Initially, when the operating systems are not developed, the
execution of an instruction is done by directly on hardware without
using any interfering hardware.
Bare machines accept the instruction only in machine language.
The Resident Monitor is a code that runs on Bare Machines. The
resident monitor works like an operating system that controls the
instructions and performs all necessary functions.
It also works like job sequencer because it also sequences the job
and sends them to the processor.
After scheduling the job Resident monitors loads the programs one
by one into the main memory according to their sequences.
The Resident monitors are divided into 4 parts as:
Control Language Interpreter
Loader
Device Driver 81
Interrupt Processing
MEMORY MANAGEMENT
Control Language Interpreter: The first part of the Resident monitor is
control language interpreter which is used to read and carry out the instruction
from one level to the next level.
Loader: The second part of the Resident monitor which is the main part of the
Resident Monitor is Loader which Loads all the necessary system and
application programs into the main memory.
Device Driver: The third part of the Resident monitor is Device Driver which is
used to manage the connecting input-output devices to the system. So basically
it is the interface between the user and the system. it works as an interface
between the request and response. Request which user made, Device driver
responds that the system produces to fulfill these requests.
Interrupt Processing: The fourth part as the name suggests, it processes the
all occurred interrupt to the system.
82
MEMORY MANAGEMENT
Managing Free Memory Space:
Free space management refers to the process of tracking and
managing unallocated disk space within a file system.
For this, the system maintain a free space list which keeps track of
whether free disk blocks re not allocated to some file (or directory)
or not.
When a file is created, the operating system searches the free space
list for the required space allocated to save a file.
While deletion a file, the file system frees the allocated space and
adds this to the free space list.
It minimizes fragmentation and maximizing storage utilization.
The free space list can be implemented mainly as:
There are four free space management methods available in OS.
These are - Bit vector, linked list, grouping, and counting.
Bit vector or Bitmap:
Bitmaps are arrays of bits where each bit represents a block on the
disk. A bit value of 0 indicates that the block is allocated and 0
indicates that the block is free.
83
MEMORY MANAGEMENT
It is a most frequently used method to implement the free space
list.
For example, consider a diagrammatic representation of a disk in
which there are 16 blocks. Free blocks are represented by 1 and
occupied blocks are represented by 0.
Advantages:
Easy to implement.
Efficient in terms of space, as each block is represented by a single
bit.
84
MEMORY MANAGEMENT
Disadvantages:
Can become inefficient for very large disks, as the size of the
bitmap grows with the number of blocks.
1 Tb disk with 4Kb block size requires 32 Mb to store bit map
Linked list:
Each free block contains a pointer to the next free block, forming a
chain.
A linked list is another method for free space management in an
operating system.
In this, all the free blocks inside a disk are linked together in a
linked list by a pointer.
The pointers of the free block contain the address of the next free
block and the last pointer of the list points to null which indicates
the end of the linked list.
The free space list head points to the first node in the list.
85
MEMORY MANAGEMENT
Each entry in the list specifies a process (P) or a hole (H), the address
at which it starts, the length and a pointer to the next entry.
Advantages:
Simple to implement.
Can handle any disk size without significantly increasing overhead.
Disadvantages:
Traversing the linked list to find a free block can be slow.
86
Requires extra space to store pointers, reducing the amount of usable
disk space.
MEMORY MANAGEMENT
Importance of free space management.
Efficient Disk Utilization:
Proper free space management ensures that disk spaces are
utilized efficiently, reducing wasted space and preventing the disk
from becoming prematurely full.
Improved System Performance:
Efficient allocation and de-allocation of disk space reduce the time
required for these operations, leading to improved system
performance
System Reliability:
Effective free space management helps prevent file system
corruption and data loss.
Ease of Expansion:
Storage needs grow, efficient free space management allows for
easier expansion and better scalability.
87
MEMORY MANAGEMENT
Thrashing:
Thrashing is a phenomenon that occurs in operating system when
the system spent an excessive amount of time in swapping data
between main memory (RAM) and virtual memory (disk) due to
high demand of resources and low available resources.
Consider a process that currently needs some more frames.
Currently all the frames are occupied.
So, it will have to swap with certain pages; however, since all the
pages are actively being used, there would be need for an
immediate page replacement after this.
Such a scenario is called thrashing. It results in serious
performance problems.
88
MEMORY MANAGEMENT
Thrashing is often caused:
When the system does not have enough memory.
the system swap file is not properly configured, or
too much is running on the computer and it has low system
resources.
To resolve hard drive thrashing, a user can do any of the
below.
Increase the amount of RAM in the computer.
Decrease the amount of programs being run on the computer.
Adjust the size of the swap file.
Disadvantages
The page fault rate increases tremendously.
Effective memory access time increases.
No work is being done.
Average service time is also increased.
89
File system
File Types: File type refers to the ability of the operating system to distinguish different
types of file such as text files, source files, ojects file and binary files etc.
• Regular files contain user information and data bytes.
Regular files may be ASC II/ text file or binary file.
• ASCII files consist of lines of text, each line terminated by carriage return or line
feed. Each line may have variable length. These files are easy to edit and display.
• Source file –sequence of function, subroutine and it has extension such as (c,py,java,
cpp etc)
• Object file- sequence of bytes organized in to blocks and it has extension .obj
• Binary files- A binary file is a file whose content is in a binary format
consisting of a series of sequential bytes, each of which is eight bits in length.
1
• Executables file- it is a series of encoded code that system executes directly.
The file name is split into two parts-a name and an extension, the system uses the extension
to indicate the type of the file and the type of operations that can be done on that file.
The different categories of files extension are as follows:
3
• Recovery and Fault Tolerance: The file system implementation includes features for
recovering from system failures and maintaining data integrity
Different types of file systems:
There are several types of file systems, each which is designed for specific purposes and
compatible with other operating systems. Some common file system types are:
There are several types of file systems, each designed for specific purposes and compatible
with different operating systems. Some common file system types include:
• FAT32 (File Allocation Table 32): Commonly used in older versions of Windows
and compatible with various operating systems.
• NTFS (New Technology File System): Used in modern windows operating systems,
offering improved performance, reliability, and security features.
• ext4 (Fourth Extended File System): Used in Linux distributions, providing features
such as journaling, large file support, and extended file attributes.
• HFS+ (Hierarchical File System Plus): Used in macOS systems prior to macOS
High Sierra, offering support for journaling and case-insensitive file names.
• APFS (Apple File System): Introduced in macOS High Sierra and the default file
system for macOS and iOS devices, featuring enhanced performance, security, and
snapshot capabilities.
• ZFS (Zettabyte File System): A high-performance file system known for its
advanced features, including data integrity, volume management, and efficient
snapshots.
Contiguous allocation:
If the blocks are allocated to the file in such a way that all the logical blocks of the file get the
contiguous physical block in the hard disk then such allocation scheme is known as
contiguous allocation. The given starting block address and the length of the file (in terms
of blocks required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation contains
• Address of starting block
• Length of the allocated portion.
4
Advantages:
1. It is simple to implement.
2. We will get Excellent read performance.
Disadvantages:
1. The disk will become fragmented.(External fragmentation)
2. It may be difficult to have a file grow.
Advantages:
• There is no external fragmentation with linked allocation.
5
• Any free block can be utilized in order to satisfy the file block requests.
• File can continue to grow as long as the free blocks are available.
• Directory entry will only contain the starting block address.
Disadvantages:
• Random Access is not provided.
• Pointers require some space in the disk blocks.
• Any of the pointers in the linked list must not be broken otherwise the file will get
corrupted.
• Need to traverse each block.
Advantages:
• This supports direct access to the blocks occupied by the file and therefore provides
fast access to the file blocks.
• It overcomes the problem of external fragmentation.
Disadvantages:
• The pointer overhead for indexed allocation is greater than linked allocation.
• Multiple index even file is small.
6
Layers structure of file system:
Most of the Operating Systems use layering approach for every task including file systems.
Every layer of the file system is responsible for some activities. The fig shown below,
elaborates how the file system is divided in different layers, and also the functionality of each
layer.
• Application Programs: This is the topmost layer where users interact with files
through applications. It provides the user interface for file operations like creating,
deleting, reading, writing, and modifying files. Examples include text editors, file
browsers, and command-line interfaces.
• Logical File system – It contains the meta data about the file and directory structure.
It also maintains file control block (FCB) which has information about a file – owner,
size, permissions, and location of file contents.
• File Organization Module – It has information about files, the location of files and
their logical and physical blocks. In order to store and retrieve the files, the logical
blocks need to be mapped to physical blocks. This mapping is done by file
organization module. It is also responsible for free space management. Once file
organization module determine which physical block the application program needs,
it passes this information to basic file system.
• Basic File system – The basic file system is responsible for issuing the commands to
I/O control in order to fetch the required blocks.
• I/O Control – I/O controls contain the codes by using which it can access hard disk.
These codes are known as device drivers. Device driver act as an interface between
devices and OS, they help to transfer data between disk and main memory.
• Devices Layer- The bottommost layer, consisting of the actual hardware devices. It
performs the actual reading and writing of data to the physical storage medium.
7
Directory:
The directory is data structure that contains information about the files, including
attributes, location and ownership. That is, directory is a container that is used to contain
folders and files. The operating system uses directories to keep track of files and where they
are stored. Directory can be viewed as a symbol table that translates file name into
directory entries. A directory contains a number of entries, one per file. Directory can be
organized in many ways. The number of directories varies from system to system.
The operations allowed for managing directories are: -
a) CREATE
b) DELETE
c) OPENDIR
d) CLOSEDIR
e) READIR
f) RENAME
g) LINK
h) UNLINK
File path:
The way by which path name for each file is specified is called file path. A path is a unique
location to a file or a folder in a file system of an OS. A path to a file is a combination of /
and alpha-numeric characters.
An absolute path is defined as specifying the location of a file or directory from the
root directory (/). For example, /user/ ast/ mailbox
Relative path name is the path for the file beginning from the current working directory.
For example, if current directory is /user/ast, then relative path can be mailbox.
Directory structure (logical structure of directory): There are different types of directory
structures that help organize and manage files efficiently. These are:
• Single-Level Directory
• Two-Level Directory
• Tree Structure/ Hierarchical Structure
• Acyclic Graph Structure
Single-Level Directory:
The single-level directory is the simplest directory structure in which all files are contained
in the same directory. Each file must have unique name.
8
Fig. single level directory
Advantages:
• Implementation is very easy.
• If the files are smaller in size, searching will become faster.
• The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.
Disadvantages:
• there may be chance of name collision because two files can have the same name.
• Searching will become time taking if the directory is large.
• This cannot group the same type of files together.
Two-Level Directory:
Each user has their own user files directory (UFD). The UFDs have similar structures, but
each lists only the files of a single user.
9
Advantages
• This directory structure allows subdirectories inside a directory.
• The searching is easier.
• File sorting of important and unimportant becomes easier.
• This directory is more scalable than the other two directory structures explained.
Disadvantages
• As the user isn’t allowed to access other user’s directory, this prevents the file sharing
among users.
• If the number of subdirectories increases the searching may become complicated.
• Users cannot modify the root directory data.
10
Fig. Acyclic graph directory.
Advantages
• Sharing of files and directories is allowed between multiple users.
• Searching becomes too easy.
• Flexibility is increased as file sharing and editing access is there for multiple users.
Disadvantages
• It is difficult to implement this directory structure than other.
• The user must be careful to edit or even deletion of file as the file is accessed by
multiple users.
• If we need to delete the file, then we need to delete all the references of the file in
order to delete it permanently.
11
File System Layout:
File system are stored on disks. The disks are separated into partitions. Master boot record is
the information present in the first sector of any hard disk and mainly used for booting the
computer. It contains the information regarding how and where the operating system is
located in the hard disk so that it can be booted in the RAM. It also has a table included in its
partition. This table has the starting and ending address of each partition. When system is
booted, the BIOS (is the program that is used to start the computer when it is powered on)
reads and execute MBR. The MBR program locates in the active partition read its first block,
which is the boot block and executes it. As boot block executed, the CPU fetches all the
information and load the operating system into the main memory. The superblock contains
key parameters about a file system. It includes information about size, block size, empty and
filled blocks. If you are unable to mount your devices, this can be due to corrupted
superblock. Next to superblock, we have a block which includes details of available free
block. Then, we have the I-nodes an array of data structure associated with each file. After
that, there is a root directory, servicing the apex of the file system hierarchy. Finally, the rest
of the disk accommodates the remaining directories and files within the file system.
12
• Eg: consider a disk with 32768 bytes per track, rotation time of 16.67 ms and average
seek time of 30ms.
• For block size of K bytes’ time to read a block is:
30+16.67+(K/32768) * 16.67
I – nodes:
An I-node (index node) is a data structure in UNIX operating system that contains important
information pertaining to files within a file system. In UNIX based operating system, each
file is indexed by an I-node. I-node is special disk block that are created when the file system
is created. The number of files or directories in a file system depends on the number of I-
nodes in the file system. Each file is associated with a table called index-nodes (I-node)
which lists the attributes and disk addresses of the file’s blocks.
• Attributes (permissions, time stamp, ownership details, etc) of the file such as
o Numeric UID of the owner.
o Numeric GUID of the owner.
o Size of the file.
o File type: regular,directory,device etc…
o Date and Time of Last modification of the file data.
o Date and Time of Last access of file data.
o Date and Time of Last change of the I-node.
• A number of direct blocks (typically 12) that contains the first 12 blocks of the files.
13
• A single indirect pointer that points to a disk block which in turn is used as an index
block, if the file is too big to be indexed entirely by the direct blocks.
• A double indirect pointer that points to a disk block which is a collection of pointers
to disk blocks which are index blocks, used if the file is too big to beindexed by the
direct and single indirect blocks.
• A triple indirect pointer that points to an index block of index blocks of index blocks.
Numerical:
Example1:
The index node (inode) of a Unix-like file system has 12 direct, one single-indirect and one
double-indirect pointer. The disk block size is 4 kB and the disk block addresses 32-bits long.
Calculate the maximum possible file size (rounded off to 1 decimal place).
Solution:
Given, In index node of a Unix-like file system
Direct Block pointer = 12
14
Single Indirect Block pointer = 1
double Indirect Block pointer = 1
Disk block address = disk block entries size = 32 bit = 4 bytes
Disk Block size = 4 KB
Calculation:
Number of entries in a block= 4KB/4B=1K=210
File size = (12 direct + 1 single indirect block + 1 double indirect) × block size
File size = (12 × 1 + 1 × 210 + 1 × 210 x 210) × 4 KB
File size = 48 KB + 4 MB + 4 GB
File size = 4.003952 GB ≈ 4 GB
Example1:
A file system with 300 GByte disk uses a file descriptor with 8 direct block addresses, 1
indirect block address and 1 doubly indirect block address. The size of each disk block is 128
Bytes and the size of each disk block address is 8 Bytes. Calculate the maximum possible file
size (rounded off to 1 decimal place). [Ans= 35 KB] (8 × 1 + 1 × 24 + 1 × 24 x 24) × 128
Bytes)
16
v-node points to the local disk i-node. If the file is remote, the v-node contains the address of
the remote NFS server.
Pipes
UNIX allows you to link commands together using a pipe. The pipe acts a temporary file
which only exists to hold data from one command until it is read by another. A Unix pipe
provides a one-way flow of data. The output or result of the first command sequence is used
as the input to the second command sequence. To make a pipe, put a vertical bar (|) on the
command line between two commands. For example: who | wc -l In long-format output of ls
–l , named pipes are marked by the “p” symbol.
17
Input/output interface and disk scheduling
➢ Device independence
• It should be possible to write programs that can access any I/O device without
having to specify the device in advance.
• For example, a program that reads a file as input should be able to read a file on a
hard disk, a CD-ROM, a DVD, or a USB without having to modify the program for
each different device.
➢ Uniform naming
• The name of the device should simply be a string or an integer and do not
depend on the device in any way.
➢ Error Handling
• In general, the error should be handled as close to the hardware as possible
• For example, if controller discovers read error, it should try to correct the error
itself. If it cannot, the device driver should handle it.
➢ Synchronous (blocking) Vs. Asynchronous (interrupt driven) transfers
• It is up to the OS to make the operation that are interrupt driven look blocking to the
user programs.
➢ Buffering
• Often, data that comes off a device cannot be stored directly to its final
destination due to various reasons.
• buffer is the intermediate destination.
➢ Dedicated Vs Shared devices
• Devices that can be used by many users at a time are sharable. E.g. Disk
(multiple users can open files from the same disk at the same time.
• Some devices have to be dedicated to a single person. E.g. tapes
Each of the above given four layer has a well-defined function to perform and a well-defined
interface to the adjacent layers. The actual hardware and hardware controller which interact
Input/output interface and disk scheduling
with the device drivers and makes hardware alive.The functionality and interfaces differ from
system to system.
Interrupt handlers:
An interrupt handler, also known as an interrupt service routine(ISR), is a piece of software
or more specifically a callback function in an operating system or more specifically in a
device driver, whose execution is triggered by the reception of an interrupt. The interrupt
procedure will perform whatever actions are necessary in order to deal with an interrupt
whenever one of those events takes place.
When the interrupt happens, the interrupt procedure does whatever it has to in order to handle
the interrupt, updates data structures and wakes up process that was waiting for an interrupt to
happen.
The interrupt mechanism accepts an address ─ a number that selects a specific interrupt
handling routine/function from a small set. In most architecture, this address is an offset
stored in a table called the interrupt vector table. This vector contains the memory addresses
of specialized interrupt handlers.
Device drivers:
Device drivers are software modules that can be plugged into an OS to handle a particular
device. It is the software part that communicates to the device controller, giving it commands
and accepting response. A device driver provides the software interface to hardware devices,
enabling the OS and other programs to access hardware functions without having to know the
precise details of the hardware being used. Device driver is generally written by the device's
manufacturer and delivered along with the device on a CD-ROM. Device divers are hardware
dependent and operating system specific. Operating system takes help from device drivers to
handle all I/O devices.
• Interact with the device controller to take and give I/O and perform required error
handling
• Making sure that the request is executed successfully
Device controller:
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. A device
controller is a part of a computer system that makes sense of the signals going to, and coming
from the CPU. 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.
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,
Input/output interface and disk scheduling
controllers, and I/O devices where CPU and device controllers all use a common bus for
communication as shown in fig. below:
Interrupt driven I/O is a method of transferring data between a processor and an I/O device. It
uses interrupt to signal when data is ready to be exchanged, freeing up the processor to
perform other tasks. The operation performed by interrupt driven I/O are:-
• An I/O device sends an interrupt signal to the processor when it’s ready to send or
received data.
• The processor suspends its current task and services the interrupt request.
• The processor executes the data transfer.
• The processor resumes its previous task.
Working of DMA:
• First the CPU programs the DMA controller by setting its registers so it knows what
to transfer where
• Alongside, DMAC issues a command to the disk controller telling it to read data from
the disk into its internal buffer and verify the checksum. When valid data are in the
disk controller's buffer, DMA can begin.
• The DMA controller initiates the transfer by issuing a read request over the bus to the
disk controller
• The write to memory is another standard bus cycle
• When the write is complete, the disk controller sends an acknowledgement signal to
the DMA controller, also over the bus
• The DMA controller then increments the memory address to use and decrements the
byte count. If the byte count is still greater than 0, steps 2 through 4 are repeated until
the count reaches 0. At that time, the DMA controller interrupts the CPU to let it
know that the transfer is now complete.
Advantage:
• Free up CPU
• high transfer rates
• fewer CPU cycles for each transfer
Disadvantages
• DMA transfer requires a DMA controller to carry out the operation, hence more
expensive system
• Synchronization mechanisms must be provided in order to avoid accessing non-
updated information from RAM
Disk scheduling:
Disk scheduling is done by operating systems to schedule I/O requests arriving for the
disk. Disk scheduling is also known as I/O scheduling.
• Multiple I/O requests may arrive by different processes and only one I/O request
can be served at a time by the disk controller. Thus other I/O requests need to wait in
the waiting queue and need to be scheduled.
Terms:
• Two or more request may be far from each other so can result in greater disk arm
movement.
• Hard drives are one of the slowest parts of the computer system and thus need to be
accessed in an efficient manner.
Following are the disk performance parameters:
• Seek Time: Seek time is the time taken to locate the disk arm to a specified track
where the data is to be read or write.
Seek time = Number of track traversed × Disk drive constant + Startup time
Seek time do not apply to device with fixed read/write heads.
So the disk scheduling algorithm that gives minimum average seek time is better.
• Rotational Latency or rotational delay: Rotational latency is the time taken to
locate the desired sector of disk to rotate into a position so that it can access the
read/write heads. So the disk scheduling algorithm that gives minimum rotational
latency is better.
• Transfer Time: Transfer time is the time to transfer the data. It depends on the
rotating speed of the disk and number of bytes to be transferred.
T=b/rN where T=data transfer time, b=no. of bytes to be transferred
N=no. of bytes on track, r=rotational speed per second
Thus, total average access time can be expressed as
Ta= Ts +1/2r +b/rN, where Ts= estimated seek time
Input/output interface and disk scheduling
• Disk Access Time: The sum of the seek time ,the rotational delay equals and transfer
time equals the access time
Disk Access Time = Seek Time +Rotational Latency + Transfer Time
Disk Response Time: Response Time is the average of time spent by a request waiting to
perform its I/O operation.
Disk capacity:
Disk capacity = Number of cylinders × Number of heads × Number of sectors per track ×
Number of bytes per track
Example1: Consider a disk with a sector size of 512 bytes, 2000 tracks per surface, 50
sectors per track, five double side platter, and average seek time of 10 msec.
(i) What is the capacity of a track in bytes? What is the capacity of each surface? What is
the capacity of disk?
(ii) How many cylinders does the disk have?
(iii) Give the example of valid block sizes. Is 256 bytes a valid block size?
(iv) if the disk platter rotateat 5400 rpm. What are the maximum rotational delay and average
rotational delay.
(v) If one track of data can be transferred per revolution, what is the transfer rate?
Solution:
(i) Capacity of a track in bytes= Number of sectors per track × size of per sector
= 50x512 bytes=25600 bytes= 25K
Capacity of each surface= no. of track x capacity of track=2000x25K=50000K
Capacity of disk=byte per surface x surface per disk=50000K x5x2=5000000K
(ii) The number cylinders is the same as the number of tracks on each platter, which is 2000.
(iii) The block size should be a multiple of the sector size. We can see that 256 is not valid
block size and valid size is 2048.
(iv) If disk platter rotate at 5400 rpm, then rotational delay, r =60/5400=1/90 sec =00.011 s
The average rotational delay, is half of the rotation time= 1/(2*90)=
(v) The capacity of a track is 25K bytes, since one track of data can be transferred per
revolution, the data transfer rate is 25K* 90= 2250 KB/sec
Example2: A certain moving arm disk storage device has the following:
No. of track per surface=404
Track storage capacity=13030 bytes
Disk speed=3600 rpm
Average seek time=30 msec
Find the average latency time, disk storage capacity and rate of data transfer.
Solution :
Given, no. of bytes on track (N)=13030 bytes
Ts=10x10-3 sec
Disk speed =3600 rpm
Time required for one rotation=1/3600 per minute=1/60 sec
Average latency time=1/2x60=1/120 seconds
Input/output interface and disk scheduling
Disk storage capacity= No. of track per surface x Track storage capacity
=404 X 13030 bytes
Data transfer rate= Track storage capacity/Time required for one rotation
=13030/(1/60)=60x13030 bytes/sec
Total data transfer time =Ts +1/2r +b/rN
= 10x10-3 + 1/(2*60) +(13030 bytes/(60*13030)) bytes/sec
=0.054967 sec
Example:
1. Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
Soln:
So, total seek time:
=(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642
Advantages:
Disadvantages:
Example:
1. Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
Example:
1. Suppose the requests to be addressed are- 82,170,43,140,24,16,190. And the
Read/Write arm is at 50, and it is also given that the disk arm should move “towards
the larger value”.
Input/output interface and disk scheduling
Advantages:
• High throughput
• Low variance of response time
• Average response time
Disadvantages:
Long waiting time for requests for locations just visited by disk arm
Example1:
Consider the following example:
The disk queue has the following request to read track 87,170,40,150,36,72,66,15.
If the disk head is initially 60. Find the total headmovement.
Example2:
Scan/ Elevator algorithm:
• The disk head constantly moves from the inner most cylinders (current position) to
the outer cylinder servicing the request in order and then it changes its direction back
toward.
• This algorithm is sometimes called as elevator algorithm because the disk arm
behaves just like an elevator of the building.
• First it serves all the requests of going up and then reaching at top, it goes downward.
Example:
Consider a disk queue with requests for I/O to blocks on cylinders 98, 183, 41, 122, 14, 124,
65, 67. The SCAN scheduling algorithm is used. The head is initially at cylinder number 53
moving towards larger cylinder numbers on its servicing pass. The cylinders are numbered
from 0 to 199. The total head movement (in number of cylinders) incurred while servicing
these requests is _______.
Solution-
Input/output interface and disk scheduling
Solution:
Advantages:
• The waiting time for the cylinders just visited by the head is reduced as compared to
the SCAN Algorithm.
• It provides uniform waiting time.
• It provides better response time.
Disadvantages:
• It causes more seek movements as compared to SCAN Algorithm.
• It causes the head to move till the end of the disk even if there are no requests to be
serviced.
LOOK Scheduling algorithm:
• LOOK Algorithm is an improved version of the SCAN Algorithm.
• Head starts from the first request at one end of the disk and moves towards the last
request at the other end servicing all the requests in between.
• After reaching the last request at the other end, head reverses its direction.
• It then returns to the first request at the starting end servicing all the requests in
between.
• The same process repeats
Example:
Consider a disk queue with requests for I/O to blocks on cylinders 98, 183, 41, 122, 14, 124,
65, 67. The LOOK scheduling algorithm is used. The head is initially at cylinder number 53
moving towards larger cylinder numbers on its servicing pass. The cylinders are numbered
from 0 to 199. What is the total head movement (in number of cylinders) incurred while
servicing these requests.
Solution-
Advantages-
• It does not cause the head to move till the ends of the disk when there are no requests
to be serviced.
• It provides better performance as compared to SCAN Algorithm.
• It does not lead to starvation.
Input/output interface and disk scheduling
Disadvantages-
Disks:
• Disks are the information storage media.
• Advantages of disks over main memory:
o storage capacity of disk is greater than main memory
o disks are non-volatile (i.e. unlike main memory, the information on
disk is not lost even after power off)
• Disks come in variety of types:
▪ Magnetic Disks : Hard Disks, Floppy disks
▪ Optical Disks, CD-ROMs, DVDs etc.
RAM Disk:
RAM Disk also known as RAM Drive is not a disk at all;
A RAM disk (also called a RAM drive) is a block of random-access memory (primary
storage or volatile memory) that a computer's software is treating as if the memory were a
disk drive (secondary storage).
It is a program which takes the portion of the system memory (RAM, in the form of memory
chips) and uses it as a disk drive. You can create a larger RAM disk, if your computer has
more RAM. A RAM disk can also take the form of a hardware device or a virtual disk. RAM
drive makes use of regular RAM instead of accessing the data bus. As long as the power is
on, you can use a RAM disk just like a real disk drive-you can copy files to and from the
RAM disk, display the RAM disk's directory or folder on your screen, and run any programs
you've stored there.
applications or those files from there if the program allows that. Mostly, RAM disks are
suitable for those
• who want to store temporary data.
• You can also set the Windows temp folder location there for instance.
Interleaving:
Interleaving is the gap between the two continuous serial sectors. Interleaving is a data
storage optimization technique employed in hard disks by putting data accessed sequentially
into non-sequential sectors. Let the size of one sector is 512 bytes. Let the total number of
sector 17 per track. Therefore, in one round of r/w head, the total amount of data transferred=
512*17= 8707 bytes.
But the controller cannot recognize 8704 bytes in one round. Therefore, a method in which
lesser number of sector can be transverse for r/w operation is known as sector interleaving..
No interleaving (factor 1:1) single interleaving (factor 1:2) double interleaving (factor 1:2)
Traversing fewer sectors can be made possible only when we leave sectors and not r/w all
sectors serially.
Benefit of sector leaving factor:
Using sector leaving factor, we can manage the capacity of controller. Let the capacity of
controller=2K but if interleaving factor is 1:1 then
If size of sector is 512 bytes and total sector per track is 17 then,
total data transferred per revolution=512*17= 8704 bytes
i.e. 8704/1024=8.5K
but controller can only support 2K per revolution.
Hence, we set interleaving factor as 1:4 or 1:5 to resolve this problem.
Terminal:
➢ Terminals are data input-output device usually made up of a monitor (display),
keyboard, mouse, or touch screen.
Input/output interface and disk scheduling
RAID:
RAID redundant array of inexpensive disks or redundant array of independent disks is
a data storage virtualization technology that combines multiple physical data storage
components into one or more logical units for the purposes of data redundancy, performance
improvement, or both. In case of hardware RAID, there is a dedicated controller and processor
present inside the disks that manage the memory. There are various raid levels as discussed below.
Standard RAID levels RAID 0:
RAID 0 is based on data striping. A stream of data is divided into multiple segments or
blocks and each of those blocks is stored on different disks. So, when the system wants to
read that data, it can do so simultaneously from all the disks and join them together to
Input/output interface and disk scheduling
reconstruct the entire data stream. The benefit of this is that the speed increases drastically for
read and write operations.
Advantages:
• Performance boost for read and write operations.
• Space is not wasted as the entire volume of the individual disks are used up to store unique
data.
Disadvantages
• There is no redundancy/duplication of data. If one of the disks fails, the entire data is lost.
RAID 1:
RAID 1 uses the concept of data mirroring. Data is mirrored or cloned to an identical set of
disks so that if one of the disks fails, the other one can be used. It also improves read
performance since different blocks of data can be accessed from all the disks simultaneously.
Input/output interface and disk scheduling
Advantages:
• Data can be recovered in case of disk failure
• Increased performance for read operation
Disadvantages
• Slow write performance
• Space is wasted by duplicating data which increases the cost per unit memory
RAID 4:
RAID 4 stripes the data across multiple disks just like RAID 0. In addition to that, it also
stores parity information of all the disks in a separate dedicated disk to achieve redundancy.
In the diagram below, Disk 4 serves as the parity disk having parity blocks Ap, Bp, Cp and
Dp. So, if one of the disks fails, the data can be reconstructed using the parity information of
that disk. Space is more efficiently used here when compared to RAID 1 since parity
information uses way less space than mirroring the disk.
Advantages
• Efficient data redundancy in terms of cost per unit memory
• Performance boost for read operations due to data stripping
Disadvantages
• Write operation is slow because all the parity information is written on a single disk
which is a bottleneck.
• If the dedicated parity disk fails, data redundancy is lost
RAID 5:
RAID 5 is the most common secure RAID level. It requires at least 3 drives but can work
with up to 16. Data blocks are striped across the drives and on one drive a parity checksum of
all the block data is written. The parity data are not written to a fixed drive, they are spread
across all drives, as the drawing below shows. Using the parity data, the computer can
recalculate the data of one of the other data blocks, should those data no longer be available.
That means a RAID 5 array can withstand a single drive failure without losing data or access
to data.
Input/output interface and disk scheduling
Advantages:
• Read data transactions are very fast while write data transactions are somewhat slower
(due to the parity that has to be calculated).
Disadvantages
• Can only handle up to a single disk failure
RAID 6:
RAID 6 uses double parity blocks to achieve better data redundancy than RAID 5. This
increases the fault tolerance for up to two drive failures in the array. Each disk has two parity
blocks which are stored on different disks across the array. RAID 6 is a very practical
infrastructure for maintaining high availability systems.
Advantages:
• Better data redundancy. Can handle up to 2 failed drives
Input/output interface and disk scheduling
Disadvantages
• Large parity overhead
Advantages:
• Very fast performance
• Redundancy and fault tolerance
Disadvantages:
• Cost per unit memory is high since data is mirrored
Deadlock:
Deadlock is a situation where a set of processes are blocked
because each process is holding a resource and waiting for
another resource acquired by some other process.
Or two or more processes are said to be in a deadlock if
they wait for the happening of an event that would never
be happen. For e.g. Traffic only in one direction,
For example, in the below diagram, Process 1 is holding
Resource 1 and waiting for resource 2 which is acquired by
process 2, and process 2 is waiting for resource 1.
1
DEADLOCK:
Deadlock can arise if the following four conditions hold
simultaneously (Necessary Conditions for Deadlock) :
Mutual Exclusion: Two or more resources are non-shareable
(Only one process can use at a time)
Hold and Wait: A process must be holding at least one resource
and waiting for at least one resource that is currently being held
by some other process.
No Preemption: Resources cannot be preempted i.e. once a
process is holding a resource then that resource cannot be taken
from a process unless the process releases the resource.
Circular Wait: A set {P0, P1, P2, ..., Pn} of waiting processes
must exist such that:
P0 is waiting for a resource held by P1,
P1 is waiting for a resource held by P2, …
Pn-1 is holding for a resource held by Pn, and
Pn is waiting for resource held by P0.
2
DEADLOCK:
Resource Allocation Graph::
The resource allocation graph is the pictorial representation of the
state of a system.
As its name suggests, the resource allocation graph is the
complete information about all the processes which are holding
some resources or waiting for some resources.
It also contains the information about all the instances of all the
resources whether they are available or being used by the
processes.
In Resource allocation graph, the process is represented by a
Circle while the Resource is represented by a rectangle.
Vertices are mainly of two types, Resource and process. Each of
them will be represented by a different shape.
Circle represents process while rectangle represents resource. A
resource can have more than one instance.
Each instance will be represented by a dot inside the rectangle.
3
DEADLOCK:
• Process
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
4
DEADLOCK:
6
DEADLOCK:
7
DEADLOCK:
9
DEADLOCK:
12
DEADLOCK:
Safe and Unsafe States: A system is said to be in a Safe
State, if there is a safe execution sequence.
An execution sequence is an ordering for process execution
such that each process runs until it terminates or blocked
and all request for resources are immediately granted if the
resource is available.
A system is said to be in an Unsafe State, if there is no safe
execution sequence.
An unsafe state may not be deadlocked, but there is at
least one sequence of requests from processes that would
make the system deadlocked.
For the Banker's algorithm to work, it needs to know
three things:
• How much of each resource could possibly request by
each process.
• How much of each resource is currently holding by each
process. 13
15
DEADLOCK:
Safety Algorithm:
This algorithm can be described as follows:
a. Finish[i] = = false
b. Needi ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation
Finish[i] = true
Go to step 2
4. If Finish[i] == true for all i, then the system is in safe
state.
16
DEADLOCK:
Resource-Request Algorithm:
Let Requesti be the request vector for process Pi. If
Requesti[j] = k, then process Pi wants k instances of
resource type Rj. When a request for resources is made by
process Pi, the following actions are taken:
1. If Requesti < Needi, go to step 2. Otherwise, raise an
error condition, since the process has
exceeded its maximum claim.
The system is currently in safe state. The sequence <P1, P3, P4, P2, P0> satisfies the
safety criteria. */ 19
DEADLOCK:
As we know that, Need[i][j]=Max[i][j]-Allocation[i][j]
So, the content of need matrix is:
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
20
DEADLOCK:
ii. Applying safety algorithm on the given system,
For pi, If Needi≤Available
Then p1 is in safe sequence.
Available = Available+Allocation,
For p0, Need0=(7,4,3)
Available=(3,3,2)
And Need0≤Available, is false
So, p0 must wait.
For p1, Need1=(1,2,2)
Available=(3,3,2)
And Need1≤Available, which is true
So, p1 will be in kept in safe sequence.
Now, Available will be updated as
Available= (2,3,2)+(2,0,0)=(5,3,2)
For p2, Need2=(6,0,0)
Available=(5,3,2)
and Need2≤Available, which is
false 21
So, p2 must wait.
DEADLOCK:
For p3, Need1=(0,1,1)
Available=(5,3,2)
And Need3≤Available, which is true
So, p3 will be in kept in safe sequence.
Now, Available will be updated as
Available= (2,3,2)+(2,1,1)=(7,4,3)
For p4, Need4=(4,3,1)
Available=(7,4,3)
And Need4≤Available, which is true
So, p4 will be in kept in safe sequence.
Now, Available will be updated as
Available= (7,4,3)+(0,0,2)=(7,4,5)
Now, we have two process p0 and p2 in waiting state. As current
available either p0 or p2 can be kept in safe sequence.
For p2, Need2=(6,0,0)
Available=(7,4,5)
and Need2≤Available, which is true
25
DEADLOCK:
Allocation0=Allocation+ Request0
= (1,0,1)+(0,2,0)
=(0,3,0)
Need0=Need0- Request0
=(7,4,3)-(0,2,0)
=(7,2,3)
30
DEADLOCK:
32
DEADLOCK:
33
DEADLOCK:
Work= available =2 1 0, finish=00000
P1: Need1 = 1 1 0 < 2 1 0, finish = 01000, work =2 1 0 + 2 1 2 =4 2 2
P0: Need0 =3 2 1 < 4 2 2, finish = 11000, work =4 2 2+ 1 1 2 =5 3 4
P2: Need2 =5 0 1 <5 3 4, finish =11100, work = 5 3 4+ 4 0 1 = 9 3 5
P3: Need3 =7 3 3 < 9 3 5, finish = 11110, work =9 3 5+ 0 2 0 = 9 5 5
P4: Need4 = 10 1 1 < 9 5 5, No sequence is possible thus the state is
unsafe.
(iv) Usage of deadlock detection algorithm:( assume that requests are
granted)
Work =available = 2 1 0, finish = 00000
P1: request1 = 1 1 0 < 2 1 0, finish = 01000, work =2 1 0 + 2 1 2 =4 2 2
P4: request4 = 0 11 < 4 2 2, finish - 01001, work =4 2 2 + 1 1 2 = 5 3 4
P0: request0 =3 3 1 < 5 3 4, finish = 11001, work =5 3 4 + 1 1 2 =6 4 6
P2: request2 =6 0 1 <6 4 6, finish =11101, work = 6 4 6 + 4 0 1 = 10 4 7
P3: request3 =7 2 3 < 10 6 7, finish = 11111, work =10 4 7 + 0 2 0 = 10 6 7
No deadlock: safe sequence is < P1, P4, P0, P2 ,P3>
34
DEADLOCK:
Example4:
Consider the snapshot of the system has been taken:
37
Protection and security:
Protection is the mechanism for controlling the access to program, process, and user to a
resource. Protection deal with the internal threats.
Security refers to protection mechanism to computer system resources such as CPU, memory,
disk, software programs and most importantly data/information stored in the computer
system. Security preserves the system from hacking and viruses, worms, and malware.
Security deals with the external threats. Mainly security covers integrity, confidentiality, and
availability of the system.
Confidentiality- This principle is based on privacy, access control and authentication. Only
authorized persons can access the system’s data. (eg. Wiretapping)
Integrity: This principle relates data accuracy. The authorized person must be able to access
or modify data without any flaw or error as per the terms (eg. SQL injection).
Availability: The authorized person when needed must be able to access computer resources
needed to access the data. (eg.DoS)
Operating system security is the mechanism that protect the OS from various intrusions such
as
• Threat: It is the processed potential for the occurrence of a harmful target.
• Attack: It is the action taken in the target system in order to harm the target.
• Vulnerability: It is the loop hole or weakness in a system processes that makes it
susceptible for harmful
If a computer program is run by an unauthorized user, then he/she may cause severe damage
to computer or data stored in it. So, a computer system must be protected against
unauthorized access, malicious access to system memory, viruses, worms etc.
The following methods are used for security:
• Authentication
• One Time passwords
• Program Threats
• System Threats
• Computer Security Classifications
Authentication:
Authentication refers to identifying each user of the system and associating the executing
programs with those users. It is the responsibility of the Operating System to create a
protection system which ensures that a user who is running a particular program is authentic.
Operating Systems generally identifies/authenticates users using following three ways −
• Username / Password − User need to enter a registered username and password with
Operating system to login into the system.
• User card/key − User need to punch card in card slot, or enter key generated by key
generator in option provided by operating system to login into the system.
• User attribute - fingerprint/ eye retina pattern/ signature − User need to pass
his/her attribute via designated input device used by operating system to login into the
system.
One Time passwords:
One-time passwords provide additional security along with normal authentication. In One-
Time Password system, a unique password is required every time user tries to login into the
system. Once a one-time password is used, then it cannot be used again. One-time passwords
are implemented in various ways.
• Random numbers − Users are provided cards having numbers printed along with
corresponding alphabets. System asks for numbers corresponding to few alphabets
randomly chosen.
• Secret key − User are provided a hardware device which can create a secret id
mapped with user id. System asks for such secret id which is to be generated every
time prior to login.
• Network password − Some commercial applications send one-time passwords to
user on registered mobile/ email which is required to be entered prior to login.
Program Threats:
Operating system's processes and kernel do the designated task as instructed. If a user
program made these process do malicious tasks, then it is known as Program Threats. One
of the common examples of program threat is a program installed in a computer which can
store and send user credentials via network to some hacker. Following is the list of some
well-known program threats:-
• Trojan Horse − Such program that traps user login credentials and stores them to
send to malicious user who can later on login to computer and can access system
resources. After it executes, this allows cybercriminals to perform many actions on
the user’s computer like deleting data from files, modifying data from files, and more.
Trojan horse does not have the ability to replicate itself. For example, when users
have downloaded the game then secretly turned into a self-replicating virus. Trojans
can also be found in MP3 songs that the user may have downloaded, from an
unsecured website.
The most basic prevention method of Trojan Horse are: –
➢ Do not download anything like the images, and audios from an unsecured
website.
➢ Do not click on the ads that pop up on the page with advertisements for online
games.
➢ Do not open any attachment that has been sent from an unknown use.
➢ The user has to install the anti-virus program.
• Trap Door
o A trap door is kind of a secret entry point into a program that allows anyone to
gain access to any system without going through the usual security access
procedures.
o It is a method of bypassing normal authentication methods. Therefore it is also
known as a back door.
o A developer may create a trap door so that an application, operating system
(OS) or data can be accessed for troubleshooting, debugging and testing
programs or other purposes.
o Trap doors turn to threats when any dishonest programmers gain illegal
access.
o Program development and software update activities should be the first focus
of security measures. The operating system that controls the trap doors is
difficult to implement.
• Logic Bomb − A logic bomb is a type of cyber attack in which a set of instructions is
secretly inserted into a computer system or application to cause damage. Logic bombs
execute malicious code (their “payload”) which can lead to a range of harmful
outcomes, from relatively minor ones like a slower computer or a few corrupt files to
major data losses or crashed systems. Logic bombs are different from other
cyberattacks because they remain inactive until certain conditions are met.
• Virus − Virus as name suggest can replicate themselves on computer system. They
are highly dangerous and can modify/delete user files, crash systems. A virus is
generally a small code embedded in a program. As user accesses the program, the
virus starts getting embedded in other files/ programs and can make system unusable
for user
System Threats:
System threats refer to misuse of system services and network connections to put user in
trouble. System threats can be used to launch program threats on a complete network called
as program attack. System threats create such an environment that operating system
resources/ user files are misused. Following is the list of some well-known system threats.
• Worm: Worm is a process which can choke down a system performance by using
system resources to extreme levels. A Worm process generates its multiple copies
where each copy uses system resources, prevents all other processes to get required
resources. Worms’s processes can even shut down an entire network.
• Port Scanning: Port scanning is a mechanism or means by which a hacker can detects
system vulnerabilities to make an attack on the system.
• Denial of Service: Denial of service attacks normally prevents user to make
legitimate use of the system. For example, a user may not be able to use internet if
denial of service attacks browser's content settings.
Prevention of security breaches: It’s better to prevent a security breach than to resolve one
after it occurs. Some common ways to prevent a security breaches are:-
• Secure your passwords: Create strong passwords that are unique for all of your
accounts and change them regularly.
• Two-factor authentication: Accounts that use two-factor authentication (2FA)
require two or more forms of authentication for access, making them more secure.
• Regular software updates: Software updates often include fixes to security
vulnerabilities. Regularly update your software and operating system to prevent
hackers from taking advantage of these vulnerabilities.
• Use anti-malware software: Quality anti-malware software can detect and remove
threats to your system and devices long before you even ask yourself: “Have I been
hacked?”
• Security equipment: Offices and homes with valuable data and systems should
protect against physical breaches by installing security equipment like cameras.
Types of attack:
It is the action taken in the target system in order to harm the target.
1. Active Attack 2. Passive attack
Active Attack: An active attack is an attack in which attackers directly harm your computer
systems. They can create several problems, such as crashing files, stealing data, etc. The
different types of active attack are:-
1.1 Masquerade Attack: Masquerade attacks are considered one type of cyber attack in
which the attacker disguises himself to pose as some other person and accesses systems or
data.
1.2 Denial of service (DOS): Denial of Service (DoS) is a form of cyber security attack that
involves denying the intended users of the system or network access by flooding traffic or
requests. A denial-of-service attack floods systems, servers, or networks with traffic to
exhaust resources and bandwidth. As a result, the system is unable to fulfil legitimate
requests.
1.3 Repudiation:
Repudiation attacks are a type of cyber attack wherein some person does something
damaging online, such as a financial transaction or sends a message one does not want to
send, then denies having done it.
1.4 Replay:
When the network is not completely secure or the login control is tampered with, an attack
occurs. With this attack, the information of the author can be changed by malicious user
actions to save suspicious data in log files, up to the widespread alteration of data on behalf
of others, similar to the spoofing of email messages.
Passive attack: A Passive attack refers to an attack in which the attackers quietly watch and
collect the information without your knowledge. They do not modify or destroy the data but
collect the data secretly. Passive Attacks are in the nature of eavesdropping on or monitoring
transmission. The types of passive attacks are:-
A Structured Query Language: A Structured Query Language (SQL) injection occurs when
an attacker inserts malicious code into a server that uses SQL and forces the server to reveal
information it normally would not.
Security Policy:
Security policy is a well thought and agreed document or statement of what is or is not
allowed, when implemented provides high possibility of overcoming threats, and attacks.
The security policy must include:
a) Scope: who the policy applies to
b) Who does the actions defined by the policy
c) When defined actions are to be done
d) Where or on what equipment the policy applies to
e) Who enforces the policy
f) Consequences of failure to follow the policy
Vulnerability Assessment:
It is the process of examining the system periodically to detect vulnerability. Is includes: -
• Port scanning
• Check for bad passwords
• Check for vulnerability programs in system
• Check for incorrect permissions
• Check for unauthorized accounts
Access control list (ACL): Access control list is a list that specifies which users or system
are granted or denied access to a particular object or system resource. ACLs are typically
associated with individual resources, such as files and directories. So, we can associate an
access lists with each file and directory to implement identity dependent access. When a user
access requests access to a particular file, the OS checks the access list associated with that
file. If that user is granted the requested access, then the access is allowed. Otherwise, a
protection violation occurs & the user is denied access to the file. But the main problem with
access lists is their length. It is very tedious to construct such a list. So, we use a condensed
version of the access list by classifying the users into 3 categories:
• Owners: The user who created the file.
• Group: A set of users who are sharing the files.
• Others: All other users in the system.
Here only 3 fields are required to define protection. Each field is a collection of bits each of
which either allows or prevents the access. E.g. The UNIX file system defines 3 fields of 3
bits each: rwx (r- read access, w-write access, x-execute access)
Separate fields are kept for file owners, group & other users. So, a bit is needed to record
protection information for each file.
Object A B C
domain
• There are 3 types of resource A, B & C and there are 3 users (domain) Ram, Shyam &
Hari.
• Ram can read, write and execute resource A, and only read the resource B and C.
• Similarly, Shyam can only read the resource A.B and C.
• Hari can only read the resource A and C but ( read, write and execute B)
Capability lists: Capability lists are a way of specifying what resources a particular user has
access to and what type of access they have. If we partitioned the access control matrix by
rows we get an list of access control list of an user. Capability lists are typically associated
with individual users which can access the network.
Protection Mechanism
• Protection mechanism is a procedure, or method to enforce security policy.
• It implements functions that help to prevent, detect and respond to recovery to
security attacks.
• It provides a means to distinguish authorized and unauthorized usage of the system.
• The protection model can be viewed as a matrix.
Protection domain
• Protection domain is a process that operates within that domain which specifies the
resources that the process may access.
• Each domain contains a set of objects and the operations that can be performed on
each object.
• The ability to execute an operation on an object is called access right.
• A domain is a collection of access rights as an ordered pair
• Two domains can share access rights.
• Domain can be realized as:
➢ User as a domain
➢ Process as a domain
➢ Procedure as a domain
OS Design Considerations for Security
1. Least privilege: Every object should work within a minimal set of privileges. Access rights
should be obtained by explicit request.
2. Economy of mechanism: security mechanism should be simple and small
3. Acceptability
4. Complete
5. Open design
Computer Security Classifications:
As per the U.S. Department of Defense Trusted Computer System's Evaluation Criteria there
are four security classifications in computer systems: A, B, C, and D. This is widely used
specifications to determine and model the security of systems and of security solutions.
Following is the brief description of each classification.
Type A:
Highest Level. Uses formal design specifications and verification techniques. Grants a high
degree of assurance of process security.
Type B:
Provides mandatory protection system. Have all the properties of a class C2 system. Attaches
a sensitivity label to each object. It is of three types.
• B1 − Maintains the security label of each object in the system. Label is used for
making decisions to access control.
• B2 − Extends the sensitivity labels to each system resource, such as storage objects,
supports covert channels and auditing of events.
• B3 − Allows creating lists or user groups for access-control to grant access or revoke
access to a given named object.
Type C:
Provides protection and user accountability using audit capabilities. It is of two types.
• C1 − Incorporates controls so that users can protect their private information and keep
other users from accidentally reading / deleting their data. UNIX versions are mostly
Cl class.
• C2 − Adds an individual-level access control to the capabilities of a Cl level system.
Type D:
• Lowest level. Minimum protection. MS-DOS, Window 3.1 fall in this category.
Cryptography Basics:
- Cryptography is the mechanism of transforming an intelligible message into unintelligible
form and retransforming back to its original form.
- Plaintext: original intelligible (accessible / readable) message
- Ciphertext: transformed message
- Cipher: algorithm used for transformation
- Key: critical information used by cipher.
Consider two parties Alice and Bob. Now, Alice wants to send a message m to Bob over a
secure channel. So, what happens is as follows. The sender’s message or sometimes called
the Plaintext, is converted into an unreadable form using a Key k. The resultant text obtained
is called the Ciphertext. This process is known as Encryption. At the time of receival, the
Ciphertext is converted back into the plaintext using the same Key k, so that it can be read by
the receiver. This process is known as Decryption.
Alice (Sender) Bob (Receiver)
C = E (m, k) ----> m = D (C, k)
Here, C refers to the Ciphertext while E and D are the Encryption and Decryption algorithms
respectively. Let’s consider the case of Caesar Cipher or Shift Cipher as an example. As the
name suggests, in Caesar Cipher each character in a word is replaced by another character
under some defined rules. Thus, if A is replaced by D, B by E and so on. Then, each character
in the word would be shifted by a position of 3. For example:
Plaintext :Geeksforgeeks
Ciphertext :Jhhnvirujhhnv
Firewall:
A firewall is a security device — computer hardware or software — that can help protects
your network by filtering traffic and blocking outsiders from gaining unauthorized access to
the private data on your computer.
- It is a network security system that monitors and controls all the incoming and outgoing
network traffic based on predetermined security rules.
- It establishes berries between secured internal network and outside network internet.
Firewalls filter the network traffic within a private network. It analyses which traffic should
be allowed or restricted based on a set of rules. Think of the firewall like a gatekeeper at your
computer’s entry point which only allows trusted sources, or IP addresses, to enter your
network.
A firewall welcomes only that incoming traffic that has been configured to accept. It
distinguishes between good and malicious traffic and either allows or blocks specific data
packets on pre-established security rules.
These rules are based on several aspects indicated by the packet data, like their source,
destination, content, and so on. They block traffic coming from suspicious sources to prevent
cyber attacks.
9.SYSTEM ADMINISTRATION
System Administrator
A system administrator, or sysadmin, is a person who is responsible for the upkeep,
configuration, and reliable operation of computer systems; especially multi-
user computers, such as servers.
SYSTEM ADMINISTRATION
System Administration Tasks
✓Planning - cost, capacity planning, logistics (network design, server locations, where to install,
wiring, IP address assignments, ...), network service providers (ISPs)
✓Preparing - temperature, humidity, electrical, fire, security, EIA/TIA wiring closet and cabling
standards, UPS; Change management (preparing for any service changes)
✓Installing hardware - computers, terminals, disk drives, CD-ROMs, RAM, printers, NICs,
cabling
✓Maintaining - regular preventative maintenance (daily, weekly, ...), boot and shutdown
systems when needed, printers, backup media, tune systems for performance
✓Monitoring - printers, disk space, network, servers and workstations, performance, and
security, and all log files regularly
✓Installing/upgrading/removing software - OS (kernel patches, new device drivers, ...),
applications (new versions, DLLs, new configurations), documentation
THANK YOU !!