OS Combined Notes (Mukti Sir)
OS Combined Notes (Mukti Sir)
Operating System
Muktikanta Sahu
Spring 2020
General Information
z Textbook:
z Operating System Concepts, 8th Ed, by Silberschatz,
Galvin, and Gagne
z Materials from other books as and when
needed
z Grading Policy
* Midsem – 30%
* Quiz and TA - 20%
* Endsem – 50%
Introduction
What is an Operating System?
z User-centric definition
z A program that acts as an intermediary between
a user of a computer and the computer hardware
z Defines an interface for the user to use services
provided by the system
z Provides a “view” of the system to the user
z System-centric definition
z Resource allocator – manages and allocates
resources
z Control program – controls the execution of user
programs and operations of I/O devices
Computer System Components
1. Hardware – provides basic computing resources
(CPU, memory, I/O devices).
2. Operating system – controls and coordinates the
use of the hardware among the various
application programs for the various users.
3. Application programs – defines the ways in which
the system resources are used to solve the
computing problems of the users (compilers,
databases, games, …).
4. Users (people, machines, other computers).
Abstract View of System
Components
Types of Systems
z Batch Systems
z Multiple jobs, but only one job in memory at one time
and executed (till completion) before the next one starts
z Multiprogrammed Batch Systems
z Multiple jobs in memory, CPU is multiplexed between
them
z CPU-bound vs I/O bound jobs
z Time-sharing Systems
z Multiple jobs in memory and on disk, CPU is multiplexed
among jobs in memory, jobs swapped between disk and
memory
z Allows interaction with users
z Personal Computers
z Dedicated to a single user at one time
z Multiprocessing Systems
z More than one CPU in a single machine to allocate jobs to
z Symmetric Multiprocessing, NUMA machines …
z Multicore
z Other Parallel Systems, Distributed Systems, Clusters…
z Different types of systems with multiple CPUs/Machines
z Real Time Systems
z Systems to run jobs with time guarantees
z Other types possible depending on resources in the
machine, types of jobs to be run…
z OS design depends on the type of system it is
designed for
z User-centric view
z System Calls
z Command Interpreter (not strictly a part of an OS)
Process Management
z A process is a program in execution.
z Needs certain resources to accomplish
its task
z CPU time, memory, files, I/O devices…
z OS responsibilities
z Process creation and deletion.
z Process suspension and resumption.
z Provide mechanisms for:
z process synchronization
z interprocess communication
Main-Memory Management
z OS responsibilities
z Keeps track of which parts of memory are currently
being used and by whom
z Decides which processes to load when memory space
becomes available
z Allocates and deallocates memory space as needed
File Management
z OS responsibilities
z File creation, deletion, modification
z Directory creation, deletion, modification
z Support of primitives for manipulating files and
directories
z Mapping files onto secondary storage.
z File backup on stable (nonvolatile) storage media
I/O System Management
z The I/O system consists of:
z A buffer-caching system
z Device driver interface
z Drivers for specific hardware devices
Secondary-Storage Management
A bit, called mode bit, is added to the hardware of the computer to indicate the current mode: kernel (0)
or user (1). With the mode bit, we are able to distinguish between a task that is executed on behalf of the
operating system and one that is executed on behalf of the user. When the computer system is executing on
behalf of a user application, the system is in user mode. However, when a user application requests a
service from the operating system (via a system call), it must transit from user to kernel mode to fulfill the
request.
Transition from user to kernel mode
User process
Kernel mode
#include<stdio.h>
#include<unistd.h>
int main()
{
const char cp[]=”Greetings”;
write((STDOUT_FILENO,cp,sizeof((cp)-1); // system method
return 0;
}
Example contd…
int main()
{
int x;
const char cp[]=“Enter a value for x\n”;
write(STDOUT_FILENO,cp,sizeof(cp)-1);
read(STDIN_FILENO,&x,sizeof(x));
write(STDOUT_FILENO,&x,sizeof(x));
return 0;
}
Types of System Calls
#include<stdio.h>
#include<sys/types.h> output
#include<unistd.h> Main Program
int main()
I am parent and my id is=6910
{
pid_t pid; This is child process and id=6911 and my parent id=6910
printf("Main Program\n");
pid=fork();
if(pid<0)
{ printf("Error\n"); return 1; }
else if(pid==0)
{ printf("This is child process and id=%d and my parent id=%d\n",getpid(),getppid()); }
else
{
printf("I am parent and my id is=%d\n",getpid());
wait(NULL);
}
return 0;
}
Creating a child process
Parent does not wait for the child to terminate
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() output
{ Main Program
pid_t pid;
printf("Main Program\n"); I am parent and my id is=5515
pid=fork(); This is child process and id=5516 and my parent id=1
if(pid<0)
{ printf("Error\n"); return 1; }
else if(pid==0)
{ printf("This is child process and id=%d and my parent id=%d\n",getpid(),getppid()); }
else
{
printf("I am parent and my id is=%d \n",getpid());
// wait(NULL);
}
return 0;
}
Address space of child process same as that of parent
fork2.c
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
pid_t pid;
printf("Main Program\n");
pid=fork();
if(pid<0)
{ output
printf("Error\n");
return 1; Main Program
} The parent is waiting
else if(pid==0)
{ This is child process
printf("This is child process\n"); Main program terminates
//execlp("/home/user3/myfile","myfile",NULL);
} Child has finished
else Main program terminates
{
printf("The parent is waiting\n");
wait(NULL);
printf("Child has finished\n");
}
printf("Main program terminates\n");
return 0;
}
Address space of child process is different from parent process
fork1.c
#include<stdio.h>
myfile.c
#include<sys/types.h> #include<stdio.h>
#include<unistd.h>
int main()
int main()
{ {
pid_t pid;
printf("Main Program\n");
printf("hello\n");
pid=fork(); return 0;
if(pid<0)
{
}
printf("Error\n");
return 1;
}
else if(pid==0)
{
printf("This is child process\n"); output
execlp("/home/user22/myfile","myfile",NULL); Main Program
}
The parent is waiting output
else
{ This is child process hello
printf("The parent is waiting\n");
wait(NULL);
hello
printf("Child has finished\n"); Child has finished
}
printf("Main program terminates\n");
Main program terminates
return 0;
}
CPU Scheduling
Types of jobs
z CPU-bound vs. I/O-bound
z Maximum CPU utilization obtained with
multiprogramming
z Batch, Interactive, real time
z Different goals, affects scheduling policies
CPU Scheduler
z Selects from among the processes in memory
that are ready to execute, and allocates the CPU
to one of them
z CPU scheduling decisions may take place when
a process:
z Switches from running to waiting state
z Switches from running to ready state
z Switches from waiting to ready
z Terminates
z Scheduling under 1 and 4 is nonpreemptive.
z All other scheduling is preemptive.
Dispatcher
z Dispatcher module gives control of the CPU to
the process selected by the CPU scheduler; this
involves:
z switching context
z switching to user mode
z jumping to the proper location in the user program to
restart that program
z Dispatch latency – time it takes for the
dispatcher to stop one process and start another
running.
Scheduling Criteria
z CPU utilization – keep the CPU as busy as
possible
z Throughput – # of processes that complete their
execution per time unit
z Turnaround time – amount of time to execute a
particular process
z Waiting time – amount of time a process has
been waiting in the ready queue
z Response time – amount of time it takes from
when a request was submitted until the first
response is produced, not output (for time-
sharing environment)
Optimization Criteria
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
P1 P3 P2 P4
0 3 7 8 12 16
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
z α =1
z τn+1 = tn
z Only the actual last CPU burst counts
z Nonpreemptive
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
•Types of Processes
•Why IPC?
•Models of IPC
•Shared Memory Systems
•Message Passing Systems
•Shared Memory Vs. Message Passing
• Shared Memory
• Message Passing
The producer and consumer must be synchronized , so that the consumer does
not try to consume an item that has not yet been produced.
Disadvantages:
•Introduction
•Benefits
•Demerits
•Types of Threads
•Multithreading Models
•Programming Example
• Many-to-One Model
• One-to-One Model
• Many-to-Many Model
#include<iostream>
#include<cstdlib> Compilation command
#include<omp.h> g++ –fopenmp –o filename filename.cpp
using namespace std;
int main()
{
int i=0, end=64;
#pragma omp parallel for num_threads(4)
for(i=0;i<end;i++)
{
int ID=omp_get_thread_num();
#pragma omp critical
{
cout<<"I am thread: "<<ID<<"\t printing i="<<i<<endl;
}
}
return 0;
}
Execute command
mpirun –np 4 ./filename
mpi_thread.cpp
item nextProduced;
while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Bounded-Buffer:
Consumer Process
item nextConsumed;
while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
The solution allows at most n – 1 items in buffer
(of size n) at the same time. A solution, where
all n items are used is not simple
Suppose we modify the producer-consumer
code by adding a variable counter, initialized to
0 and incremented each time a new item is
added to the buffer
Producer process
item nextProduced;
Shared data while (1) {
while (counter == BUFFER_SIZE)
; /* do nothing */
#define B_SIZE 10
buffer[in] = nextProduced;
typedef struct { in = (in + 1) % BUFFER_SIZE;
... counter++;
} item; }
item buffer[B_SIZE];
int in = 0; Consumer process
int out = 0;
int counter = 0; item nextConsumed;
while (1) {
while (counter == 0)
; /* do nothing */
Will this work? nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
The Problem with this solution
The statement “counter++” may be implemented
in machine language as:
register1 = counter
register1 = register1 + 1
counter = register1
Why?
As disabling interrupts can be time consuming as the message is passed to all the processors.
Hardware Instruction Based
Solutions
Some architectures provide special instructions
that can be used for synchronization
Process Pi
do {
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}while(TRUE);
Mutual Exclusion with Swap
Shared data (initialized to false):
boolean lock;
boolean waiting[n];
Process Pi
do {
key = true;
while (key == true)
Swap(lock,key);
critical section
lock = false;
remainder section
}while(TRUE);
Problem with TestAndSet()/Swap()
Where S is an integer.
All the modifications to the integer value of the
signal(S) semaphore are atomic. That is, when one process modifies
{ the semaphore value, no other process can simultaneously
modify the same semaphore.
S++;
}
Types of Semaphores
while(TRUE)
{
wait(mutex);
// critical section
signal(mutex)
// remainder section
}
Semaphore usage
Process Synchronization
We can also use semaphores to solve various synchronization problems. For example, consider two
concurrently running process: P1 with a statement S1 and P2 with a statement S2. Suppose we want that S2 can
be executed only after S1 has completed. We can implement this scheme readily by letting P1 and P2 share a
common semaphore synch, initialized to 0, and by inserting the statements
S1 ;
signal(synch); P1
wait(synch);
S2 ; P2
Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal(synch), which is after
statement S1 has been executed.
Semaphore usage
typedef struct The wait() semaphore operation The signal() semaphore operation can
{ can now be defined as: now be defined as:
1
int value;
2 3
struct process *list; wait()(semaphore *S) signal(semaphore *S)
}semaphore; { {
S->value--; S->value++;
Each semaphore has an if(S->value<0) if(S->value<=0)
integer value and a queue of { {
processes. add this process to S->list; remove process P from S->list;
block(); wakeup(P);
} }
} }
Note that in this implementation, semaphore values may be negative. The above implementation satisfies
If a semaphore value is negative, its magnitude is the number of mutual exclusion and progress. Bounded
processes waiting on that semaphore. 4 waiting is dependent on the queueing
strategy of the semaphore list. 5
Semaphore list(queue), S->list can be implemented using any queuing
strategy(e.g., FIFO, LIFO, Priority).
Pitfalls
Use carefully to avoid
• Deadlock – two or more processes are waiting indefinitely for an event that can
be caused by only one of the waiting processes
• Starvation – indefinite blocking. A process may never be removed from the
semaphore queue in which it is suspended
• Deadlock - A condition that arises when two or more processes are waiting
indefinitely for an event that can only be caused by one of the waiting
processes
- The events that the processes are waiting for will never happen :-(
- Other processes in the system that need the resources that are currently locked
will never get access to them
• Deadlock will become an even greater problem with the trend toward more
processor cores and more threads
• Starvation is possible
- Ignore the problem and pretend that deadlocks never occur in the system
(used by most operating systems, including UNIX)
• Requires that application developers write programs that avoid deadlock
• To ensure that deadlocks never occur, a system can use either deadlock-
prevention or deadlock-avoidance
Muktikanta Sahu
Overview
Mutual Exclusion
Hold and wait
No preemption
Circular wait
• In a safe state if the system can allocate resources to each process (up to its
maximum) in some order and still avoid deadlock
• 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 plus resources held by all the Pj, with j < i
• That is:
- If resources needed by Pi are not immediately available, then Pi can wait until all Pj have
finished
- When Pj is finished, Pi can obtain the resources it needs, execute, return allocated
resources, and terminate
- When Pi terminates, Pi +1 can obtain the resources it needs, and so on
• Two main avoidance algorithms ... choice depends on how many instances of
available resource types exist
- Single instance of a resource type
• Use a resource-allocation graph
• Introduce a new type of edge to the resource-allocation graph, the claim edge
- A claim edge Pi → Rj indicates that process Pi may request a resource Rj
- A claim edge is represented as dashed lines in resource-allocation graph
- Resources must be claimed a priori in the system, so all claim edges are known
• When a process gets all its resources it must return them in a finite amount of
time
- Max -- n x m matrix that defines the maximum demand of each process on each
resource type. If Max[i,j] = k, then process Pi may request at most k instances of
resource type Rj
• The sequence <P1, P3, P0, P2, P4> is a safe sequence (there are other safe
sequences as well)
• Since the system is in a safe state, use the Resource Request procedure to
determine if the following request can be granted
- P1 requests one instance of resource A, and two instances of resource C
The request vector looks like --> Request1 = (1, 0, 2)
- First note if the resources are available to fulfill this request: Request1 ≤ Available
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
• After determining that the resources are actually available, pretend to allocate
them to process P1
• Check to see if this new state is safe, if yes, then allocate resources for real
Allocation Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2
P1 2 0 0 3 2 2 1 2 2 2 3 0
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
• Is this a safe state? The sequence <P1, P3, P4, P0, P2> is a safe sequence
CS420: Operating Systems 32
Example of Banker’s Algorithm - P1 Request (Cont.)
• If all resources have only a single instance, use a wait-for graph for deadlock
detection
- All Nodes are processes
- An edge Pi → Pj exists if Pi is waiting for Pj to release a resource
• Deadlock exists in the system if and only if the wait-for graph contains a cycle
• Periodically invoke an algorithm that searches for a cycle in the graph. If there
is a cycle, there exists a deadlock
• The wait-for graph cannot be used in systems with multiple instances of each
resource type
• The deadlock detection algorithm for several instance contains similar data
structures to the Banker’s Algorithm
• Continuing the previous example, suppose that process P2 now requests one
additional resource of type C
Allocation Request
A B C A B C Available
P0 0 1 0 0 0 0 A B C
0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0 Can only reclaim resources held by
P0. Insufficient resources to handle
P4 0 0 2 0 0 2
the requests from other processes.
• Is the system deadlocked? Deadlock exists consisting of
processes P1, P2, P3, and P4
• When, and how often, to invoke the detection algorithm depends on:
- How often a deadlock is likely to occur?
- How many processes will need to be rolled back?
• If the detection algorithm is invoked every time a request for allocation cannot be
granted:
- The algorithm can detect specifically which processes caused the deadlock
- There is a lot of computational overhead when running the algorithm so frequently
• If the detection algorithm is invoked less frequently (e.g. once per hour):
- It is difficult to determine which processes caused the deadlock
- The computational overhead can be reduced
• Once deadlock has been detected in a system there are several options for
dealing with deadlock
- Notify the system user and let them deal with it
- Have the system terminate one or more processes
- Have the system preempt resources from one or more of the deadlocked
processes
• Two options:
- Abort all deadlocked processes
• Potential to lose a lot of work / computation time
- Abort one process at a time until the deadlock cycle is eliminated
• Lots of overhead since the deadlock-detection algorithm must be run after
each process is terminated
• Consider:
- Priority of the process
- How long process has computed, and how much longer to completion
- Resources the process has used
- Resources process needs to complete
- How many processes will need to be terminated
- Is process interactive or batch?
• Continually preempt the resources from processes and give those resources to
other processes until the system is no longer deadlocked
MEMORY MANAGEMENT
8: Memory Management 1
OPERATING SYSTEM
Memory Management
What Is In This Chapter?
8: Memory Management 2
MEMORY Definitions
MANAGEMENT
• The concept of a logical address space that is bound to a separate
physical address space is central to proper memory management.
• 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
8: Memory Management 3
MEMORY Definitions
MANAGEMENT
Relocatable Means that the program image can reside anywhere in physical memory.
Binding Programs need real memory in which to reside. When is the location of that
real memory determined?
• This is called mapping logical to physical addresses.
• This binding can be done at compile/link time. Converts symbolic to
relocatable. Data used within compiled source is offset within object
module.
Compiler: If it’s known where the program will reside, then absolute code is generated.
Otherwise compiler produces relocatable code.
Execution: The code can be moved around during execution. Means flexible virtual
mapping.
8: Memory Management 4
MEMORY Binding Logical To Physical
MANAGEMENT
Source
This binding can be done at compile/link
time. Converts symbolic to relocatable.
Compiler
Data used within compiled source is offset
within object module.
Object
Can be done at load time. Other Objects
Binds relocatable to physical.
Can be done at run time. Linker
Implies that the code can be
moved around during
execution. Executable
Libraries
Loader
In-memory Image
8: Memory Management 5
MEMORY More Definitions
MANAGEMENT
Dynamic loading
+ Routine is not loaded until it is called
+ Better memory-space utilization; unused routine is never loaded.
+ Useful when large amounts of code are needed to handle infrequently occurring cases.
+ No special support from the OS is required - implemented through program design.
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 needed to check if routine is in processes’ memory address.
+ Dynamic linking is particularly useful for libraries.
8: Memory Management 6
Operating System
Muktikanta Sahu
Listing 3: sub.cpp
1 i n t sub ( i n t a , i n t b )
2 { r e t u r n a−b ; } // Save i t a s sub . cpp
Now, do the static linking of the driver program with the library.
Now, do the dynamic linking of the driver program with the library.
Static linking is performed by programs called linkers as the last step in Dynamic linking is performed at run time by the operating system.
compiling a program. Linkers are also called link editors.
Statically linked files are significantly larger in size because external programs In dynamic linking only one copy of shared library is kept in memory. This
are built into the executable files. significantly reduces the size of executable programs, thereby saving memory
and disk space.
In static linking if any of the external programs has changed then they have to be In dynamic linking this is not the case and individual shared modules can be
recompiled and re-linked again else the changes won't reflect in existing updated and recompiled. This is one of the greatest
executable file. advantages dynamic linking offers.
Statically linked program takes constant load time every time it is loaded into the In dynamic linking load time might be reduced if the shared library code is
memory for execution. already present in memory.
Programs that use statically-linked libraries are usually faster than those that use Programs that use shared libraries are usually slower than those that
shared libraries. use statically-linked libraries.
In statically-linked programs, all code is contained in a single executable module. Dynamically linked programs are dependent on having a compatible library. If
Therefore, they never run into compatibility issues. a library is changed (for example, a new compiler release may change a
library), applications might have to be reworked to be made compatible with
the new version of the library. If a library is removed from the system,
programs using that library will no longer work.
DEFINITION OF PARTITIONS:
Division of physical memory into fixed sized regions. (Allows address spaces to be
distinct = one user can't mock with another user, or the system.)
The number of partitions determines the level of multiprogramming. Partition is given
to a process when it's scheduled.
Protection around each partition determined by
bounds ( upper, lower )
base / limit.
These limits are done in hardware.
8: Memory Management 12
MEMORY
SINGLE PARTITION
MANAGEMENT ALLOCATION
RESIDENT MONITOR:
Must check each memory reference against fence ( fixed or variable ) in hardware or
register. If user generated address < fence, then illegal.
User program starts at fence -> fixed for duration of execution. Then user code has
fence address built in. But only works for static-sized monitor.
If monitor can change in size, start user at high end and move back, OR use fence as
base register that requires address binding at execution time. Add base register to
every generated user address.
Isolate user from physical address space using logical address space.
Limit Relocation
Register Register
Yes
CPU < +
Logical Physical
Address No Address MEMORY
8: Memory Management 14
MEMORY CONTIGUOUS
MANAGEMENT ALLOCATION
Must take into account who wants to run, the memory needs, and partition
availability. (This is a combination of short/medium term scheduling.)
Sequence of events:
In an empty memory slot, load a program
THEN it can compete for CPU time.
Upon job completion, the partition becomes available.
Can determine memory size required ( either user specified or
"automatically" ).
8: Memory Management 15
Fundamental Memory Management Problem
• How do we manage applications whose size may be larger
than the size of memory available?
Partition in blocks and load as necessary
• How do we share memory resources among different
processes?
• Achieved by partitioning memory
Look at several schemes
Multi-programmed:
o Divide memory into partitions of different sizes
Fixed partitions:
1 queue per partition vs 1 queue for all partitions
OS OS OS
Process 2 process 4
process 2 Process 4
Terminates Starts
process 3 process 3 process 3
8: Memory Management 16
CONTIGUOUS
MEMORY ALLOCATION
MANAGEMENT
HOW DO YOU ALLOCATE MEMORY TO NEW PROCESSES?
Avoid small holes (external fragmentation). This occurs when there are
many small pieces of free memory.
What should be the minimum size allocated, allocated in what chunk size?
Want to also avoid internal fragmentation. This is when memory is
handed out in some fixed way (power of 2 for instance) and requesting
program doesn't use it all.
8: Memory Management 17
The Buddy System
Sizes: 1, 2, 4, 8, 16
a) 3 blocks allocated
& 3 holes left
b) Block of size 1
allocated
There's little or no internal fragmentation (the process uses the memory given to it -
the size given to it will be a page.)
But there can be a great deal of external fragmentation. This is because the
memory is constantly being handed cycled between the process and free.
8: Memory Management 18
MEMORY MANAGEMENT COMPACTION
Trying to move free memory to one large block.
Only possible if programs linked with dynamic relocation (base and limit.)
OS OS OS
P1 P1
P1
P2 P3
P3 P2
P2
P3
8: Memory Management 19
Dealing with Insufficient Memory
• Memory compaction
How much and what to move?
• Swapping
Temporarily move process to disk
Requires dynamic relocation
• Overlays
Allow programs large than physical memory
Programs loaded as needed according to calling structure.
8: Memory Management 20
PAGING
MEMORY MANAGEMENT
Address Translation Scheme
Address generated by the CPU is divided into:
• Page number (p) – used as an index into a page table which
contains base address of each page in physical memory.
p d
8: Memory Management 21
PAGING
MEMORY MANAGEMENT
Permits a program's memory to be physically noncontiguous so it can be allocated
from wherever available. This avoids fragmentation and compaction.
Size of frames/pages is
defined by hardware (power
of 2 to ease calculations)
HARDWARE
An address is determined by:
8: Memory Management 22
PAGING
MEMORY MANAGEMENT
0
Paging Example - 32-byte memory with 4-byte pages 4 I
j
k
0a
l
1b
8 m
2c
n
3d 0 5 o
1 6 p
4e
2 1
5f 12
3 2
6g
7h 16
Page Table
8I 20 a
9j b
10 k c
11 l d
24 e
12 m f
13 n g
14 o Physical Memory h
15 p 28
Logical Memory 8: Memory Management 23
MEMORY MANAGEMENT PAGING
• A 32 bit machine can IMPLEMENTATION OF THE PAGE TABLE
address 4 gigabytes which
is 4 million pages (at 1024 TLB = Translation Lookaside Buffer
bytes/page). WHO says
how big a page is, anyway?
• Could use dedicated
registers (OK only with
small tables.)
• Could use a register
pointing to table in memory
(slow access.)
• Cache or associative
memory
• (TLB = Translation
Lookaside Buffer):
• simultaneous search is fast
and uses only a few
registers.
8: Memory Management 24
MEMORY MANAGEMENT PAGING
Relevant times:
2 nanoseconds to search associative memory – the TLB.
20 nanoseconds to access processor cache and bring it into TLB for next time.
8: Memory Management 25
MEMORY MANAGEMENT PAGING
SHARED PAGES
8: Memory Management 26
MEMORY MANAGEMENT PAGING
MULTILEVEL PAGE TABLE
8: Memory Management 29
Logical Physical
Page Memor
Content L. A. Memory
Frame Content P.A.
y
A 0 0 M 0
0 B 1 N 1
C 2 O 2
D 3 P 3
E 4 Page table 2 1 I 4
1 F 5 (P2)
Page Frame J 5
G 6 0 7 K 6
H 7 0 1 11 L 7
I 8 2 1 2 $ 8
2 J 9 3 0 % 9
K 10 4 4 & 10
L 11 1 5 12 * 11
M 12 Outer 6 13 3 7 12
3 N 13 page
Page Frame 7 2 11 13
O 14 table
0 3 1 14
P 15 (P1)
1 6 0 15
Q 16 4 Q 16
4 R 17 R 17
S 18 S 18
T 19 T 19
U 20 5 20
5 V 21 21
W 22 22
X 23 23
Y 24 6 4 24
6 Z 25 12 25
@ 26 13 26
# 27 2 27
$ 28 7 A 28
7 % 29 B 29
& 30 C 30
* 31 D 31
8 32
33
34
35
9 36
37
38
39
10 40
41
42
43
11 44
45
46
47
12 U 48
V 49
W 50
X 51
13 Y 52
Z 53
@ 54
# 55
14 56
57
58
59
15 60
61
62
63
MEMORY MANAGEMENT PAGING
8: Memory Management 27
Example:
A process of size 2 GB with:
Page size = 512 Bytes
Size of page table entry = 4 Bytes, then
Calculate the size of the inverted page table if the main memory size is 128 MB and each entry in the
inverted page table is of size 4 Bytes.
Answer:
Number of entries in an inverted page table = 128 MB / 512 Bytes = 227 / 29 = 218
Inverted page table size = 218 * 22 = 220 Bytes
MEMORY MANAGEMENT Segmentation
USER'S VIEW OF MEMORY
global variables
procedure call stack
code for each function
local variables for each
large data structures
Segment Table
Limit Base
S D
CPU
Logical
Address Yes
< +
Physical MEMORY
No Address
8: Memory Management 31
MEMORY MANAGEMENT Segmentation
HARDWARE
base / limit pairs in a segment table.
Limit Base
0 1000 1400
1 400 6300 1
1 2 400 4300
3 1100 3200 4
2 4 1000 4700
0
3 2
4
3
8: Memory Management 32
MEMORY MANAGEMENT Segmentation
PROTECTION AND SHARING
0 1219 700
1 2300 14
2 90 100
3 1327 580
4 1952 96
Which of the following logical address will produce trap addressing error?
Calculate the physical address if no trap is produced.
1. 0, 430
2. 1, 11
3. 2, 100
4. 3, 425
5. 4, 95
Answer:
1. Offset = 430 < 700 => No trap. Physical Address = 1219(Base) + 430(offset) = 1649
4. Offset = 425 < 580 => No trap. Physical Address = 1327(Base) + 425(offset) = 1752
1. Segment Number specifies the specific segment from which CPU wants to reads the data.
2. Page Number specifies the specific page of that segment from which CPU wants to read the data.
3. Page Offset specifies the specific word on that page that CPU wants to read.
4. For the generated segment number, corresponding entry is located in the segment table.
5. Segment table provides the frame number of the frame storing the page table of the referred segment.
7. For the generated page number, corresponding entry is located in the page table.
8. Page table provides the frame number of the frame storing the required page of the referred segment.
10. The frame number combined with the page offset forms the required physical address.
11. For the generated page offset, corresponding word is located in the page and read.
MEMORY MANAGEMENT
WRAPUP
8: Memory Management 35
Memory Management Schemes
Best Buddy
1 Queue Fit System
1 Queue
per for all the
Partition Partitions
Consider the following segment table-
0 1219 700
1 2300 14
2 90 100
3 1327 580
4 1952 96
Which of the following logical address will produce trap addressing error?
Calculate the physical address if no trap is produced.
1. 0, 430
2. 1, 11
3. 2, 100
4. 3, 425
5. 4, 95
Answer:
1. Offset = 430 < 700 => No trap. Physical Address = 1219(Base) + 430(offset) = 1649
4. Offset = 425 < 580 => No trap. Physical Address = 1327(Base) + 425(offset) = 1752
1. Segment Number specifies the specific segment from which CPU wants to reads the data.
2. Page Number specifies the specific page of that segment from which CPU wants to read the data.
3. Page Offset specifies the specific word on that page that CPU wants to read.
4. For the generated segment number, corresponding entry is located in the segment table.
5. Segment table provides the frame number of the frame storing the page table of the referred segment.
7. For the generated page number, corresponding entry is located in the page table.
8. Page table provides the frame number of the frame storing the required page of the referred segment.
10. The frame number combined with the page offset forms the required physical address.
11. For the generated page offset, corresponding word is located in the page and read.
Virtual Memory
Basic Concept
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
FIFO Page Replacement
Belady’s Anamoly
z The number of page faults may increase with
increase in number of page frames for FIFO
z Counter-intuitive
z Consider 4 page frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
Belady’s Anamoly
Optimal Algorithm
z Replace page that will not be used for longest period of
time.
z 4 frames example: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
2 6 page faults
3
4 5
1 5
2
3 5 4
4 3
z Counter implementation
z Every page entry has a counter; every time page is
referenced through this entry, copy the clock into the
counter
z When a page needs to be changed, look at the
counters to determine which are to change
LRU Page Replacement
LRU Algorithm (Cont.)
z Prepaging
z Bring in pages not referenced yet
z TLB Reach
z The amount of memory accessible from the TLB.
z TLB Reach = (TLB Size) X (Page Size)
z Ideally, the working set of each process is stored in
the TLB. Otherwise there is a high degree of page
faults
Other Considerations (Cont.)
z Program structure
int A[][] = new int[1024][1024];
Each row is stored in one page
Program 1
for (j = 0; j < A.length; j++)
for (i = 0; i < A.length; i++)
A[i,j] = 0;
1024 x 1024 page faults!!
Program 2
for (i = 0; i < A.length; i++)
for (j = 0; j < A.length; j++)
A[i,j] = 0;
1024 page faults
Other Considerations (Cont.)
Head pointer 53
FCFS
z Version of C-SCAN
z Arm only goes as far as the last request in each
direction, then reverses direction immediately,
without first going all the way to the end of the
disk.
C-LOOK (Cont.)
Selecting a Disk-Scheduling
Algorithm
z SSTF is common and has a natural appeal
z SCAN and C-SCAN perform better for systems that
place a heavy load on the disk
z Performance depends on the number and types of
requests
z Requests for disk service can be influenced by the file-
allocation method
z The disk-scheduling algorithm should be written as a
separate module of the operating system, allowing it to
be replaced with a different algorithm if necessary
z Either SSTF or C-LOOK is a reasonable choice for the
default algorithm (depending on load)
Disk Management
z Low-level formatting, or physical formatting — Dividing a
disk into sectors that the disk controller can read and
write.
z To use a disk to hold files, the operating system still
needs to record its own data structures on the disk
z Partition the disk into one or more groups of cylinders
Muktikanta Sahu
April 2, 2019
Given-
Number of surfaces = 16
Number of tracks per surface = 128
Number of sectors per track = 256
Number of bytes per sector = 512 bytes
Capacity of disk pack
= Total number of surfaces x Number of tracks per surface x Number of
sectors per track x Number of bytes per sector
= 16 x 128 x 256 x 512 bytes
= 228 bytes
= 256 MB
If the format overhead is 32 bytes per sector, what is the formatted disk
space?
Formatting overhead
= Total number of sectors x overhead per sector
= 219 x 32 bytes
= 219 x 25 bytes
= 224 bytes
= 16 MB
Now, Formatted disk space = Total disk space – Formatting overhead
= 256 MB – 16 MB
= 240 MB
If the disk is rotating at 3600 RPM, what is the data transfer rate?