Adikavi Nannaya University: Unit-I Topics
Adikavi Nannaya University: Unit-I Topics
Adikavi Nannaya University: Unit-I Topics
OF COMPUTER SCIENCE
UNIT- I
TOPICS
1.10 Threads 26
B.V.A.SWAMY 1|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.0
B.V.A.SWAMY 2|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.0
1
Following are some of important functions of an operating System.
• Memory Management
• Processor Management
• Device Management
• File Management
• Security
• Control over system performance
• Job accounting
• Error detecting aids
• Coordination between other software and users
a) Memory Management
Memory management refers to management of Primary Memory or Main Memory. Main
memory is a large array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a
program to be executed, it must in the main memory. An Operating System does the following
activities for memory management −
• Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part
are not in use.
• In multiprogramming, the OS decides which process will get memory when and how
much.
• Allocates the memory when a process requests it to do so.
• De-allocates the memory when a process no longer needs it or has been terminated.
b) Processor Management
In multiprogramming environment, the OS decides which process gets the processor
when and for how much time. This function is called process scheduling. An Operating
System does the following activities for processor management −
• Keeps tracks of processor and status of process. The program responsible for this
task is known as traffic controller.
• Allocates the processor (CPU) to a process.
• De-allocates processor when a process is no longer required.
c) Device Management
An Operating System manages device communication via their respective drivers. It does
the following activities for device management −
• Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.
• Decides which process gets the device when and for how much time.
• Allocates the device in the efficient way.
B.V.A.SWAMY 3|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.0
d) File Management
A file system is normally organized into directories for easy navigation and usage. These
directories may contain files and other directions.
An Operating System does the following activities for file management −
• Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
• Decides who gets the resources.
• Allocates the resources.
• De-allocates the resources.
B.V.A.SWAMY 4|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.1
B.V.A.SWAMY 5|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.1
• Problem of reliability.
• Question of security and integrity of user programs and data.
• Problem of data communication.
• With resource sharing facility, a user at one site may be able to use the resources
available at another.
• Speedup the exchange of data with one another via electronic mail.
• If one site fails in a distributed system, the remaining sites can potentially continue
operating.
• Better service to the customers.
• Reduction of the load on the host computer.
• Reduction of delays in data processing.
B.V.A.SWAMY 6|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.1
B.V.A.SWAMY 7|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.2
Layered structure:
An OS can be broken into pieces and retain much more control on system. In this
structure the OS is broken into number of layers (levels). The bottom layer (layer 0) is the
hardware and the topmost layer (layer N) is the user interface. These layers are so
designed that each layer uses the functions of the lower level layers only. This simplifies
the debugging process as if lower level layers are debugged and an error occurs during
debugging then the error must be on that layer only as the lower level layers have already
been debugged.
The main disadvantage of this structure is that at each layer, the data needs to be
modified and passed on which adds overhead to the system. Moreover careful planning of
the layers is necessary as a layer can use only lower level layers. UNIX is an example of
this structure.
B.V.A.SWAMY 8|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.2
Micro-kernel:
This structure designs the operating system by removing all non-essential
components from the kernel and implementing them as system and user programs. This
result in a smaller kernel called the micro-kernel.
Advantages of this structure are that all new services need to be added to user space and
does not require the kernel to be modified. Thus it is more secure and reliable as if a service
fails then rest of the operating system remains untouched. Mac OS is an example of this
type of OS.
B.V.A.SWAMY 9|Page
DEPT. OF COMPUTER SCIENCE
Topic: 1.3
• Program execution
• I/O operations
• File System manipulation
• Communication
• Error Detection
• Resource Allocation
• Protection
a) Program execution
Operating systems handle many kinds of activities from user programs to system programs
like printer spooler, name servers, file server, etc. Each of these activities is encapsulated as
a process.
A process includes the complete execution context (code to execute, data to manipulate,
registers, OS resources in use). Following are the major activities of an operating system with
respect to program management −
b) I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver software. Drivers
hide the peculiarities of specific hardware devices from the users.
An Operating System manages the communication between user and device drivers.
• I/O operation means read or write operation with any file or any specific I/O device.
• Operating system provides the access to the required I/O device when required.
Topic: 1.3
magnetic tape, magnetic disk and optical disk drives like CD, DVD. Each of these media has
its own properties like speed, capacity, data transfer rate and data access methods.
A file system is normally organized into directories for easy navigation and usage. These
directories may contain files and other directions. Following are the major activities of an
operating system with respect to file management −
d) Communication
In case of distributed systems which are a collection of processors that do not share memory,
peripheral devices, or a clock, the operating system manages communications between all
the processes. Multiple processes communicate with one another through communication
lines in the network.
The OS handles routing and connection strategies, and the problems of contention and
security. Following are the major activities of an operating system with respect to
communication −
e) Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices or in the
memory hardware. Following are the major activities of an operating system with respect to
error handling −
f) Resource Management
In case of multi-user or multi-tasking environment, resources such as main memory, CPU
cycles and files storage are to be allocated to each user or job. Following are the major
activities of an operating system with respect to resource management −
B.V.A.SWAMY 11 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.3
g) Protection
Considering a computer system having multiple users and concurrent execution of multiple
processes, the various processes must be protected from each other's activities.
Protection refers to a mechanism or a way to control the access of programs, processes, or
users to the resources defined by a computer system. Following are the major activities of an
operating system with respect to protection −
B.V.A.SWAMY 12 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.4
As can be seen from this diagram, the processes execute normally in the user mode
until a system call interrupts this. Then the system call is executed on a priority basis in the
kernel mode. After the execution of the system call, the control returns to the user mode and
execution of user processes can be resumed.
In general, system calls are required in the following situations −
• If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
• Creation and management of new processes.
• Network connections also require system calls. This includes sending and receiving
packets.
• Access to a hardware devices such as a printer, scanner etc. requires a system call.
B.V.A.SWAMY 13 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.4
• Device Management : These system calls are responsible for device manipulation
such as reading from device buffers, writing into device buffers etc.
• Information Maintenance : These system calls handle information and its transfer
between the operating system and the user program.
B.V.A.SWAMY 14 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.4
There are many different system calls as shown above. Details of some of those system calls
are as follows −
• open() : The open() system call is used to provide access to a file in a file system. This system
call allocates resources to the file and provides a handle that the process uses to refer to the file.
A file can be opened by multiple processes at the same time or be restricted to one process. It all
depends on the file organisation and file system.
• read():The read() system call is used to access data from a file that is stored in the file system.
The file to read can be identified by its file descriptor and it should be opened using open() before
it can be read. In general, the read() system calls takes three arguments i.e. the file descriptor,
buffer which stores read data and number of bytes to be read from the file.
• write() : The write() system calls writes the data from a user buffer into a device such as a file.
This system call is one of the ways to output data from a program. In general, the write system
calls takes three arguments i.e. file descriptor, pointer to the buffer where data is stored and
number of bytes to write from the buffer.
• close() : The close() system call is used to terminate access to a file system. Using this system
call means that the file is no longer required by the program and so the buffers are flushed, the file
metadata is updated and the file resources are de-allocated.
• wait() : In some systems, a process may wait for another process to complete its execution. This
happens when a parent process creates a child process and the execution of the parent process
is suspended until the child process executes. The suspending of the parent process occurs with
a wait() system call. When the child process completes execution, the control is returned back to
the parent process.This system call runs an executable file in the context of an already running
process. It replaces the previous executable file. This is known as an overlay. The original process
identifier remains since a new process is not created but data, heap, stack etc. of the process are
replaced by the new process.
• fork() : Processes use the fork() system call to create processes that are a copy of themselves.
This is one of the major methods of process creation in operating systems. When a parent process
creates a child process and the execution of the parent process is suspended until the child
process executes. When the child process completes execution, the control is returned back to the
parent process.
• exit() : The exit() system call is used by a program to terminate its execution. In a multithreaded
environment, this means that the thread execution is complete. The operating system reclaims
resources that were used by the process after the exit() system call.
• kill() : The kill() system call is used by the operating system to send a termination signal to a
process that urges the process to exit. However, kill() system call does not necessarily mean killing
the process and can have various meanings.
B.V.A.SWAMY 15 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.5
• System Models
B.V.A.SWAMY 16 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.5
SYSGEN program obtains information concerning the specific configuration of the hardware
system.
To generate a system, we use a special program. The SYSGEN program reads from
a given file, or asks the operator of the system for information concerning the specific
configuration of the hardware system, or probes the hardware directly to determine what
components are there.
What CPU will be used? What options (extended instruction sets, floating point arithmetic, and
so on) are installed? For multiple-CPU systems, each CPU must be described.
How much memory is available? Some systems will determine this value themselves by
referencing memory location after memory location until an "illegal address" fault is generated.
This procedure defines the final legal address and hence the amount of available memory.
What devices are available? The system will need to know how to address each device (the
device number), the device interrupt number, the device's type and model, and any special
device characteristics.
What operating-system options are desired, or what parameter values are to be used? These
options or values might include how many buffers of which sizes should be used, what type of
CPU scheduling algorithm is desired, what the maximum number of processes to be supported
is.
Booting –The procedure of starting a computer by loading the kernel is known as booting the
system. Most
computer systems have a small piece of code, stored in ROM, known as the bootstrap
program or bootstrap
loader. This code is able to locate the kernel, load it into main memory, and start its execution.
Some computer systems, such as PCs, use a two-step process in which a simple bootstrap
loader fetches a more complex boot program from disk, which in turn loads the kernel.
B.V.A.SWAMY 17 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.6
B.V.A.SWAMY 18 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.6
B.V.A.SWAMY 19 | P a g e
DEPT. OF COMPUTER SCIENCE
B.V.A.SWAMY 21 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.7
Process Management
Topic:1.7 Process Concepts
1). What is process concept?
Ans:
Process Concept
• Informally, a process is a program in execution. A process is more than the program
code, which is sometimes known as the text section. It also includes the current
activity, as represented by the value of the program counter and the contents of the
processor's registers. In addition, a process generally includes the process stack,
which contains temporary data (such as method parameters, return addresses, and
local variables), and a data section, which contains global variables.
• An operating system executes a variety of programs:
✦ Batch system – jobs
✦ Time-shared systems – user programs or tasks
• Process – a program in execution; process execution must progress in sequential
fashion.
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
B.V.A.SWAMY 22 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.8
B.V.A.SWAMY 23 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.9
Ans: Cooperating processes are those that can affect or are affected by other
processes running on the system. Cooperating processes may share data with each
other.
• Modularity
Modularity involves dividing complicated tasks into smaller subtasks. These
subtasks can completed by different cooperating processes. This leads to faster
and more efficient completion of the required tasks.
• Information Sharing
Sharing of information between multiple processes can be accomplished using
cooperating processes. This may include access to the same files. A
mechanism is required so that the processes can access the files in parallel to
each other.
• Convenience
There are many tasks that a user needs to do such as compiling, printing,
editing etc. It is convenient if these tasks can be managed by cooperating
processes.
• Computation Speedup
Subtasks of a single task can be performed parallely using cooperating
processes. This increases the computation speedup as the task can be
executed faster. However, this is only possible if the system has multiple
processing elements.
Methods of Cooperation
Cooperating processes can coordinate with each other using shared data or
messages. Details about these are given as follows −
• Cooperation by Sharing
The cooperating processes can cooperate with each other using shared data
such as memory, variables, files, databases etc. Critical section is used to
provide data integrity and writing is mutually exclusive to prevent inconsistent
data.
B.V.A.SWAMY 24 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.9
In the above diagram, Process P1 and P2 can cooperate with each other using
shared data such as memory, variables, files, databases etc.
• Cooperation by Communication
The cooperating processes can cooperate with each other using messages.
This may lead to deadlock if each process is waiting for a message from the
other to perform a operation. Starvation is also possible if a process never
receives a message.
A diagram that demonstrates cooperation by communication is given as follows
In the above diagram, Process P1 and P2 can cooperate with each other using
messages to communicate.
B.V.A.SWAMY 25 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
4).What is Thread?
Ans:
A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers which
hold its current working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data
segment and open files. When one thread alters a code segment memory item, all
other threads see that.
A thread is also called a lightweight process. Threads provide a way to
improve application performance through parallelism. Threads represent a software
approach to improving performance of operating system by reducing the overhead
thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a
process. Each thread represents a separate flow of control. Threads have been
successfully used in implementing network servers and web server. They also
provide a suitable foundation for parallel execution of applications on shared memory
multiprocessors. The following figure shows the working of a single-threaded and a
multithreaded process.
B.V.A.SWAMY 26 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
4 If one process is blocked, then no While one thread is blocked and waiting,
other process can execute until the a second thread in the same task can
first process is unblocked. run.
6 In multiple processes each process One thread can read, write or change
operates independently of the another thread's data.
others.
Advantages of Thread
B.V.A.SWAMY 27 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
Advantages
B.V.A.SWAMY 28 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
Advantages
• Kernel can simultaneously schedule multiple threads from the same process on
multiple processes.
• If one thread in a process is blocked, the Kernel can schedule another thread of the
same process.
• Kernel routines themselves can be multithreaded.
Disadvantages
• Kernel threads are generally slower to create and manage than the user threads.
• Transfer of control from one thread to another within the same process requires a
mode switch to the Kernel.
Multithreading Models
Some operating system provide 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. Multithreading models
are three types
B.V.A.SWAMY 29 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
B.V.A.SWAMY 30 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.10
1 User-level threads are faster to create and Kernel-level threads are slower to
manage. create and manage.
3 User-level thread is generic and can run on Kernel-level thread is specific to the
any operating system. operating system.
B.V.A.SWAMY 31 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.11
B.V.A.SWAMY 32 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.11
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for
an OS designer but complicated for a programmer and if it is of variable size then it is easy
for a programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message
length, and control information. The control information contains information like what to do
if runs out of buffer space, sequence number, priority. Generally, message is sent using
FIFO style.
Message Passing through Communication Link.
Direct and Indirect Communication link
Now, We will start our discussion about the methods of implementing communication links.
While implementing the link, there are some questions that need to be kept in mind like :
Topic: 1.11
4. What is the capacity of a link? Is the size of a message that the link can
accommodate fixed or variable?
5. Is a link unidirectional or bi-directional?
A link has some capacity that determines the number of messages that can reside in it
temporarily for which every link has a queue associated with it which can be of zero
capacity, bounded capacity, or unbounded capacity. In zero capacity, the sender waits
until the receiver informs the sender that it has received the message. In non-zero capacity
cases, a process does not know whether a message has been received or not after the
send operation. For this, the sender must communicate with the receiver explicitly.
Implementation of the link depends on the situation, it can be either a direct
communication link or an in-directed communication link.
Direct Communication links are implemented when the processes use a specific process
identifier for the communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue
of messages. The sender keeps the message in mailbox and the receiver picks them up.
communicate only if they share a mailbox. Link established only if processes share a
common mailbox and a single link can be associated with many processes. Each pair of
processes can share several communication links and these links may be unidirectional or
bi-directional. Suppose two processes want to communicate through Indirect message
passing, the required operations are: create a mailbox, use this mailbox for sending and
receiving messages, then destroy the mailbox. The standard primitives used are: send(A,
message) which means send the message to mailbox A. The primitive for the receiving
the message also works in the same way e.g. received (A, message). There is a problem
with this mailbox implementation. Suppose there are more than two processes sharing the
same mailbox and suppose the process p1 sends a message to the mailbox, which
process will be the receiver? This can be solved by either enforcing that only two
processes can share a single mailbox or enforcing that only one process is allowed to
execute the receive at a given time or select any process randomly and notify the sender
about the receiver. A mailbox can be made private to a single sender/receiver pair and can
also be shared between multiple sender/receiver pairs. Port is an implementation of such
mailbox that can have multiple senders and a single receiver. It is used in client/server
applications (in this case the server is the receiver). The port is owned by the receiving
process and created by OS on the request of the receiver process and can be destroyed
either on request of the same receiver processor when the receiver terminates itself.
Enforcing that only one process is allowed to execute the receive can be done using the
concept of mutual exclusion. Mutex mailbox is created which is shared by n process. The
sender is non-blocking and sends the message. The first process which executes the
receive will enter in the critical section and all other processes will be blocking and will
wait.
Now, let’s discuss the Producer-Consumer problem using the message passing concept.
The producer places items (inside messages) in the mailbox and the consumer can
consume an item when at least one message present in the mailbox
Topic: 1.12
• Job queue − This queue keeps all the processes in the system.
• Ready queue − This queue keeps a set of all processes residing in main
memory, ready and waiting to execute. A new process is always put in this
queue.
• Device queues − The processes which are blocked due to unavailability of an
I/O device constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin,
Priority, etc.). The OS scheduler determines how to move processes between the
ready and run queues which can only have one entry per processor core on the
system; in the above diagram, it has been merged with the CPU.
Two-State Process Model
Two-state process model refers to running and non-running states which are
described below −
1
Running
When a new process is created, it enters into the system as in the running
state.
2
Not Running
Processes that are not running are kept in queue, waiting for their turn to
execute. Each entry in the queue is a pointer to a particular process. Queue
is implemented by using linked list. Use of dispatcher is as follows. When a
process is interrupted, that process is transferred in the waiting queue. If the
process has completed or aborted, the process is discarded. In either case,
the dispatcher then selects a process from the queue to execute.
B.V.A.SWAMY 36 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.12
Schedulers
Schedulers are special system software which handle 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. Schedulers are of three types −
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
Long Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs
are admitted to the system for processing. It selects processes from the queue and
loads them into memory for execution. Process loads into the memory for CPU
scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such
as I/O bound and processor bound. It also controls the degree of multiprogramming.
If the degree of multiprogramming is stable, then the average rate of process creation
must be equal to the average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-
sharing operating systems have no long term scheduler. When a process changes
the state from new to ready, then there is use of long-term scheduler.
Short Term Scheduler
It is also called as CPU scheduler. Its main objective is to increase system
performance in accordance with the chosen set of criteria. It is the change of ready
state to running state of the process. CPU scheduler selects a process among the
processes that are ready to execute and allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which
process to execute next. Short-term schedulers are faster than long-term schedulers.
Medium Term Scheduler
Medium-term scheduling is a part of swapping. It removes the processes from the
memory. It reduces the degree of multiprogramming. The medium-term scheduler is
in-charge of handling the swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended
processes cannot make any progress towards completion. In this condition, to
remove the process from memory and make space for other processes, the
suspended process is moved to the secondary storage. This process is
called swapping, and the process is said to be swapped out or rolled out. Swapping
may be necessary to improve the process mix.
B.V.A.SWAMY 37 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.12
Context Switch
A context switch is the mechanism to store and restore the state or context of
a CPU in Process Control block so that a process execution can be resumed from
the same point at a later time. Using this technique, a context switcher enables
multiple processes to share a single CPU. Context switching is an essential part of a
multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute
another, the state from the current running process is stored into the process control
block. After this, the state for the process to run next is loaded from its own PCB and
used to set the PC, registers, etc. At that point, the second process can start
executing.
B.V.A.SWAMY 38 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.12
Context switches are computationally intensive since register and memory state must
be saved and restored. To avoid the amount of context switching time, some
hardware systems employ two or more sets of processor registers. When the process
is switched, the following information is stored for later use.
• Program Counter
• Scheduling information
• Base and limit register value
• Currently used register
• Changed State
• I/O State information
• Accounting information
B.V.A.SWAMY 39 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.12
2. Non-Preemptive Scheduling:
Non-preemptive Scheduling is used when a process terminates, or a
process switches from running to waiting state. In this scheduling, once the
resources (CPU cycles) is allocated to a process, the process holds the CPU till it
gets terminated or it reaches a waiting state. In case of non-preemptive scheduling
does not interrupt a process running CPU in middle of the execution. Instead, it
waits till the process complete its CPU burst time and then it can allocate the CPU
to another process.
Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF
basically non preemptive) and Priority (non preemptive version), etc.
B.V.A.SWAMY 40 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.12
B.V.A.SWAMY 41 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.13
B.V.A.SWAMY 42 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.13
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
B.V.A.SWAMY 43 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic:
Topic: 1.13
1.13
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
B.V.A.SWAMY 44 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.13
P0 0-0=0
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
B.V.A.SWAMY 45 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.13
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
B.V.A.SWAMY 46 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.14
B.V.A.SWAMY 47 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 1.15
Multicore Processors
Recent trend to place multiple processor cores on same physical chip
Faster and consume less power
Multiple threads per core also growing
Takes advantage of memory stall to make progress on another thread while memory retrieve
happens
B.V.A.SWAMY 48 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT -2
TOPICS
Process Synchronization: Page No: 51
Deadlocks: Page No
B.V.A.SWAMY 49 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT-2
Process Synchronization
Process Synchronization is mainly used for Cooperating Process that shares the
resources.Let us consider an example of
//racing condition image
B.V.A.SWAMY 50 | P a g e
DEPT. OF COMPUTER SCIENCE
It is the condition where several processes tries to access the resources and
modify the shared data concurrently and outcome of the process depends on the
particular order of execution that leads to data inconsistency, this condition is
called Race Condition. This condition can be avoided using the technique called
Synchronization or Process Synchronization, in which we allow only one process to
enter and manipulates the shared data in Critical Section.
//diagram of the view of CS
B.V.A.SWAMY 51 | P a g e
DEPT. OF COMPUTER SCIENCE
B.V.A.SWAMY 52 | P a g e
DEPT. OF COMPUTER SCIENCE
2. Hardware Approach –
The Hardware Approach of synchronization can be done through Lock & Unlock
technique. Locking part is done in the Entry Section, so that only one process is
allowed to enter into the Critical Section, after it complete its execution, the process
is moved to the Exit Section, where Unlock Operation is done so that another process
in the Lock Section can repeat this process of Execution. This process is designed in
such a way that all the three conditions of the Critical Sections are satisfied.
//Image of Lock
Using Interrupts –
These are easy to implement.When Interrupt are disabled then no other process is
allowed to perform Context Switch operation that would allow only one process to
enter into the Critical State.
//Image of Interrupts
B.V.A.SWAMY 53 | P a g e
DEPT. OF COMPUTER SCIENCE
Test_and_Set Operation –
This allows boolean value (True/False) as a hardware Synchronization, which is atomic
in nature i.e no other interrupt is allowed to access. This is mainly used in Mutual
Exclusion Application. Similar type operation can be achieved through Compare and
Swap function. In this process, a variable is allowed to accessed in Critical Section
while its lock operation is ON. Till then, the other process is in Busy Waiting State.
Hence Critical Section Requirements are achieved.
Critical Section:
In simple terms a critical section is group of instructions/statements or region
of code that need to be executed atomically (read this post for atomicity), such as
accessing a resource (file, input or output port, global data, etc.).
In concurrent programming, if one thread tries to change the value of shared
data at the same time as another thread tries to read the value (i.e. data race across
threads), the result is unpredictable.
The access to such shared variable (shared memory, shared files, shared port,
etc…) to be synchronized. Few programming languages have built-in support for
synchronization.
It is critical to understand the importance of race condition while writing kernel
mode programming (a device driver, kernel thread, etc.). since the programmer can
directly access and modifying kernel data structures.
A thread must acquire a lock prior to executing a critical section. The lock can be
acquired by only one thread. There are various ways to implement locks in the
above pseudo code. Let us discuss them in future articles.
B.V.A.SWAMY 54 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.1
Entry Section
In this section mainly the process requests for its entry in the critical section.
Exit Section
This section is followed by the critical section.
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical
section at a given point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute their
critical section then any one of these threads must be allowed to get into its critical
section.
B.V.A.SWAMY 55 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.1
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for
how many other processes can get into their critical section, before this process's
request is granted. So after the limit is reached, the system must grant the process
permission to get into its critical section.
1.Peterson's Solution
This is widely used and software-based solution to critical section problems.
Peterson's solution was developed by a computer scientist Peterson that's why it is
named so.
With the help of this solution whenever a process is executing in any critical
state, then the other process only executes the rest of the code, and vice-versa can
happen. This method also helps to make sure of the thing that only a single process
can run in the critical section at a specific time.
This solution preserves all three conditions:
• Mutual Exclusion is comforted as at any time only one process can access the
critical section.
• Progress is also comforted, as a process that is outside the critical section is
unable to block other processes from entering into the critical section.
• Bounded Waiting is assured as every process gets a fair chance to enter the
Critical section.
B.V.A.SWAMY 56 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.1
Synchronization Hardware
Many systems provide hardware support for critical section code. The critical
section problem could be solved easily in a single-processor environment if we could
disallow interrupts to occur while a shared variable or resource is being modified.
In this manner, we could be sure that the current sequence of instructions
would be allowed to execute in order without pre-emption. Unfortunately, this
solution is not feasible in a multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time-consuming
as the message is passed to all the processors.
This message transmission lag delays the entry of threads into the critical
section, and the system efficiency decreases.
Mutex Locks
As the synchronization hardware solution is not easy to implement for everyone, a
strict software approach called Mutex Locks was introduced. In this approach, in the
entry section of code, a LOCK is acquired over the critical resources modified and
used inside the critical section, and in the exit section that LOCK is released.
As the resource is locked while a process executes its critical section hence no other
process can access it.
B.V.A.SWAMY 57 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2
Introduction to Semaphores
In 1965, Dijkstra proposed a new and very significant technique for managing
concurrent processes by using the value of a simple integer variable to synchronize
the progress of interacting processes. This integer variable is called semaphore. So
it is basically a synchronizing tool and is accessed only through two low standard
atomic operations, wait and signal designated by P(S) and V(S) respectively.
In very simple words, semaphore is a variable which can hold only a non-
negative Integer value, shared between all the threads, with
operations wait and signal, which work as follow:
P(S): if S ≥ 1 then S := S - 1
else S := S + 1;
• Wait: Decrements the value of its argument S, as soon as it would become non-
• Signal: Increments the value of its argument S, as there is no more process blocked
on the queue.
Properties of Semaphores
5. Can permit multiple processes into the critical section at once, if desirable.
Types of Semaphores
Semaphores are mainly of two types:
B.V.A.SWAMY 58 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2
1. Binary Semaphore:
2. Counting Semaphores:
Example of Use
Here is a simple step wise implementation involving declaration and usage of
semaphore.
Shared var mutex: semaphore = 1;
Process i
begin
P(mutex);
execute CS;
V(mutex);
End;
B.V.A.SWAMY 59 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2
Limitations of Semaphores
Semaphore is simply a variable. This variable is used to solve the critical section
problem and to achieve process synchronization in the multiprocessing environment.
The two most common kinds of semaphores are counting semaphores and binary
semaphores. Counting semaphore can take non-negative integer values and Binary
semaphore can take the value 0 & 1. only.
Now let us see how it do so.
First, look at two operations which can be used to access and change the value of the
semaphore variable.
B.V.A.SWAMY 60 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2
Now, let us see how it implements mutual exclusion. Let there be two processes P1
and P2 and a semaphore s is initialized as 1. Now if suppose P1 enters in its critical
section then the value of semaphore s becomes 0. Now if P2 wants to enter its critical
section then it will wait until s > 0, this can only happen when P1 finishes its critical
section and calls V operation on semaphore s. This way mutual exclusion is achieved.
Look at the below image for details.
The description above is for binary semaphore which can take only two values 0 and
1. There is one other type of semaphore called counting semaphore which can take
values greater than one.
Now suppose there is a resource whose number of instance is 4.
Now we initialize S = 4 and rest is same as for binary semaphore.
Whenever process wants that resource it calls P or wait function and when it is done it
calls V or signal function.
If the value of S becomes zero then a process has to wait until S becomes positive.
B.V.A.SWAMY 61 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2
For example, Suppose there are 4 process P1, P2, P3, P4 and they all call wait operation
on S(initialized with 4). If another process P5 wants the resource then it should wait
until one of the four processes calls signal function and value of semaphore becomes
positive.
Problem in this implementation of semaphore
Whenever any process waits then it continuously checks for semaphore value (look at
this line while (s==0); in P operation) and waste CPU cycle.
To avoid this another implementation is provided below.
P(Semaphore s)
{
s = s - 1;
if (s <= 0) {
// add process to queue
block();
}
}
V(Semaphore s)
{
s = s + 1;
if (s <= 0) {
// remove process p from queue
wakeup(p);
}
}
B.V.A.SWAMY 62 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
a finite buffer pool is used to exchange messages between producer and consumer
processes.
Because the buffer pool has a maximum size, this problem is often called
the Bounded buffer problem.
• Solution to this problem is, creating two counting semaphores "full" and "empty" to
keep track of the current number of full and empty buffers respectively.
• There are five philosophers sitting around a table, in which there are five
chopsticks/forks kept beside them and a bowl of rice in the centre, When a
philosopher wants to eat, he uses two chopsticks - one from their left and one from
their right. When a philosopher wants to think, he keeps down both chopsticks at
B.V.A.SWAMY 63 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
• In this problem there are some processes(called readers) that only read the shared
data, and never change it, and there are other processes(called writers) who may
• There are various type of readers-writers problem, most centred on relative priorities
Here's a Solution
One solution of this problem is to use semaphores. The semaphores which will be
used here are:
B.V.A.SWAMY 64 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
• empty, a counting semaphore whose initial value is the number of slots in the
At any instant, the current value of empty represents the number of empty slots in
the buffer and full represents the number of occupied slots in the buffer.
wait(empty);
// acquire lock
wait(mutex);
// release lock
signal(mutex);
// increment 'full'
signal(full);
while(TRUE)
• Looking at the above code for a producer, we can see that a producer first waits until
• Then it decrements the empty semaphore because, there will now be one less
empty slot, since the producer is going to insert data in one of those slots.
• Then, it acquires lock on the buffer, so that the consumer cannot access the buffer
• After performing the insert operation, the lock is released and the value of full is
incremented because the producer has just filled a slot in the buffer.
B.V.A.SWAMY 65 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
wait(full);
wait(mutex);
signal(mutex);
// increment 'empty'
signal(empty);
while(TRUE);
• The consumer waits until there is atleast one full slot in the buffer.
• Then it decrements the full semaphore because the number of occupied slots will be
• Following that, the consumer completes the removal operation so that the data from
• Finally, the empty semaphore is incremented by 1, because the consumer has just
B.V.A.SWAMY 66 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
B.V.A.SWAMY 67 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
wait(stick[i]);
/*
*/
wait(stick[(i+1) % 5]);
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
When a philosopher wants to eat the rice, he will wait for the chopstick at his
left and picks up that chopstick. Then he waits for the right chopstick to be available,
and then picks it too. After eating, he puts both the chopsticks down.
But if all five philosophers are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they will be waiting for another
chopstick forever. The possible solutions for this are:
• A philosopher must be allowed to pick up the chopsticks only if both the left and right
• Allow only four philosophers to sit at the table. That way, if all the four philosophers
pick up four chopsticks, there will be one chopstick left on the table. So, one
philosopher can start eating and eventually, two chopsticks will be available. In this
B.V.A.SWAMY 68 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
The Solution
From the above problem statement, it is evident that readers have higher priority
than writer. If a writer wants to write to the resource, it must wait until there are no
readers currently accessing that resource.
Here, we use one mutex m and a semaphore w. An integer variable read_count is
used to maintain the number of readers currently accessing the resource. The
variable read_count is initialized to 0. A value of 1 is given initially to m and w.
Instead of having the process to acquire lock on the shared resource, we use the
mutex m to make the process to acquire and release lock whenever it is updating
the read_count variable.
The code for the writer process looks like this:
while(TRUE)
wait(w);
signal(w);
B.V.A.SWAMY 69 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
And, the code for the reader process looks like this:
while(TRUE)
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
• As seen above in the code for the writer, the writer just waits on the w semaphore
B.V.A.SWAMY 70 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.2.1
• After performing the write operation, it increments w so that the next writer can
• On the other hand, in the code for the reader, the lock is acquired whenever
the read_count value, then accesses the resource and then decrements
• The semaphore w is used by the first reader which enters the critical section and the
• The reason for this is, when the first readers enters the critical section, the writer is
blocked from the resource. Only new readers can access the resource now.
• Similarly, when the last reader exits the critical section, it signals the writer using
the w semaphore because there are zero readers now and a writer can have the
B.V.A.SWAMY 71 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.3
Critical Region
According to the critical section problem using semaphore all processes must share a
semaphore variablemutex which is initialized to one. Each process must execute wait
(mutex) before entering the critical section and execute the signal (mutex) after completing
the execution but there are various difficulties may arise with this approach like:
Case 1:
Suppose that a process interchanges the order in which the wait and signal operations on
Signal (mutex);
..........
Critical Section
...........
Wait (mutex);
In this situation several processes may be executing in their critical sections simultaneously,
Case 2:
Suppose that a process replaces the signal (mutex) with wait (mutex). The execution is as
follows:
Wait (mutex);
...........
Critical Section
...........
Wait (mutex);
Topic: 2.3
Case 3:Suppose that a process omits the wait (mutex) and the signal (mutex). In this case
the mutual exclusion is violated or a deadlock will occur. To illustrate the various types or
error generated by using semaphore there are some high level language constructs have
been introduced such as critical region and monitor. Critical region is also known as
conditional critical regions. It constructs guards against certain simple errors associated with
semaphore. This high level language synchronization construct requires a variable V of type
The variable V can be accessed only inside a region statement as like below
Wait (mutex);
While (! B) {
First_count++;
if (second_count> 0)
Signal (second_delay);
Else
Signal (mutex);
Wait (first_delay);
First_count--;
Second_count++;
if (first_count> 0)
Signal (first_delay);
Else
Signal (second_delay);
Wait (second_delay);
Second_count --;
}
S;
if (first_count> 0)
Signal (first_delay);
Else if (second_count> 0)
Signal (second_delay);
Else
Signal (mutex);
B.V.A.SWAMY 73 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.4
Monitors
Monitor is one of the ways to achieve Process synchronization. Monitor is supported
by programming languages to achieve mutual exclusion between processes. For
example Java Synchronized methods. Java provides wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined together in a
special kind of module or a package.
2. The processes running outside the monitor can’t access the internal variable of
monitor but can call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.
Condition Variables
Two different operations are performed on the condition variables of the monitor.
Wait.
signal.
Wait operation
x.wait() : Process performing wait operation on any condition variable are suspended.
The suspended processes are placed in block queue of that condition variable.
Note: Each condition variable has its unique block queue.
Signal operation
x.signal(): When a process performs signal operation on condition variable, one of
the blocked processes is given chance.
If (x block queue empty)
// Ignore signal
else
// Resume a process from block queue.
B.V.A.SWAMY 74 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.5
Topic:2.5 Deadlocks
Principles of Deadlocks
In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly process
2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in deadlock as each
of them needs the other’s resource to complete their execution but neither of them is willing to relinquish
their resources.
Coffman Conditions
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not mutually
exclusive.
• Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the diagram
below, there is a single instance of Resource 1 and it is held by Process 1 only.
B.V.A.SWAMY 75 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.6
• No Preemption
A resource cannot be preempted from a process by force. A process can only release a
resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process
1. It will only be released when Process 1 relinquishes it voluntarily after its execution is
complete.
• Circular Wait
A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource held
by the first process. This forms a circular chain. For example: Process 1 is allocated Resource2
and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is requesting
Resource 2. This forms a circular wait loop.
Deadlock Detection
A deadlock can be detected by a resource scheduler as it keeps track of all the resources that are
allocated to different processes. After a deadlock is detected, it can be resolved using the following
methods −
• All the processes that are involved in the deadlock are terminated. This is not a good
approach as all the progress made by the processes is destroyed.
• Resources can be preempted from some processes and given to others till the deadlock is
resolved.
Deadlock Prevention
It is very important to prevent a deadlock before it can occur. So, the system checks each transaction
before it is executed to make sure it does not lead to deadlock. If there is even a slight chance that a
transaction may lead to deadlock in the future, it is never allowed to execute.
Deadlock Avoidance
It is better to avoid a deadlock rather than take measures after the deadlock has occurred. The wait for
graph can be used for deadlock avoidance. This is however only useful for smaller databases as it can
get quite complex in larger databases.
B.V.A.SWAMY 76 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.4
● Resource categories may include memory, printers, CPUs, open files, tape drives, CD-ROMS, etc.
● By definition, all the resources within a category are equivalent, and a request of this category can
be equally satisfied by any one of the resources in that category. If this is not the case ( i.e. if there is
some difference between the resources within a category ), then that category needs to be further
divided into separate categories. For example, "printers" may need to be separated into "laser
printers" and "color inkjet printers".
● In normal operation a process must request a resource before using it, and release it when it is
done, in the following sequence:
1. Request - If the request cannot be immediately granted, then the process must wait until the
resource(s) it needs become available.
Example: system calls open( ), malloc( ), new( ), and request( ).
2. Use - The process uses the resource.
Example: prints to the printer or reads from the file.
3. Release - The process relinquishes the resource. so that it becomes available for other processes.
close( ), free( ), delete( ), and release( ).
● For all kernel-managed resources, the kernel keeps track of what resources are free and which are
allocated, to which process they are allocated, and a queue of processes waiting for this resource to
become available. Application-managed resources can be controlled using mutexes or wait( ) and
signal( ) calls, ( i.e. binary or counting semaphores. )
● A set of processes is deadlocked when every process in the set is waiting for a resource that is
currently allocated to another process in the set ( and which can only be released when that other
waiting process makes progress).
Deadlock Characterization
Necessary Conditions:
There are four conditions that are necessary to achieve deadlock: 91
Mutual Exclusion - At least one resource must be held in a non-sharable mode; If any other process
requests this resource, then that process must wait for the resource to be released.
Hold and Wait - A process must be simultaneously holding at least one resource and waiting for at
least one resource that is currently being held by some other process.
No preemption - Once a process is holding a resource ( i.e. once its request has been granted ),
then that resource cannot be taken away from that process until the process voluntarily releases it.
Circular Wait - A set of processes { P0, P1, P2, . . ., PN } must exist such that every P[ i ] is waiting
for P[ ( i + 1 ) % ( N + 1 ) ]. ( Note that this condition implies the hold-and-wait condition, but it is
easier to deal with the conditions if the four are considered separately. )
B.V.A.SWAMY 77 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.7
Resource-Allocation Graph
In some cases deadlocks can be understood more clearly through the use of Resource-Allocation
Graphs, having the following properties:
*A set of resource categories, { R1, R2, R3, . . ., RN }, which appear as square nodes on the graph.
Dots inside the resource nodes indicate specific instances of the resource. ( E.g. two dots might
represent two laser printers. )
*A set of processes, { P1, P2, P3, . . ., PN }
*Request Edges - A set of directed arcs from Pi to Rj, indicating that process Pi has requested Rj,
and is currently waiting for that resource to become available.
*Assignment Edges - A set of directed arcs from Rj to Pi indicating that resource Rj has been
allocated to process Pi, and that Pi is currently holding resource Rj.
Note that a request edge can be converted into an assignment edge by reversing the direction of the
arc when the request is granted. ( However note also that request edges point to the category box,
whereas assignment edges emanate from a particular instance dot within the box. )
For example:
92
*If a resource-allocation graph contains no cycles, then the system is not deadlocked. ( When looking
for cycles, remember that these are directed graphs. ) See the example in Figure 7.2 above.
*If a resource-allocation graph does contain cycles AND each resource category contains only a
single instance, then a deadlock exists.
*If a resource category contains more than one instance, then the presence of a cycle in the
resourceallocation graph indicates the possibility of a deadlock, but does not guarantee one.
Consider, for
example, Figures 7.3 and 7.4 below:
B.V.A.SWAMY 78 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8
• Elimination of “Hold and Wait” Condition: There are two possibilities for elimination of the
second condition. The first alternative is that a process request be granted all of the resources it
needs at once, prior to execution. The second alternative is to disallow a process from requesting
resources whenever it has previously allocated resources. This strategy requires that all of the
resources a process will need must be requested at once. The system must grant resources on “all or
none” basis. If the complete set of resources needed by a process is not currently available, then the
process must wait until the complete set is available. While the process waits, however, it may not
hold any resources. Thus the “wait for” condition is denied and deadlocks cannot occur. This strategy
can lead to serious waste of resources.
• Elimination of “Circular Wait” Condition: The last condition, the circular wait, can be denied by
imposing a total ordering on all of the resource types and then forcing, all processes to request the
resources in order (increasing or decreasing). This strategy impose a total ordering of all resources
types, and to require that each process requests resources in a numerical order (increasing or
decreasing) of enumeration. With this rule, the resource allocation graph can never have a cycle.
Now the rule is this: processes can request resources whenever they want to, but all requests must
be made in numerical order. A process may request first printer and then a HDD(order: 2, 4), but it
may not request first a optical driver and then a printer (order: 3, 2). The problem with this strategy is
that it may be impossible to find an ordering that satisfies everyone.
B.V.A.SWAMY 79 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.2
This approach to the deadlock problem anticipates deadlock before it actually occurs. This approach
employs an algorithm to access the possibility that deadlock could occur and acting accordingly. If the
necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by being careful
when resources are allocated. It employs the most famous deadlock avoidance algorithm that is the
Banker’s algorithm.
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.
Suppose that process Pi requests resource Rj. The request can be granted only if converting the
request edge Pi Rj to an assignment edge Rj Pi that does not result in the formation of a
cycle in the resource-allocation graph. An algorithm for detecting a cycle in this graph is called cycle
detection algorithm.
If no cycle exists, then the allocation of the resource will leave the system in a safe state. If a cycle is
found, then the allocation will put the system in an unsafe state. Therefore, process Pi will have to
wait for its requests to be satisfied.
B.V.A.SWAMY 80 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.2
Banker's algorithm
The Banker's algorithm is a resource allocation & deadlock avoidance algorithm developed by
Edsger Dijkstra that test for safety by simulating the allocation of pre-determined maximum possible
amounts of all resources. Then it makes a "safe-state" check to test for possible deadlock conditions
for all other pending activities, before deciding whether allocation should be allowed to continue.
The Banker's algorithm is run by the operating system whenever a process requests resources. The
algorithm prevents deadlock by denying or postponing the request if it determines that accepting the
request could put the system in an unsafe state (one where deadlock could occur).
Several data structures must be maintained to implement the banker's algorithm. These data
structures encode the state of the resource-allocation system. Let n be the number of processes in
the system and m be the number of resource types.
Available: A vector of length m indicates the number of available resources of each type. If
Available[j] = k, there are k instances of resource type Rj available.
Max: An n x m matrix defines the maximum demand of each process. If Max[i,j] = k, then process Pi
may request at most k instances of resource type Rj.
Allocation: An n x m matrix defines the number of resources of each type currently allocated to each
process. If
Allocation[i,j] = k, then process Pi is currently allocated k instances of resource type Rj.
Need: An n x m matrix indicates the remaining resource need of each process. If Need[i,j] = k, then
process Pi may need k more instances of resource type Ri to complete its task. Note that Need[i,j] =
Max[i,j] - Allocafion[i,j].
These data structures vary over time in both size and value. The vector Allocationi specifies the
resources currently allocated to process Pi; the vector Needi specifies the additional resources that
process Pi may still request to complete its task.
B.V.A.SWAMY 81 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.2
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as follows:
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.
2. If Requesti < Available, go to step 3. Otherwise, Pi must wait, since the resources are not
available.
3. Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows:
If the resulting resource-allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However, if the new state is unsafe, then Pi must wait for Requesti and the old
resource-allocation state is restored.
Consider a system with five processes P0 through P4 and three resource types A,B,C. Resource type
A has 10 instances, resource type B has 5 instances, and resource type C has 7 instances. Suppose
that, at time T0, the following snapshot of the system has been taken:
B.V.A.SWAMY 82 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.2
that the sequence <PI, P3, P4, P0, P2> satisfies our safety requirement. Hence, we can immediately
However, that when the system is in this state, a request for (3,3,0) by P4 cannot be granted, since
the resources are not available. A request for (0,2,0) by Po cannot be granted, even though the
B.V.A.SWAMY 83 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.3
1. Single Instance of Each Resource Type: If all resources have only a single
instance, then it can define a deadlock detection algorithm that uses a variant of the
resource-allocation graph (is called a wait-for graph).
A wait–for graph can be draw by removing the nodes of type resource and collapsing the appropriate
edges from the resource-allocation graph.
An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj to release a
resource that Pi needs. An edge Pi Pj exists in a wait-for graph if and only if the corresponding
resource allocation graph contains two edges Pi Rq and Rq Pj for some resource Rq. For
Example:
A deadlock exists in the system if and only if the wait-for graph contains a cycle. To detect deadlocks,
the system needs to maintain the wait-for graph and periodically to invoke an algorithm that searches
for a cycle in the graph. An algorithm to detect a cycle in a graph requires an order of n2 operations,
where n is the number of vertices in the graph.
B.V.A.SWAMY 84 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 2.8.4
Process Termination
There are two method to eliminate deadlocks by terminating a process as follows:
1.Abort all deadlocked processes: This method will break the deadlock cycle clearly by terminating
all process. This method is cost effective. And it removes the partial computations completed by the
processes.
2.Abort one process at a time until the deadlock cycle is eliminated: This method terminates one
process at a time, and invokes a deadlock-detection algorithm to determine whether any processes
are still deadlocked.
Resource Preemption
In resource preemption, the operator or system preempts some resources from processes and give
these resources to other processes until the deadlock cycle is broken.
If preemption is required to deal with deadlocks, then three issues need to be addressed:
1. Selecting a victim: The system or operator selects which resources and which processes are to
be preempted based on cost factor.
2. Rollback: The system or operator must roll back the process to some safe state and restart it from
that state.
3. Starvation: The system or operator should ensure that resources will not always be preempted
from the same process?
B.V.A.SWAMY 85 | P a g e
DEPT. OF COMPUTER SCIENCE
Starvation:
Starvation is the problem that occurs when high priority processes keep
executing and low priority processes get blocked for indefinite time. In heavily
loaded computer system, a steady stream of higher-priority processes can
prevent a low-priority process from ever getting the CPU. In starvation resources
are continuously utilized by high priority processes. Problem of starvation can be
resolved using Aging. In Aging priority of long waiting processes is gradually
increased.
B.V.A.SWAMY 86 | P a g e
DEPT. OF COMPUTER SCIENCE
B.V.A.SWAMY 87 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT – III
TOPICS
3.2 Swapping, 91
3.4 Paging, 94
B.V.A.SWAMY 88 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.1
UNIT – III
Topic: 3.1 Logical Versus Physical Address
Logical Versus Physical Address Space
• The concept of a logical address space that is bound to a separate physicaladdress space
is central to proper memory management.
o Logical address – address generated by the CPU; also referred to as virtual
address.
o Physical address – address seen by the memory unit.
• The set of all logical addresses generated by a program is a logical address space; the set
of all physical addresses corresponding to these logical addresses are a physical address
space.
• Logical and physical addresses are the same in compile-time and load-time address-binding
schemes; logical (virtual) and physical addresses differ in execution-time address-binding
scheme.
• The run-time mapping from virtual to physical addresses is done by a hardware device called the
memory management unit (MMU).
• This method requires hardware support slightly different from the hardware configuration. The
base register is now called a relocation register. The value in the relocation register is added to
every address generated by a user process at the time it is sent to memory.
• The user program never sees the real physical addresses. The program can create a pointer to
location 346, store it in memory, manipulate it and compare it to other addresses. The user
program deals with logical addresses. The memory mapping hardware converts logical addresses
into physical addresses. The final location of a referenced memory address is not determined
until the reference is made.
B.V.A.SWAMY 89 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.1
Dynamic Loading
• The main program is loaded into memory and is executed. When a routine needs to call another
routine, the calling routine first checks to see whether the other the desired routine into memory
and to update the program’s address tables to reflect this change. Then control is passed to the
newly loaded routine.
• Useful when large amounts of code are needed to handle infrequently occurring cases.
• No special support from the operating system is required.
Dynamic Linking
• Small piece of code, stub, is used to locate the appropriate memory-resident library routine, or to
load the library if the routine is not already present.
• When this stub is executed, it checks to see whether the needed routine is already in memory. If
not, the program loads the routine into memory.
• Stub replaces itself with the address of the routine, and executes the routine.
• Thus the next time that code segment is reached, the library routine is executed directly,
incurring no cost for dynamic linking.
B.V.A.SWAMY 90 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.2
• Backing store – fast disk large enough to accommodate copies of all memory images for all
users; must provide direct access to these memory images. It must be large enough to accommodate
copies of all memory images for all users, and it must provide direct access to these memory images.
The system maintains a ready queue consisting of all processes whose memory images are scheduler
decides to execute a process it calls the dispatcher. The dispatcher checks to see whether the next
process in the queue is in memory. If not, and there is no free memory region, the dispatcher swaps
out a process currently in memory and swaps in the desired process. It then reloads registers as
normal and transfers control to the selected process.
• Major part of swap time is transfer time; total transfer time is directly proportional to the
amount of memory swapped.
• Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows).
B.V.A.SWAMY 91 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.3
o Resident operating system, usually held in low memory with interrupt vector.
• Multiple-partition allocation
o Hole – block of available memory; holes of various size are scattered throughout
memory.
o When a process arrives, it is allocated memory from a hole large enough to
accommodate it.
o Operating system maintains information about:
o A set of holes of various sizes, is scattered throughout memory at any given time.
When a process arrives and needs memory, the system searches this set for a hole that is
large enough for this process. If the hole is too large, it is split into two: one part is
allocated to the arriving process; the other is returned to the set of holes. When a process
terminates, it releases its block of memory, which is then placed back in the set of holes.
If the new hold is adjacent to other holes, these adjacent holes are merged to form one
larger hole.
B.V.A.SWAMY 92 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.3
• This procedure is a particular instance of the general dynamic storage allocation problem,
which is how to satisfy a request of size n from a list of free holes. There are many solutions
to this problem. The set of holes is searched to determine which hole is best to allocate. The
first-fit, best-fit and worst-fit strategies are the most common ones used to select a free hole
from the set of available holes.
o Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless
ordered by size.
o Worst-fit: Allocate the largest hole; must also search entire list.
Fragmentation
• External Fragmentation – total memory space exists to satisfy a request, but it is not
contiguous.
• Internal Fragmentation – allocated memory may be slightly larger than requested memory; this
size difference is memory internal to a partition, but not being used.
o Shuffle memory contents to place all free memory together in one large block.
B.V.A.SWAMY 93 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.4
• Divide physical memory into fixed-sized blocks called frames (size is power of 2, for example
512 bytes).
• Divide logical memory into blocks of same size called pages. When a process is to be executed,
its pages are loaded into any available memory frames from the backing store. The backing store
is divided into fixed sized blocks that are of the same size as the memory frames.
• Every address generated by the CPU is divided into two parts: a page number (p) and a page
offset (d). The page number is used as an index into a page table. The page table contains the
base address of each page in physical memory. This base address is combined with the page
offset to define the physical memory address that is sent to the memory unit.
• The paging model of memory is shown in below figure. The page size is defined by the
hardware. The size of a page is typically of a power of 2, varying between 512 bytes and 16 MB
per page, depending on the computer architecture. The selection of a power of 2 as a page size
makes the translation of a logical address into a page number and page offset particularly easy. If
the size of logical address is 2m, and a page size is 2n addressing units, then the high order m-n
bits of a logical address designate the page number, and the n low order bits designate the page
offset.
B.V.A.SWAMY 94 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.4
• To run a program of size n pages, need to find n free frames and load program.
Internal fragmentation may occur. Let us take an example. Suppose a program needs 32 KB
memory for allocation. The whole program is divided into smaller units assuming 4 KB and is
assigned some address. The address consists of two parts such as:
The numbers allocated to pages are typically in power of 2 to simplify extraction of page numbers
and offsets. To access a piece of data at a given address, the system first extracts the page number
and the offset. Then it translates the page number to physical page frame and access data at offset in
physical page frame. At this moment, the translation of the address by the OS is done using a page
table. Page table is a linear array indexed by virtual page number which provides the physical page
frame that contains the particular page. It employs a lookup process that extracts the page number
and the offset. The system in addition checks that the page number is within the address space of
process and retrieves the page number in the page table. Physical address will calculated by using the
formula.
When a process arrives in the system to be executed, its size expressed in pages is examined. Each
page of the process needs one frame. Thus if the process requires n pages, at least n frames must be
available in memory. If n frames are available, they are allocated to this arriving process. The first
page of the process is loaded into one of the allocated frames, and the frame number is put in the
page table for this process. The next page is loaded into another frame, and its frame number is put
into the page table and so on as in below figure. An important aspect of paging is the clear
separation between the user’s view of memory and the actual physical memory. The user program
views that memory as one single contiguous space, containing only this one program. In fact, the
user program is scattered throughout physical memory, which also holds other programs. The
difference between the user’s view of memory and the actual physical memory is reconciled by the
address-translation hardware. The logical addresses are translated into physical addresses. This
mapping is hidden from the user and is controlled by the operating system.
B.V.A.SWAMY 96 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.4
• The TLB contains only a few of the page table entries. When a logical address is generated by
the CPU, its page number is presented to the TLB. If the page number is found, its frame
number is immediately available and is used to access memory. The whole task may take less
than 10 percent longer than it would if an unmapped memory reference were used.
• If the page number is not in the TLB (known as a TLB miss), a memory reference to the
page table must be made. When the frame number is obtained, we can use it to access
memory.
Hit Ratio
• Hit Ratio: the percentage of times that a page number is found in the associative registers.
• For example, if it takes 20 nanoseconds to search the associative memory and 100 nanoseconds
to access memory; for a 98-percent hit ratio, we have
= 122 nanoseconds.
• The Intel 80486 CPU has 32 associative registers, and claims a 98-percent hit ratio.
B.V.A.SWAMY 97 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.4
o “Valid” indicates that the associated page is in the process’ logical address space, and is
thus a legal page.
o “Invalid” indicates that the page is not in the process’ logical address space.
• Pay attention to the following figure. The program extends to only address 10,468, any
reference beyond that address is illegal. However, references to page 5 are classified as valid,
so accesses to addresses up to 12,287 are valid. This reflects the internal fragmentation of
paging.
B.V.A.SWAMY 98 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic:3.5
Where p1 is an index into the outer page table, and p2 is the displacement within the page of the
outer page table. The below figure shows a two level page table scheme.
Address-translation scheme for a two-level 32-bit paging architecture is shown in below figure
B.V.A.SWAMY 99 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.5
B.V.A.SWAMY 100 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.5
Each virtual address in the system consists of a triple <process-id, page-number, offset>. Each
inverted page table entry is a pair <process-id, page-number> where the process-id assumes the role
of the address space identifier. When a memory reference occurs, part of the virtual address,
consisting of <process-id, page-number>, is presented to the memory subsystem. The inverted page
table is then searched for a match. If a match is found say at entry i, then the physical address <i,
offset> is generated. If no match is found, then an illegal address access has been attempted.
Shared Page:
• Shared code
o One copy of read-only (reentrant) code shared among processes (i.e., text editors,
compilers, window systems).
o Shared code must appear in same location in the logical address space of all processes.
o The pages for the private code and data can appear anywhere in the logical address
space.
Reentrant code or pure code is non self modifying code. If the code is reentrant, then it never
changes during execution. Thus, two or more processes can execute the same code at the same time.
Each process has its own copy of registers and data storage to hold the data for the process’
execution. The data for two different processes will of course vary for each process.
B.V.A.SWAMY 101 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.6
Segmentation
• Segmentation is a memory management scheme that supports this user view of memory.
• A logical address space is a collection of segments. Each segment has a name and a length.
• The addresses specify both the segment name and the offset within the segment.
• The user therefore specifies each address by two quantities such as segment name and an offset.
For simplicity of implementation, segments are numbered and are referred to by a segment
number, rather than by a segment name.
B.V.A.SWAMY 102 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.6
• Segment table – maps two-dimensional physical addresses; each table entry has:
o Base – contains the starting physical address where the segments reside in memory.
• Segment-table base register (STBR) points to the segment table’s location in memory.
• When the user program is compiled by the compiler it constructs the segments.
• The loader takes all the segments and assigned the segment numbers.
• The mapping between the logical and physical address using the segmentation technique is
shown in above figure.
• Each entry in the segment table as limit and base address.
• The base address contains the starting physical address of a segment where the limit address
specifies the length of the segment.
• The logical address consists of 2 parts such as segment number and offset.
• The segment number is used as an index into the segment table. Consider the below
example is given below.
B.V.A.SWAMY 103 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.6
• The IBM OS/2 is an operating system of the Intel-386 architecture. In this technique both
segment table and page table is required.
• The program consists of various segments given by the segment table where the segment table
contains different entries one for each segment.
Then each segment is divided into a number of pages of equal size whose information is maintained
in a separate page table.
• If a process has four segments that is 0 to 3 then there will be 4 page tables for that process, one
for each segment.
• The size fixed in segmentation table (SMT) gives the total number of pages and therefore
maximum page number in that segment with starting from 0.
• If the page table or page map table for a segment has entries for page 0 to 5.
• The address of the entry in the PMT for the desired page p in a given segment s can be obtained
by B + P where B can be obtained from the entry in the segmentation table.
Using the address (B +P) as an index in page map table (page table), the page frame (f) can be
obtained and physical address can be obtained by adding offset to page frame.
B.V.A.SWAMY 104 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.7
Virtual Memory
• It is a technique which allows execution of process that may not be compiled within the primary
memory.
• It separates the user logical memory from the physical memory. This separation allows an
extremely large memory to be provided for program when only a small physical memory is
available.
• Virtual memory makes the task of programming much easier because the programmer no longer
needs to working about the amount of the physical memory is available or not.
• The virtual memory allows files and memory to be shared by different processes by page
sharing.
B.V.A.SWAMY 105 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.8
Demand Paging
A demand paging system is similar to the paging system with swapping feature. When we want to
execute a process we swap it into the memory. A swapper manipulates entire process where as a
pager is concerned with the individual pages of a process. The demand paging concept is using pager
rather than swapper. When a process is to be swapped in, the pager guesses which pages will be used
before the process is swapped out again. Instead of swapping in a whole process, the pager brings
only those necessary pages into memory. The transfer of a paged memory to contiguous disk space is
shown in below figure.
Thus it avoids reading into memory pages that will not used any way decreasing the swap time and
the amount of physical memory needed. In this technique we need some hardware support to
distinct between the pages that are in memory and those that are on the disk. A valid and invalid bit
is used for this purpose. When this bit is set to valid it indicates that the associate page is in memory.
If the bit is set to invalid it indicates that the page is either not valid or is valid but currently not in
the disk.
B.V.A.SWAMY 106 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.8
Marking a page invalid will have no effect if the process never attempts to access that page. So while
a process executes and access pages that are memory resident, execution proceeds normally. Access
to a page marked invalid causes a page fault trap. It is the result of the OS’s failure to bring the
desired page into memory.
• We check an internal table (page table) for this process to determine whether the reference was
valid or invalid.
• If the reference was invalid, we terminate the process, if it was valid but not yet brought in, we
have to bring that from main memory.
• Then we read the desired page into the newly allocated frame.
• When the disk read is complete, we modify the internal table to indicate that the page is now in
memory.
We restart the instruction that was interrupted by the illegal address trap. Now the process can access
the page as if it had always been in memory
B.V.A.SWAMY 107 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.9
Page Replacement
• Each process is allocated frames (memory) which hold the process’s pages (data)
• Frames are filled with pages as needed – this is called demand paging
•
Over-allocation of memory is prevented by modifying the page-fault service routine to replace
pages
• The job of the page replacement algorithm is to decide which page gets victimized to make room
for a new page
• Procedure: replace the page that will not be used for the longest time (or at all) – i.e. replace the
page with the greatest forward distance in the reference string
• Analysis: 12 page references, 6 page faults, 2 page replacements. Page faults per number of
frames = 6/4 = 1.5
• Unfortunately, the optimal algorithm requires special hardware (crystal ball, magic mirror, etc.)
not typically found on today’s computers
• Optimal algorithm is still used as a metric for judging other page replacement algorithms
B.V.A.SWAMY 108 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.9
• Analysis: 12 page references, 10 page faults, 6 page replacements. Page faults per number of
frames = 10/4 = 2.5
_ = faulting page 12 12 12 12 22 22 22 32 32 32 32
n = reference count 13 13 13 13 15 15 15 23 23 25
14 14 14 14 14 14 14 24 24
• At the 7th page in the reference string, we need to select a page to be victimized. Either 3 or 4
will do since they have the same reference count (1). Let’s pick 3.
• Likewise at the 10th page reference; pages 4 and 5 have been referenced once each. Let’s pick
page 4 to victimize. Page 3 is brought in, and its reference count (which was 1 before we paged it
out a while ago) is updated to 2.
• Analysis: 12 page references, 7 page faults, 3 page replacements. Page faults per number of
frames = 7/4 = 1.75
B.V.A.SWAMY 109 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.9
• Need to keep a reference count for each frame which is initialized to 1 when the page is paged in,
incremented every time the page in the frame is referenced, and reset every time the page in the
frame is replaced
_ = faulting page 12 12 12 12 22 22 22 32 32 32 32
n = reference count 13 13 13 13 15 15 15 13 13 15
14 14 14 14 14 14 14 24 24
• At the 7th reference, we victimize the page in the frame which has been referenced least often --
in this case, pages 3 and 4 (contained within frames 3 and 4) are candidates, each with a reference
count of 1. Let’s pick the page in frame 3. Page 5 is paged in and frame 3’s reference count is reset
to 1.
• At the 10th reference, we again have a page fault. Pages 5 and 4 (contained within frames 3 and
4are candidates, each with a count of 1. Let’s pick page 4. Page 3 is paged into frame 3, and frame 3’s
reference count is reset to 1.
• Analysis: 12 page references, 7 page faults, 3 page replacements. Page faults per number of
frames = 7/4 = 1.75
LRU algorithm
• Replaces pages based on their most recent reference – replace the page with the greatest
backward distance in the reference string
• Analysis: 12 page references, 8 page faults, 4 page replacements. Page faults per number of
frames = 8/4 = 2
• One possible implementation (not necessarily the best):
o Every frame has a time field; every time a page is referenced, copy the current time into
its frame’s time field.
o When a page needs to be replaced, look at the time stamps to find the oldest
B.V.A.SWAMY 110 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.10
• Thrashing results in severe performance problems. Consider the following scenario, which is
based on the actual behaviour of early paging systems. The operating system monitors CPU
utilization. If CPU utilization is too low, we increase the degree of multiprogramming by
introducing a new process to the system. A global page replacement algorithm is used; it
replaces pages with no regard to the process to which they belong. Now suppose that a
process enters a new phase in its execution and needs more frames.
B.V.A.SWAMY 111 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT - III
File System Implementation
File System Implementation: Page No
B.V.A.SWAMY 112 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.11
File System
1. File attributes: A file has certain attributes which vary from one operating system to another.
• Identifier: It is unique number that identifies the file within the file system.
• Type: This information is needed for those systems that support different types of files.
• Location: It is a pointer to a device & to the location of the file on that device
• Protection: It is the access control information that determines who can read, write &
execute a file.
• Time, date & user identification: It gives information about time of creation or last
modification & last use.
2. File operations: The operating system can provide system calls to create, read, write, reposition,
delete and truncate files.
• Creating files: Two steps are necessary to create a file. First, space must be found for the
file in the file system. Secondly, an entry must be made in the directory for the new file.
Reading a file: Data & read from the file at the current position. The system must keep a read
pointer to know the location in the file from where the next read is to take place. Once the read has
been taken place, the read pointer is updated.
• Writing a file: Data are written to the file at the current position. The system must keep a
write pointer to know the location in the file where the next write is to take place. The write
pointer must be updated whenever a write occurs.
• Repositioning within a file (seek): The directory is searched for the appropriate entry &
the current file position is set to a given value. After repositioning data can be read from or
written into that position.
B.V.A.SWAMY 113 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.11
• Deleting a file: To delete a file, we search the directory for the required file. After deletion,
the space is releasedso that it can be reused by other files.
• Truncating a file: The user may erase the contents of a file but allows all attributes to
remain unchanged expect the file length which is rest to ‘O’ & the space is released.
3. File types: The file name is spilt into 2 parts, Name & extension. Usually these two parts are
separated by a period. The user & the OS can know the type of the file from the extension itself.
Listed below are some file types along with their extension:
Archieve File arc, zip, tar (related files grouped together into file compressed for
storage)
Multimedia File mpeg (Binary file containing audio or A/V information)
• Byte sequence:The figure shows an unstructured sequence of bytes. The OS doesn’t care
about the content of file. It only sees the bytes. This structure provides maximum flexibility.
Users can write anything into their files & name them according to their convenience. Both
UNIX & windows use this approach.
byte
· Record sequence: In this structure, a file is a sequence of fixed length records. Here the
read operation returns one records & the write operation overwrites or append or record.
B.V.A.SWAMY 114 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.11
Record
• Tree:In this organization, a file consists of a tree of records of varying lengths. Each record
consists of a key field. The tree is stored on the key field to allow first searching for a
particular key.
• Sequential access: It is the simplest access method. Information in the file is processed in
order i.e. one record after another. A process can read all the data in a file in order starting
from beginning but can’t skip & read arbitrarily from any location. Sequential files can be
rewound. It is convenient when storage medium was magnetic tape rather than disk.
Direct access: A file is made up of fixed length-logical records that allow programs to read
& write records rapidly in no particular O order. This method can be used when disk are
used for storing files. This method is used in many applications e.g. database systems. If an
airline customer wants to reserve a seat on a particular flight, the reservation program must
be able to access the record for that flight directly without reading the records before it. In a
direct access file, there is no restriction in the order of reading or writing. For example, we
can read block 14, then read block 50 & then write block 7 etc. Direct access files are very
useful for immediate access to large amount of information.
B.V.A.SWAMY 115 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.12
Types of access: In case of systems that don’t permit access to the files of other users. Protection
is not needed. So, one extreme is to provide protection by prohibiting access. The other extreme is
to provide free access with no protection. Both these approaches are too extreme for general use.
So, we need controlled access. It is provided by limiting the types of file access. Access is permitted
depending on several factors. One major factor is type of access requested. The different type of
operations that can be controlled are:
• Read
• Write
• Execute
• Append
• Delete
• List
Access lists and groups:
Various users may need different types of access to a file or directory. 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:
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.
B.V.A.SWAMY 116 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.13
Operation on a directory:
• Search for a file: We need to be able to search a directory for a particular file.
• Create a file: New files are created & added to the directory.
• Delete a file: When a file is no longer needed, we may remove it from the directory.
• Rename a file: The name of a file is changed when the contents of the file changes.
• Traverse the file system: It is useful to be able to access every directory & every file
within a directory.
Structure of a directory: The most common schemes for defining the structure of the directory
are:
1. Single level directory: It is the simplest directory structure. All files are present in the same
directory. So it is easy to manage & understand.
Limitation: A single level directory is difficult to manage when the no. of files increases or
when there is more than one user. Since all files are in same directory, they must have unique
names. So, there is confusion of file names between different users.
2. Two level directories: The solution to the name collision problem in single level directory is to
create a separate directory for each user. In a two level directory structure, each user has its own
user file directory. When a user logs in, then master file directory is searched. It is indexed by
user name & each entry points to the UFD of that user.
Limitation: It solves name collision problem. But it isolates one user from another. It is an
advantage when users are completely independent. But it is a disadvantage when the users need
to access each other’s files & co-operate among themselves on a particular task.
B.V.A.SWAMY 117 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.13
Tree structured directories: It is the most common directory structure. A two level directory
is a two level tree. So, the generalization is to extend the directory structure to a tree of arbitrary
height. It allows users to create their own subdirectories & organize their files. Every file in the
system has a unique path name. It is the path from the root through all the sub-directories to a
specified file. A directory is simply another file but it is treated in a special way. One bit in each
directory entry defines the entry as a file (O) or as sub- directories. Each user has a current
directory. It contains most of the files that are of current interest to the user. Path names can be
of two types: An absolute path name begins from the root directory & follows the path down to
the specified files. A relative path name defines the path from the current directory. E.g. If the
current directory is root/spell/mail, then the relative path name is prt/first & the absolute path
name is root/ spell/ mail/ prt/ first. Here users can access the files of other users also by
specifying their path names.
Limitation: Now a file may have multiple absolute path names. So, distinct file names may refer
to the same file. Another problem occurs during deletion of a shared file. When a file is removed
by any one user. It may leave dangling pointer to the non existing file. One serious problem in a
cyclic graph structure is ensuring that there are no cycles. To avoid these problems, some
systems do not allow shared directories or files. E.g. MS-DOS uses a tree structure rather than a
cyclic to avoid the problems associated with deletion. One approach for deletion is to preserve
the file until all references to it are deleted. To implement this approach, we must have some
mechanism for determining the last reference to the file. For this we have to keep a list of
reference to a file. But due to the large size of the no. of references. When the count is zero, the
file can be deleted.
B.V.A.SWAMY 118 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.13
5. General graph directory: When links are added to an existing tree structured
directory, the tree structure is destroyed, resulting in a simple graph structure. Linking is a
technique that allows a file to appear in more than one directory. The advantage is the
simplicity of algorithm to transverse the graph & determines when there are no more
references to a file. But a similar problem exists when we are trying to determine when a file
can be deleted. Here also a value zero in the reference count means that there are no more
references to the file or directory & the file can be deleted. But when cycle exists, the
reference count may be non-zero even when there are no references to the directory or file.
This occurs due to the possibility of self referencing (cycle) in the structure. So, here we have
to use garbage collection scheme to determine when the last references to a file has been
deleted & the space can be reallocated. It involves two steps:
• Transverse the entire file system & mark everything that can be accessed.
6. But this process is extremely time consuming. It is only necessary due to presence of cycles in the
graph. So, a cyclic graph structure is easier to work than this.
B.V.A.SWAMY 119 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.14
1. Contiguous allocation:
b. Number of disk seeks required for accessing contiguously allocated file is minimum.
c. The IBM VM/CMS OS uses contiguous allocation. Contiguous allocation of a file is defined
by the disk address and length (in terms of block units).
d. If the file is ‘n’ blocks long and starts all location ‘b’, then it occupies blocks b, b+1, b+2,----
e. The directory for each file indicates the address of the starting block and the length of the
area allocated for each file.
f. Contiguous allocation supports both sequential and direct access. For sequential access, the
file system remembers the disk address of the last block referenced and reads the next block
when necessary.
g. For direct access to block i of a file that starts at block b we can immediately access block b
+ i.
Problems: One difficulty with contiguous allocation is finding space for a new file. It also
suffers from the problem of external fragmentation. As files are deleted and allocated, the
free disk space is broken into small pieces. A major problem in contiguous allocation is how
much space is needed for a file. When a file is created, the total amount of space it will need
must be found and allocated. Even if the total amount of space needed for a file is known in
advances, pre-allocation is inefficient. Because a file that grows very slowly must be allocated
enough space for its final size even though most of that space is left unused for a long period
time. Therefore, the file has a large amount of internal fragmentation.
2. Linked Allocation:
b. In linked allocation, each file is linked list of disk blocks, which are scattered throughout the
disk.
B.V.A.SWAMY 120 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.14
c. The directory contains a pointer to the first and last blocks of the file.
e. These pointers are not accessible to the user. To create a new file, we simply create a new
entry in the directory.
f. For writing to the file, a free block is found by the free space management system and this
new block is written to & linked to the end of the file.
g. To read a file, we read blocks by following the pointers from block to block.
h. There is no external fragmentation with linked allocation & any free block can be used to
satisfy a request.
i. Also there is no need to declare the size of a file when that file is created. A file can continue
to grow as long as there are free blocks.
j. Limitations: It can be used effectively only for sequential access files. To find the ‘ i ' th
block of the file, we must start at the beginning of that file and follow the pointers until we
get the ith block. So it is inefficient to support direct access files. Due to the presence of
pointers each file requires slightly more space than before. Another problem is reliability.
Since the files are linked together by pointers scattered throughout the disk. What would
happen if a pointer were lost or damaged.
3. Indexed Allocation:
a. Indexed allocation solves the problem of linked allocation by bringing all the
pointers together to one location known as the index block.
b. Each file has its own index block which is an array of disk block addresses. The ith
entry in the index block points to the ith block of the file.
b. The directory contains the address of the index block. The read the ith block, we use the
pointer in the ith index block entry and read the desired block.
c. To write into the ith block, a free block is obtained from the free space manager and its
address is put in the ith index block entry.
e. Limitations: The pointer overhead of index block is greater than the pointer overhead of
linked allocation. So here more space is wasted than linked allocation. In indexed allocation,
an entire index block must be allocated, even if most of the pointers are nil.
B.V.A.SWAMY 121 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.15
Since there is only a limited amount of disk space, it is necessary to reuse the space from the deleted
files. To keep track of free disk space, the system maintains a free space list. It records all the disk
blocks that are free i.e. not allocated to some file or dictionary. To create a file, we search the free
space list for the required amount of space and allocate it to the new file. This space is then removed
from the free space list. When a file is deleted, its disk space is added to the free space list.
Implementation:
There are 4 ways to implement the free space list such as:
• Bit Vector: The free space list is implemented as a bit map or bit vector. Each block is
represented as 1 bit. If the block is free, the bit is 1 and if it is allocated then the bit is 0. For
example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26 & 27 are free
and rest of the blocks are allocated. The free space bit map would be
0011110011111100011000000111……………………..
The main advantage of this approach is that it is simple and efficient to find the first free block
or n consecutive free blocks on the disk. But bit vectors are inefficient unless the entire vector is
kept in main memory. It is possible for smaller disks but not for larger ones.
Linked List: Another approach is to link together all the free disk blocks and keep a pointer to the
first free block. The first free block contains a pointer to the next free block and so on. For example,
we keep a pointer to block 2 as the free block. Block 2 contains a pointer to block which points to
block 4 which then points to block 5 and so on. But this scheme is not efficient. To traverse the list,
we must read each block which require a lot of I/O time
• Grouping: In this approach, we store the address of n free blocks in the first free block. The
first n-1 of these blocks is actually free. The last block contains the address of another n free
blocks and so on. Here the addresses of a large number of free blocks can be found out quickly.
Counting: Rather than keeping a list of n free disk block addresses, we can keep the address of the
first free block and the number of free contiguous blocks. So here each entry in the free space list
consists of a disk address and a count.
B.V.A.SWAMY 122 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT -III
Mass Storage Structure
Topics Page NO
B.V.A.SWAMY 123 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.16
x Disks provide a bilk of secondary storage. Disks come in various sizes, speed and
information can be stored optically or magnetically.
x Magnetic tapes were used early as secondary storage but the access time is less than disk.
x Modern disks are organized as single one-dimensional array of logical blocks. x The
actual details of disk i/o open depends on the computer system, OS, nature of i/o channels
and disk controller hardware.
x The basic unit of information storage is a sector. The sectors are stored on flat,
circular, media disk. This disk media spins against one or more read-write heads. The head
can move from
the inner portion of the disk to the outer portion.
x When the disk drive is operating the disks is rotating at a constant speed. x To read or
write the head must be positioned at the desired track and at the beginning if the desired
sector on that track. x Track selection involves moving the head in a movable head system
or electronically selecting one head on a fixed head system. x These characteristics are
common to floppy disks, hard disks, CD-ROM and DVD.
B.V.A.SWAMY 124 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.17
1. Seek Time:-Seek time is the time required to move the disk arm to the required track.
n = number of track traversed. m = constant that depends on the disk drive s = startup
time.
3. Rotational Delay :- Disks other than the floppy disk rotate at 3600 rpm which is one
revolution per 16.7ms.
total time between the first request for service and the completion of last transfer.
The amount of head movement needed to satisfy a series of i/o request can affect
the performance. If the desired drive and the controller are available the request can be serviced
immediately. If the device or controller is busy any new requests for service will be placed on the
queue of pending requests for that drive when one request is complete the OS chooses which
pending request to service next.
B.V.A.SWAMY 125 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.18
3. SSTF ( Shortest seek time first) algorithm:This selects the request with minimum seek
time from the current head position. Since seek time increases with the number of
cylinders traversed by head, SSTF chooses the pending request closest to the current
head position. Eg:-
:-consider a disk queue with request for i/o to blocks on cylinders. 98, 183, 37, 122, 14, 124,
65, 67
B.V.A.SWAMY 126 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.18
If the disk head is initially at 53, the closest is at cylinder 65, then 67, then 37 is closer
then 98 to 67. So it services 37, continuing we service 14, 98, 122, 124 and finally 183.
The total head movement is only 236 cylinders. SSTF is essentially a form of SJF and it
may cause starvation of some requests. SSTF is asubstantial improvement over FCFS, it
is not optimal.
1. SCAN algorithm:In this the disk arm starts at one end of the disk and moves towards the
other end, servicing the request as it reaches each cylinder until it gets to the other end of
the disk. At the other end, the direction of the head movement is reversed and servicing
continues. Eg:-:-consider a disk queue with request for i/o to blocks on cylinders. 98, 183,
C-SCAN is a variant of SCAN designed to provide a more uniform wait time. Like SCAN, C-
SCAN moves the head from end of the disk to the other servicing the request along the way. When
the head reaches the other end, it immediately returns to the beginning of the disk, without
servicing any request on the return. The C-SCAN treats the cylinders as circular list that wraps
B.V.A.SWAMY 127 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.18
Both SCAN and C-SCAN move the disk arm across the full width of the disk. In practice neither of
the algorithms is implemented in this way. The arm goes only as far as the final request in each
direction. Then it reverses, without going all the way to the end of the disk. These versions of
SCANand CSCAN are called Look and C-Look scheduling because they look for a request before
continuing to move in a given direction. Eg:
B.V.A.SWAMY 128 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.19
B.V.A.SWAMY 129 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 3.21
RAID Structure
Disk striping uses a group of disks as one storage unit schemes improve performance and
improve the reliability of the storage system by storing redundant data
• Mirroring or shadowing (RAID 1) keeps duplicate of each disk
• Striped mirrors (RAID 1+0) or mirrored stripes (RAID 0+1) provides high performance
and high reliability
• Block interleaved parity (RAID 4, 5, 6) uses much less redundancy
RAID within a storage array can still fail if the array fails, so automatic replication of the data
between arrays is common.Frequently, a small number of hot-spare disks are left unallocated,
automatically replacing a failed disk and having data rebuilt onto them.
• RAID – multiple disk drives provides reliability via redundancy. It is arranged into six
different levels.Several improvements in disk-use techniques involve the use of multiple
disks working cooperatively.Disk striping uses a group of disks as one storage unit.
• RAID schemes improve performance and improve the reliability of the
storage system by storing redundant data.
• Mirroring or shadowing keeps duplicate of each disk.
• Block interleaved parity uses much less redundancy.
• RAID alone does not prevent or detect data corruption or other errors, just disk
failures .Solaris ZFS adds checksums of all data and metadata Checksums kept with pointer
to object, to detect if object is the right one and whether it changed can detect and correct
data and metadata corruptionZFS also removes volumes, partititions .Disks allocated in
pools
Filesystems with a pool share that pool, use and release space like “malloc” and “free” memory allocate
/ release calls.
B.V.A.SWAMY 130 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT- 4
TOPICS
PROTECTION Page No
B.V.A.SWAMY 131 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 4.1
PRINCIPLES OF PROTECTION
DOMAIN OF PROTECTION
Domain Structure
B.V.A.SWAMY 132 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 4.1
• When file is executed and setuid = on, then user-id is set to owner of the file
being executed. When execution completes user-id is reset.
B.V.A.SWAMY 133 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 4.2
• Each column = Access-control list for one object Defines who can
Domain 3 Read
Fore each domain, what operations allowed on what objects. Object 1 – Read Object 4
– Read, Write, Execute
Object 5 – Read, Write, Delete, Copy
B.V.A.SWAMY 134 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 4.3
B.V.A.SWAMY 135 | P a g e
DEPT. OF COMPUTER SCIENCE
Removable Disks
• Floppy disk — thin flexible disk coated with magnetic material, enclosed in a protective
plastic case
• Most floppies hold about 1 MB; similar technology is used for removable disks
that hold more than 1 GB
• Removable magnetic disks can be nearly as fast as hard disks, but they are at
a greater risk of damage from exposure
• • A magneto-optic disk records data on a rigid platter coated with magnetic
material
• Laser heat is used to amplify a large, weak magnetic field to record a bit
• Laser light is also used to read data (Kerr effect)
• The magneto-optic head flies much farther from the disk surface than a
magnetic disk head, and the magnetic material is covered with a protective layer
of plastic or glass; resistant to head crashes
• Optical disks do not use magnetism; they employ special materials that are
altered by laser light
The data on read-write disks can be modified over and over
• WORM (“Write Once, Read Many Times”) disks can be written only once
• Thin aluminum film sandwiched between two glass or plastic platters
• To write a bit, the drive uses a laser light to burn a small hole through the
aluminum; information can be destroyed by not altered
• Very durable and reliable..
• Read-only disks, such ad CD-ROM and DVD, com from the factory with the data
pre-recorded Tapes
• Compared to a disk, a tape is less expensive and holds more data, but random
access is much slower.
• Tape is an economical medium for purposes that do not require fast
random access, e.g., backup copies of disk data, holding huge volumes of data
• Large tape installations typically use robotic tape changers that move tapes between
tape drives and storage slots in a tape library
• stacker – library that holds a few tapes
• silo – library that holds thousands of tapes
A disk-resident file can be archived to tape for low cost storage; the computer can stage
it back into disk storage for active use.
B.V.A.SWAMY 136 | P a g e
DEPT. OF COMPUTER SCIENCE
Topic: 4.4
Application Interface
• Most OSs handle removable disks almost exactly like fixed disks — a new
cartridge is formatted and an empty file system is generated on the disk.
• Tapes are presented as a raw storage medium, i.e., and application does not
not open a file on the tape, it opens the whole tape drive as a raw device.
• Usually the tape drive is reserved for the exclusive use of that application.
• Since the OS does not provide file system services, the application must
decide how to use the array of blocks.
• Since every application makes up its own rules for how to organize a
tape, a tape full of data can generally only be used by the program that created it.
B.V.A.SWAMY 137 | P a g e
DEPT. OF COMPUTER SCIENCE
• Hydra
• Fixed set of access rights known to and interpreted by the system
• Interpretation of user-defined rights performed solely by user's program;
system provides access protection for use of these rights
• • Cambridge CAP System
• Data capability -provides standard read, write, execute of individual storage
segments associated with object
• Software capability -interpretation left to the subsystem, through its protected
procedures
• Specification of protection in a programming language allows the high-level
description of policies for the allocation and use of resources
• Language implementation can provide software for protection enforcement
when automatic hardware- supported checking is unavailable
• Protection in Java 2
• Protection is handled by the Java Virtual Machine (JVM)
• A class is assigned a protection domain when it is loaded by the JVM
• The protection domain indicates what operations the class can (and cannot)
perform
• If a library method is invoked that performs a privileged operation, the stack is
inspected to ensure the operation can be performed by the library
• Interpret protection specifications to generate calls on whatever protection
system is provided by the hardware and the operating system.
B.V.A.SWAMY 138 | P a g e
DEPT. OF COMPUTER SCIENCE
UNIT – 4
TOPICS
Case Study Page No
Linux
B.V.A.SWAMY 139 | P a g e