Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Republic of the Philippines

Mountain Province State Polytechnic College


Bontoc, Mountain Province

DEADLOCKS & MEMORY MANAGEMENT

Module 4 of 4

PLATFORM TECHNOLOGIES

Brueckner B. Aswigue

Information Technology Department

1st Semester, SY 2020-2021


CP #: 0947-3621-495
[email protected]
INTRODUCTION

This module will discuss deadlocks where problems that can only arise in a
system with multiple active asynchronous processes. You will learn the three basic
approaches to deadlock: prevention, avoidance, and detection. It can be useful to pose
a deadlock problem in human terms and ask why human systems never deadlock. It
will also allocate the resources to prevent deadlock, known as the Banker’s Algorithm.

Many memory-management schemes exist, reflecting various approaches, and


the effectiveness of each algorithm depends on the situation. This modules will covers
strategies for small dedicated systems of memory management. You will learn resident
monitor, swapping, partitions, paging, and segmentation in the lesson 2.

This module presents the overview of deadlock and memory management of the
Operating Systems. Ebook, powerpoint and movies presentation also included for you
to give you more details of the module and also as your references.

The number of hours allotted for this module shall be for 11 hours. You are
expected to finish the module in 7 weeks.

LEARNING OUTCOMES

At the end of the module, you should be able to:


a. evaluate thoroughly the concepts, preventing, and avoiding deadlocks of the
concurrent processes; and,
b. analyze extensively memory-management organization memory hardware,
techniques, paging and segmentation.

PRE-TEST

The following questions cover general areas of this module. You may not
know the answers to all questions, but please attempt to answer them without
asking others or referring to books.
Choose the best answer for each question and write the letter of your choice
after the number.

1. A situation that occurs in Operating System when any process enter a waiting state
because another waiting process is holding the demanded resources.
a. Starvation
b. Deadlock
c. Request
d. Use

2. A resource can be release only voluntarily by the process holding it, after that process
has completed its task
a. Mutual exclusion
b. Hold and wait
c. No preemption
d. Circular wait

3. A deadlock occurrence can be detected by the resource scheduler


a. Deadlock prevention
b. Deadlock no prevention
c. Deadlock no action
d. Avoidance

4. If means -
a. Process is holding an instances of Resources

1
b. Process request instance of Resources
c. Process is pointing to Resources
d. Process moves to Resources
5. Helps you to dynamics access the resource – allocation state so that there can never
be a circular wait situation
a. Avoidance Algorithm
b. Prevention Algorithm
c. Hold and wait
d. Multiple instances

6. Consist of a large array of bytes that have an each with its own.
a. Memory
b. CPU
c. Instruction
d. Cache

7. Specifies the size of the range of the logical address space


a. Base register
b. Limit register
c. Cache
d. Memory access

8. Set of all logical addresses generated by a progam


a. Logical address space
b. Physical address space
c. Virtual address space
d. All of the above

9. It is called the small piece of code


a. Stub
b. Stab
c. Stall
d. bits

10. Fast disk large enough to accommodate copies of all memory images for all users
a. Store
b. Front store
c. Backing store
d. Alternative store

LESSON 1: Deadlocks

Objectives:
At the end of the lesson, you should be able to:
1. identify accurately the meaning of platform technologies;
2. discuss comprehensively the degree of abstraction, bundling, interoperability
and evolution of the platform;
3. identify accurately the meaning of what OS all about; and,
4. discuss comprehensively the OS and its features

Let’s Engage.
A deadlock is a situation in which two computer programs sharing the same
resource are effectively preventing each other from accessing the resource, resulting in
both programs ceasing to function.

2
The earliest computer operating systems ran only one program at a time. All of
the resources of the system were available to this one program. Later, operating systems
ran multiple programs at once, interleaving them. Programs were required to specify in
advance what resources they needed so that they could avoid conflicts with other
programs running at the same time. Eventually some operating systems offered dynamic
allocation of resources. Programs could request further allocations of resources after
they had begun running. This led to the problem of the deadlock. Here is the simplest

Both threads (road highway) are blocked; each is waiting for an event which will
never occur. Traffic gridlock is an everyday example of a deadlock situation. In order
for deadlock to occur, four conditions must be true. Mutual exclusion - Each resource
is either currently allocated to exactly one process or it is available.

Figure 1. Example of Deadlock occurs in the crossroad (Operating System.


http://www.os-book.com).

1. System Model

A system consists of a finite number of resources to be distributed among a number


of competing processes. The resources may be partitioned into several types (or classes),
each consisting of some number of identical instances. CPU cycles, files, and I/O devices
(such as printers and DVD drives) are examples of resource types. If a system has two
CPUs, then the resource type CPU has two instances. Similarly, the resource type printer
may have five instances.

If a process requests an instance of a resource type, the allocation of any instance


of the type should satisfy the request. If it does not, then the instances are not identical,
and the resource type classes have not been defined properly.

3
For example, a system may have two printers. These two printers may be defined
to be in the same resource class if no one cares which printer prints which output.
However, if one printer is on the ninth floor and the other is in the basement, then
people on the ninth floor may not see both printers as equivalent, and separate resource
classes may need to be defined for each printer.

A process must request a resource before using it and must release the resource
after using it. A process may request as many resources as it requires to carry out its
designated task. Obviously, the number of resources requested may not exceed the total
number of resources available in the system. In other words, a process cannot request
three printers if the system has only two. Under the normal mode of operation, a process
may utilize a resource in only the following sequence:
1. Request. The process requests the resource. If the request cannot be granted
immediately (for example, if the resource is being used by another process),
then the requesting process must wait until it can acquire the
resource.
2. Use. The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
3. Release. The process releases the resource.

2. Deadlock Characterization
Deadlock is a situation that occurs in OS when any process enters a waiting state
because another waiting process is holding the demanded resource. Deadlock is a
common problem in multi-processing where several processes share a specific type of
mutually exclusive resource known as a soft lock or software.

Deadlock can arise if four conditions hold simultaneously.


• Mutual exclusion: only one process at a time can use a resource
• Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
• No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task
• Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such
that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource
that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn
is waiting for a resource that is held by P0.

Example of Deadlock
• A real-world example would be traffic, which is going only in one direction.
• Here, a bridge is considered a resource.
• So, when Deadlock happens, it can be easily resolved if one car backs up
(Preempt resources and rollback).
• Several cars may have to be backed up if a deadlock situation occurs.
• So starvation is possible.

Figure 2. Example of deadlock (Operating System. http://www.os-book.com).


Circular wait
One process is waiting for the resource, which is held by the second
process, which is also waiting for the resource held by the third process etc.
This will continue until the last process is waiting for a resource held by the
first process. This creates a circular chain.

4
For example, Process A is allocated Resource B as it is requesting
Resource A. In the same way, Process B is allocated Resource A, and it is
requesting Resource B. This creates a circular wait loop.

Example of Circular wait


For example, a computer has three USB drives and three processes. Each
of the three processes able to holds one of the USB drives. So, when each process
requests another drive, the three processes will have the deadlock situation as
each process will be waiting for the USB drive to release, which is currently in
use. This will result in a circular chain.

Figure 3. Example of Circular wait (Operating System. http://www.os-


book.com).

Resource-Allocation Graph
A set of vertices V and a set of edges E.
• V is partitioned into two types:
o P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system
o R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
▪ request edge – directed edge Pi → Rj
▪ assignment edge – directed edge Rj → Pi

5
Figure 4. Example of Resource-allocation graph

o The sets P, R, and E:


▪ P = {P1, P2, P3}
▪ R = {R1, R2, R3, R4}
▪ E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}
▪ Resource instances:
• One instance of resource type R1
• Two instances of resource type R2
• One instance of resource type R3
• Three instances of resource type R4
▪ Process states:
• Process P1 is holding an instance of resource type R2 and
is waiting for an instance of resource type R1.
• Process P2 is holding an instance of R1 and an instance of
R2 and is waiting for an instance of R3.
• Process P3 is holding an instance of R3.

Figure 5. Resource Allocation Graph With A Deadlock

▪ Suppose that process P3 requests an instance of resource type R2.


Since no resource instance is currently available, we add a request
edge
P3 → R2 to the figure 5. At this point, two minimal cycles exist in the
system:

P1 → R1 → P2 → R3 → P3 → R2 → P1
P2 → R3 → P3 → R2 → P2
▪ Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the
resource R3, which is held by process P3. Process P3 is waiting for
either process P1 or process P2 to release resource R2. In addition,
process P1 is waiting for process P2 to release resource R1.

6
Figure 6. Resource-allocation graph with a cycle but no deadlock
(Operating System. http://www.os-book.com).

Basic facts:
• If graph contains no cycles  no deadlock
• If graph contains a cycle 
o if only one instance per resource type, then deadlock
o if several instances per resource type, possibility of deadlock

3. Methods for Handling Deadlocks

• Ensure that the system will never enter a deadlock state:


o Deadlock prevention
o Deadlock avoidance
• Allow the system to enter a deadlock state and then recover
• Ignore the problem and pretend that deadlocks never occur in the system; used by
most operating systems, including UNIX

Deadlock Prevention:
It's important to prevent a deadlock before it can occur. The system checks every
transaction before it is executed to make sure it doesn't lead the deadlock situations.
Such that even a small change to occur dead that an operation which can lead to
Deadlock in the future it also never allowed process to execute.
It is a set of methods for ensuring that at least one of the conditions cannot hold.

Deadlock Detection
A deadlock occurrence can be detected by the resource scheduler. A resource
scheduler helps OS to keep track of all the resources which are allocated to different
processes. So, when a deadlock is detected, it can be resolved using the below-given
methods:

No preemptive action:
No Preemption - A resource can be released only voluntarily by the process
holding it after that process has finished its task
• If a process which is holding some resources request another resource that
can't be immediately allocated to it, in that situation, all resources will be
released.
• Preempted resources require the list of resources for a process that is
waiting.
• The process will be restarted only if it can regain its old resource and a new
one that it is requesting.
• If the process is requesting some other resource, when it is available, then it
was given to the requesting process.
• If it is held by another process that is waiting for another resource, we
release it and give it to the requesting process.

7
Mutual Exclusion:
Mutual Exclusion is a full form of Mutex. It is a special type of binary
semaphore which used for controlling access to the shared resource. It includes a
priority inheritance mechanism to avoid extended priority inversion problems. It
allows current higher priority tasks to be kept in the blocked state for the shortest
time possible.
Resources shared such as read-only files never lead to deadlocks, but
resources, like printers and tape drives, needs exclusive access by a single process.

Hold and Wait:


In this condition, processes must be stopped from holding single or multiple
resources while simultaneously waiting for one or more others.

Circular Wait:
It imposes a total ordering of all resource types. Circular wait also requires that
every process request resources in increasing order of enumeration.

Deadlock Avoidance
It is better to avoid a deadlock instead of taking action after the Deadlock has
occurred. It needs additional information, like how resources should be used.
Deadlock avoidance is the simplest and most useful model that each process
declares the maximum number of resources of each type that it may need.

Avoidance Algorithms
The deadlock-avoidance algorithm helps you to dynamically assess the
resource-allocation state so that there can never be a circular-wait situation.
A single instance of a resource type.
• Use a resource-allocation graph
• Cycles are necessary which are sufficient for Deadlock
Multiples instances of a resource type.
• Cycles are necessary but never sufficient for Deadlock.
• Uses the banker's algorithm

Difference Between Starvation and Deadlock


Here, are some important differences between Deadlock and starvation:
Deadlock Starvation

The deadlock situation occurs when Starvation is a situation where all the
one of the processes got blocked. low priority processes got blocked, and
the high priority processes execute.

Deadlock is an infinite process. Starvation is a long waiting but not an


infinite process.

Every Deadlock always has starvation. Every starvation doesn't necessarily


have a deadlock.

Deadlock happens then Mutual It happens due to uncontrolled priority


exclusion, hold and wait. Here, and resource management.
preemption and circular wait do not
occur simultaneously.

Advantages of Deadlock
Here, are pros/benefits of using Deadlock method
• This situation works well for processes which perform a single burst of
activity
• No preemption needed for Deadlock.
• Convenient method when applied to resources whose state can be saved and
restored easily
• Feasible to enforce via compile-time checks

8
• Needs no run-time computation since the problem is solved in system
design

Disadvantages of Deadlock method


Here, are cons/ drawback of using deadlock method
• Delays process initiation
• Processes must know future resource need
• Pre-empts more often than necessary
• Dis-allows incremental resource requests
• Inherent preemption losses.

Summary:
• Deadlock Definition: It is a situation that occurs in OS when any process
enters a waiting state because another waiting process is holding the
demanded resource
• Circular waiting happens when one process is waiting for the resource,
which is held by the second process, which is also waiting for the resource
held by the third process etc.
• A deadlock occurrence can be detected by the resource scheduler.
• It's important to prevent a deadlock before it can occur.
• A resource can be released only voluntarily by the process holding it after
that process has finished its task.
• Mutual Exclusion is a full form of Mutex. It is a special type of binary
semaphore which used for controlling access to the shared resource.
• Hold and wait is a condition where processes must be stopped from holding
single or multiple resources while simultaneously waiting for one or more
others.
• Deadlock avoidance is the simplest and most useful model that each process
declares the maximum number of resources of each type that it may need.
• The deadlock-avoidance algorithm helps you to dynamically assess the
resource-allocation state so that there can never be a circular-wait situation.
• Deadlock is an infinite process, whereas starvation is a long waiting but not
an infinite process.

Deadlock Avoidance
Requires that the system has some additional a priori information
available
• Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
• The deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that there can never be a circular-wait condition
• Resource-allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes

Safe State
• When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
• System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes in the systems such that for each Pi, the resources that Pi can
still request can be satisfied by currently available resources + resources
held by all the Pj, with j < I
• That is:
o If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
o When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
o When Pi terminates, Pi +1 can obtain its needed resources, and so on
• If a system is in safe state  no deadlocks
• If a system is in unsafe state  possibility of deadlock
• Avoidance  ensure that a system will never enter an unsafe state.

9
Safe, Unsafe, Deadlock State

A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe


state. Figure 7 shows that not all unsafe states are deadlocks, however an unsafe
state may lead to a deadlock. As long as the state is safe, the operating system
can avoid unsafe (and deadlocked) states. In an unsafe state, the operating
system cannot prevent processes from requesting resources in such a way that a
deadlock occurs. The behavior of the processes controls unsafe states.

Figure 7. Safe, unsafe, and deadlocked state spaces (Operating System.


http://www.os-book.com).

Avoidance Algorithms
• Claim edge Pi → Rj indicated that process Pj may request resource Rj;
represented by a dashed line
• Claim edge converts to request edge when a process requests a resource
• Request edge converted to an assignment edge when the resource is
allocated to the process
• When a resource is released by a process, assignment edge reconverts to
a claim edge
• Resources must be claimed a priori in the system

Banker’s Algorithm

Let n = number of processes, and m = number of resources types.


• Available: Vector of length m. If available [j] = k, there are k instances of
resource type Rj available
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj
to complete its task

Need [i,j] = Max[i,j] – Allocation [i,j]

Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi  Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state

10
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj
1. If Requesti  Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim
2. If Requesti  Available, go to step 3. Otherwise Pi must wait, since
resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• If safe  the resources are allocated to Pi
• If unsafe  Pi must wait, and the old resource-allocation state is restored

Example of Banker’s Algorithm


• 5 processes P0 through P4;
o 3 resource types:
o A (10 instances), B (5instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433

• The content of the matrix Need is defined to be Max – Allocation


Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies
safety criteria

Example: P1 Request (1,0,2)


• Check that Request  Available (that is, (1,0,2)  (3,3,2)  true
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?

DEADLOCK DETECTION
• Allow system to enter deadlock state
• Detection algorithm

11
• Recovery scheme

Single Instance of Each Resource Type


• Maintain wait-for graph
o Nodes are processes
o Pi → Pj if Pi is waiting for Pj
• Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock
• 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

Figure 8. (a) Resource-allocation graph. (b) Corresponding wait-for graph.

Several Instances of a Resource Type


• Available: A vector of length m indicates the number of available resources
of each type
• Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process
• Request: An n x m matrix indicates the current request of each process. If
Request [i][j] = k, then process Pi is requesting k more instances of resource
type Rj.

Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti  Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock
state. Moreover, if Finish[i] == false, then Pi is deadlocked

Algorithm requires an order of O(m x n2) operations to detect whether the


system is in deadlocked state

Example of Detection Algorithm


• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000

12
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i

• P2 requests an additional instance of type C


Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
• State of system?
o Can reclaim resources held by process P0, but insufficient resources
to fulfill other processes; requests
o Deadlock exists, consisting of processes P1, P2, P3, and P4

Detection-Algorithm Usage

• When, and how often, to invoke depends on:


o How often a deadlock is likely to occur?
o How many processes will need to be rolled back?
▪ one for each disjoint cycle
• If detection algorithm is invoked arbitrarily, there may be many cycles in the
resource graph and so we would not be able to tell which of the many
deadlocked processes “caused” the deadlock

Recovery from Deadlock: Process Termination


• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is eliminated
• In which order should we choose to abort?
o Priority of the process
o How long process has computed, and how much longer to completion
o Resources the process has used
o Resources process needs to complete
o How many processes will need to be terminated
o Is process interactive or batch?
o
Recovery from Deadlock: Resource Preemption
• Selecting a victim – minimize cost
• Rollback – return to some safe state, restart process for that state
• Starvation – same process may always be picked as victim, include number
of rollback in cost factor
If you want to know more interesting facts about deadlock, visit the
following:
• Additional resources such as movie presentation in the detection
and prevention of deadlock for you to understand thoroughly the
lesson;
• Operating system. http://www.wiley.com/college and clicking
“Who’s my rep?”; and,
• Operating System. http://www.os-book.com.

It’s Your Turn.


Based on the previous modules, the rubrics still followed on this
activity. Explain each questions. Before you begin writing, read
carefully the module and plan what you will say.
1. List three examples of deadlocks that are not related to a computer
system environment.
2. Suppose that a system is in an unsafe state. Show that it is possible for the
processes to complete their execution without entering a deadlocked state.

13
3. Consider the following snapshot of a system:

Answer the following questions using the banker’s algorithm:


a. What is the content of the matrix Need?
b. Is the system in a safe state?
c. If a request from process P1 arrives for (0,4,2,0), can the request be
granted immediately

Lesson 2: MEMORY MANAGEMENT

Objectives:
At the end of the lesson, you should be able to:
1. identify comprehensively the description of organizing memory hardware; and,
2. analyze extensively the memory-management techniques, paging and
segmentation.

Let’s Engage.
Main Memory refers to a physical memory that is the internal memory to the
computer. The word main is used to distinguish it from external mass storage devices
such as disk drives. Main memory is also known as RAM. The computer is able to change
only data that is in main memory. Therefore, every program we execute and every file
we access must be copied from a storage device into main memory.

The memory management algorithms vary from a primitive bare-machine


approach to paging and segmentation strategies. Each approach has its own advantages
and disadvantages. Selection of a memory-management method for a specific system
depends on many factors, especially on the hardware design of the system. As we shall
see, many algorithms require hardware support, leading many systems to have closely
integrated hardware and operating-system memory management.

Memory management is the functionality of an operating system which handles


or manages primary memory and moves processes back and forth between main
memory and disk during execution. Memory management keeps track of each and every
memory location, regardless of either it is allocated to some process or it is free. It checks
how much memory is to be allocated to processes. It decides which process will get
memory at what time. It tracks whenever some memory gets freed or unallocated and
correspondingly it updates the status.

This lesson will discuss several issues that are pertinent to managing memory:
basic hardware, the binding of symbolic memory addresses to actual physical
addresses, and the distinction between logical and physical addresses.

14
Memory is central to the operation of a modern computer system. Memory
consists of a large array of bytes, each with its own address. The CPU fetches
instructions from memory according to the value of the program counter. These
instructions may cause additional loading from and storing to specific memory
addresses.

A typical instruction-execution cycle, for example, first fetches an instruction


from memory. The instruction is then decoded and may cause operands to be fetched
from memory. After the instruction has been executed on the operands, results may be
stored back in memory. The memory unit sees only a stream of memory addresses; it
does not know how they are generated (by the instruction counter, indexing, indirection,
literal addresses, and so on) or what they are for (instructions or data). Accordingly, we
can ignore how a program generates a memory address. We are interested only in the
sequence of memory addresses generated by the running program.

Background
• Program must be brought (from disk) into memory and placed within a process
for it to be run
• Main memory and registers are only storage CPU can access directly
• Memory unit only sees a stream of addresses + read requests, or address + data
and write requests
• Register access in one CPU clock (or less)
• Main memory can take many cycles, causing a stall
• Cache sits between main memory and CPU registers
• Protection of memory required to ensure correct operation

Base and Limit Registers


• A pair of base and limit registers define the logical address space
• CPU must check every memory access generated in user mode to be sure it is
between base and limit for that user
• Figure 9 shows on how we can provide this protection by using two registers,
usually a base and a limit. The base register holds the smallest legal physical
memory address; the limit register specifies the size of the range. For example, if
the base register holds 300040 and the limit register is 120900, then the
program can legally access all addresses from 300040 through 420939
(inclusive).

Figure 9. A base and a limit register define a logical address space (Operating
System. http://www.os-book.com).

15
• Figure 10 shows the hardware address protection to prevent a user program from
(accidentally or deliberately) modifying the code or data structures of either the
operating system or other users.
• The base and limit registers can be loaded only by the operating system, which
uses a special privileged instruction. Since privileged instructions can be
executed only in kernel mode, and since only the operating system executes in
kernel mode, only the operating system can load the base and limit registers .

Figure 10. Hardware address protection with base and limit registers

Address Binding
• Programs on disk, ready to be brought into memory to execute form an input
queue
o Without support, must be loaded into address 0000
• Inconvenient to have first user process physical address always at 0000
o How can it not be?
• Further, addresses represented in different ways at different stages of a
program’s life
o Source code addresses usually symbolic
o Compiled code addresses bind to relocatable addresses
▪ i.e. “14 bytes from beginning of this module”
o Linker or loader will bind relocatable addresses to absolute addresses
▪ i.e. 74014
o Each binding maps one address space to another

Binding of Instructions and Data to Memory


• Address binding of instructions and data to memory addresses can
happen at three different stages
o Compile time: If memory location known a priori, absolute code
can be generated; must recompile code if starting location changes
o Load time: Must generate relocatable code if memory location is
not known at compile time
o Execution time: Binding delayed until run time if the process
can be moved during its execution from one memory segment to
another
▪ Need hardware support for address maps (e.g., base and
limit registers)

16
Figure 11. Multistep Processing of a User Program (Operating System.
http://www.os-book.com).

Logical vs. Physical Address Space


• The concept of a logical address space that is bound to a separate
physical address space is central to proper memory management
o Logical address – generated by the CPU; also referred to as
virtual address
o Physical address – address seen by the memory unit
• 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
• Logical address space is the set of all logical addresses generated by a
program
• Physical address space is the set of all physical addresses generated by
a program

Memory-Management Unit (MMU)


• Hardware device that at run time maps virtual to physical address
• Many methods possible, covered in the rest of this chapter
• To start, consider simple scheme where the value in the relocation
register is added to every address generated by a user process at the
time it is sent to memory
o Base register now called relocation register
o MS-DOS on Intel 80x86 used 4 relocation registers
• The user program deals with logical addresses; it never sees the real
physical addresses
o Execution-time binding occurs when reference is made to
location in memory
o Logical address bound to physical addresses

Dynamic relocation using a relocation register


• Routine is not loaded until it is called
• Better memory-space utilization; unused routine is never loaded
• All routines kept on disk in relocatable load format
• Useful when large amounts of code are needed to handle infrequently
occurring cases
• No special support from the operating system is required

17
o Implemented through program design
o OS can help by providing libraries to implement dynamic loading

Figure 12. Dynamic relocation using a relocation register (Operating


System. http://www.os-book.com).

Dynamic Linking
• Static linking – system libraries and program code combined by the loader
into the binary program image
• Dynamic linking –linking postponed until execution time
• Small piece of code, stub, used to locate the appropriate memory-resident
library routine
• Stub replaces itself with the address of the routine, and executes the routine
• Operating system checks if routine is in processes’ memory address
o If not in address space, add to address space
• Dynamic linking is particularly useful for libraries
• System also known as shared libraries
• Consider applicability to patching system libraries
o Versioning may be needed

Swapping
• A process can be swapped temporarily out of memory to a backing store, and then
brought back into memory for continued execution
o Total physical memory space of processes can exceed physical memory
• Backing store – fast disk large enough to accommodate copies of all memory
images for all users; must provide direct access to these memory images
• Roll out, roll in – swapping variant used for priority-based scheduling algorithms;
lower-priority process is swapped out so higher-priority process can be loaded and
executed
• Major part of swap time is transfer time; total transfer time is directly proportional
to the amount of memory swapped
• System maintains a ready queue of ready-to-run processes which have memory
images on disk
• Does the swapped out process need to swap back in to same physical addresses?
• Depends on address binding method
o Plus consider pending I/O to / from process memory space
• Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and
Windows)
o Swapping normally disabled
o Started if more than threshold amount of memory allocated
o Disabled again once memory demand reduced below threshold

18
Figure 8.5 Swapping of two processes using a disk as a backing store
(Operating System. http://www.os-book.com).

Context Switch Time including Swapping


• If next processes to be put on CPU is not in memory, need to swap out a
process and swap in target process
• Context switch time can then be very high
• 100MB process swapping to hard disk with transfer rate of 50MB/sec
o Swap out time of 2000 ms
o Plus swap in of same sized process
o Total context switch swapping component time of 4000ms (4 seconds)
• Can reduce if reduce size of memory swapped – by knowing how much
memory really being used
o System calls to inform OS of memory use via request_memory() and
release_memory()
o Other constraints as well on swapping
▪ Pending I/O – can’t swap out as I/O would occur to wrong process
▪ Or always transfer I/O to kernel space, then to I/O device
• Known as double buffering, adds overhead
o Standard swapping not used in modern operating systems
▪ But modified version common
• Swap only when free memory extremely low

Swapping on Mobile Systems


• Not typically supported
o Flash memory based
▪ Small amount of space
▪ Limited number of write cycles
▪ Poor throughput between flash memory and CPU on mobile
platform
• Instead use other methods to free memory if low
o iOS asks apps to voluntarily relinquish allocated memory
▪ Read-only data thrown out and reloaded from flash if needed
▪ Failure to free can result in termination
o Android terminates apps if low free memory, but first writes
application state to flash for fast restart
o Both OSes support paging as discussed below

CONTIGUOUS ALLOCATION
• Main memory must support both OS and user processes
• Limited resource, must allocate efficiently
• Contiguous allocation is one early method
• Main memory usually into two partitions:
o Resident operating system, usually held in low memory with interrupt vector

19
o User processes then held in high memory
o Each process contained in single contiguous section of memory
• Relocation registers used to protect user processes from each other, and from
changing operating-system code and data
o Base register contains value of smallest physical address
o Limit register contains range of logical addresses – each logical address
must be less than the limit register
o MMU maps logical address dynamically
o Can then allow actions such as kernel code being transient and kernel
changing size

Multiple-partition allocation
• Multiple-partition allocation
o Degree of multiprogramming limited by number of partitions
o Variable-partition sizes for efficiency (sized to a given process’ needs)
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 Process exiting frees its partition, adjacent free partitions combined
o Operating system maintains information about:
a) allocated partitions b) free partitions (hole)

Dynamic Storage-Allocation Problem


How to satisfy a request of size n from a list of free holes?
• First-fit: Allocate the first hole that is big enough
• Best-fit: Allocate the smallest hole that is big enough; must search entire
list, unless ordered by size
o Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search entire list
o Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and storage utilization

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
• First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to
fragmentation
o 1/3 may be unusable -> 50-percent rule
• Reduce external fragmentation by compaction
o Shuffle memory contents to place all free memory together in one
large block
o Compaction is possible only if relocation is dynamic, and is done at
execution time
o I/O problem
▪ Latch job in memory while it is involved in I/O
▪ Do I/O only into OS buffers

20
• Now consider that backing store has same fragmentation problems
Segmentation
• Memory-management scheme that supports user view of memory
• A program is a collection of segments
o A segment is a logical unit such as:
▪ main program
▪ procedure
▪ function
▪ method
▪ object
▪ local variables, global variables
▪ common block
▪ stack
▪ symbol table
▪ arrays

Figure. User’s View of a Program (Operating System. http://www.os-


book.com).

Segmentation Architecture
• Logical address consists of a two tuple:
• <segment-number, offset>,
• Segment table – maps two-dimensional physical addresses; each table
entry has:
o base – contains the starting physical address where the segments
reside in memory
o limit – specifies the length of the segment
o Segment-table base register (STBR) points to the segment table’s
location in memory
• Segment-table length register (STLR) indicates number of segments used
by a program;
o segment number s is legal if s < STLR
• Protection
o With each entry in segment table associate:
▪ validation bit = 0  illegal segment
▪ read/write/execute privileges
• Protection bits associated with segments; code sharing occurs at segment
level
• Since segments vary in length, memory allocation is a dynamic storage-
allocation problem
• A segmentation example is shown in the following diagram

21
Figure . Segmentation hardware (Operating System. http://www.os-book.com).

Paging
• Physical address space of a process can be noncontiguous; process is allocated
physical memory whenever the latter is available
o Avoids external fragmentation
o Avoids problem of varying sized memory chunks
• Divide physical memory into fixed-sized blocks called frames
o Size is power of 2, between 512 bytes and 16 Mbytes
• Divide logical memory into blocks of same size called pages
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames and load program
• Set up a page table to translate logical to physical addresses
• Backing store likewise split into pages
• Still have Internal fragmentation

Address Translation Scheme


• Address generated by CPU is divided into:
o Page number (p) – used as an index into a page table which contains
base address of each page in physical memory
o Page offset (d) – combined with base address to define the physical
memory address that is sent to the memory unit
page number page offset
p d
m -n n
o For given logical address space 2m and page size 2n

• Calculating internal fragmentation


o Page size = 2,048 bytes
o Process size = 72,766 bytes
o 35 pages + 1,086 bytes
o Internal fragmentation of 2,048 - 1,086 = 962 bytes
o Worst case fragmentation = 1 frame – 1 byte
o On average fragmentation = 1 / 2 frame size
o So small frame sizes desirable?
o But each page table entry takes memory to track
o Page sizes growing over time
▪ Solaris supports two page sizes – 8 KB and 4 MB
• Process view and physical memory now very different
• By implementation process can only access its own memory

Implementation of Page Table


• Page table is kept in main memory

22
• Page-table base register (PTBR) points to the page table
• Page-table length register (PTLR) indicates size of the page table
• In this scheme every data/instruction access requires two memory accesses
o One for the page table and one for the data / instruction
• The two memory access problem can be solved by the use of a special fast-
lookup hardware cache called associative memory or translation look-aside
buffers (TLBs)
• Some TLBs store address-space identifiers (ASIDs) in each TLB entry –
uniquely identifies each process to provide address-space protection for that
process
o Otherwise need to flush at every context switch
• TLBs typically small (64 to 1,024 entries)
• On a TLB miss, value is loaded into the TLB for faster access next time
o Replacement policies must be considered
o Some entries can be wired down for permanent fast access

Associative Memory
• Associative memory – parallel search
Page # Frame #

• Address translation (p, d)


o If p is in associative register, get frame # out
• Otherwise get frame # from page table in memory

Effective Access Time


• Associative Lookup =  time unit
o Can be < 10% of memory access time
• Hit ratio = 
o Hit ratio – percentage of times that a page number is found in the
associative registers; ratio related to number of associative registers
• Consider  = 80%,  = 20ns for TLB search, 100ns for memory access
• Effective Access Time (EAT)
▪ EAT = (1 + )  + (2 + )(1 – )
• =2+–
• Consider  = 80%,  = 20ns for TLB search, 100ns for memory access
o EAT = 0.80 x 100 + 0.20 x 200 = 120ns
• Consider more realistic hit ratio ->  = 99%,  = 20ns for TLB search, 100ns for
memory access
o EAT = 0.99 x 100 + 0.01 x 200 = 101ns

Memory Protection
• Memory protection implemented by associating protection bit with each frame
to indicate if read-only or read-write access is allowed
o Can also add more bits to indicate page execute-only, and so on
• Valid-invalid bit attached to each entry in the page table:
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
o Or use page-table length register (PTLR)
• Any violations result in a trap to the kernel

Shared Pages
• Shared code
o One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems)

23
o Similar to multiple threads sharing the same process space
o Also useful for interprocess communication if sharing of read-write pages
is allowed
• Private code and data
o Each process keeps a separate copy of the code and data
o The pages for the private code and data can appear anywhere in the
logical address space

Structure of the Page Table


• Memory structures for paging can get huge using straight-forward methods
o Consider a 32-bit logical address space as on modern computers
o Page size of 4 KB (212)
o Page table would have 1 million entries (232 / 212)
o If each entry is 4 bytes -> 4 MB of physical address space / memory for page
table alone
▪ That amount of memory used to cost a lot
▪ Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
• Hashed Page Tables
• Inverted Page Tables

Two-Level Paging Example


• A logical address (on 32-bit machine with 1K page size) is divided into:
o a page number consisting of 22 bits
o a page offset consisting of 10 bits
o Since the page table is paged, the page number is further divided into:
o a 12-bit page number
o a 10-bit page offset
• Thus, a logical address is as follows:

where p1 is an index into the outer page table, and p2 is the displacement within
the page of the inner page table
• Known as forward-mapped page table

It’s Your Turn.

Direction: Read the passage carefully and plan what you will write. Place your answer
in the pad paper whether yellow or white to be submitted. Each question has 10 points
each. The Essay rubrics have a correspond points that will guide you in your essay.
Features 9-10 points 7-8 points 4-6 points 1-3 points
Expert Accomplished Capable Beginner
Understanding Writing shows Writing shows a Writing shows Writing shows
strong clear adequate little
understanding understanding understanding understanding

24
Quality of Piece was Piece was written Piece had little Piece had no style
Writing written in an in an interesting style
extraordinary style
style
Gives no new
Very informative Somewhat Gives some new information and
and well- informative and information but very poorly
organized organized poorly organized organized
Grammar, Virtually no Few spelling and A number of So many spelling,
Usage & spelling, punctuation spelling, punctuation and
Mechanics punctuation or errors, minor punctuation or grammatical
grammatical grammatical grammatical errors that it
errors errors errors interferes with the
meaning

QUESTIONS:

1. List three examples of deadlocks that are not related to a computer system
environment.
2. Suppose that a system is in an unsafe state. Show that it is possible for
the processes to complete their execution without entering a deadlocked
state.
DIRECTION: SOLVE THE PROBLEM USING BANKER’S ALGORITHM.
3. Consider the following snapshot of a system:

Answer the following questions using the banker’s algorithm:


a. What is the content of the matrix Need?
b. Is the system in a safe state?
c. If a request from process P1 arrives for (0,4,2,0), can the request be
granted immediately?
d. If a request from process P1 arrives for (1,0,2,0), can the request be
granted immediately?

POST ASSESSMENT
Directions: This will assess you what you been learned on this module. Select the
letters only. Write your answer in a separate clean paper for submission.
1. Only one process at a time can use a resource
a. Mutual exclusion
b. Hold and wait
c. No Preemption
d. Circular wait

2. The system checks every transaction before it is executed to make sure it doesn’t
lead the deadlock situations
a. Deadlock prevention
b. Deadlock no prevention
c. Deadlock no action
d. Avoidance
3. It imposes a total ordering of all resources type.
a. Hold and wait
b. Circular wait
c. Deadlock avoidance
d. Avoiding algorithm

25
4. if means -
a. Process is holding an instances of Resources
b. Process request instance of Resources
c. Process is pointing to Resources
d. Process moves to Resources
5. A full form of Mutex is called
a. Mutual exclusion
b. Deadlock
c. Hold and wait
d. Resource
6. Hold the smallest legal physical memory address
a. Base register
b. Limit register
c. Cache
d. Memory access
7. Program on disk, ready to be brought into memory to execute form an input queue
a. Trap
b. Address Binding
c. Address error
d. System monitor
8. Set of all physical addresses generated by a program
a. Logical address space
b. Physical address space
c. Virtual address space
d. All of the above
9. System libraries and program code combined by the loader into the binary program
image.
a. No linking
b. Shared Linking
c. Dynamic Linking
d. Static linking
10. Block of available is called
a. Hole
b. Variable
c. Store
d. Transient

REFERENCES
Silberschatz, A. et. al. (2013). Operating System Concepts. 9th Edition. Hoboken, New
Jersey, USA: John Wiley & Sons, Inc.

Stallings, William (2012). Operating System Internals and Design Principles. 7th edition.
Upper Saddle River, New Jersey: Pearson Education, Inc.

26

You might also like