Operating Systems Summary
Operating Systems Summary
Chapter 1 ( introduction )
Chapter 2 ( process )
Chapter 3 ( Scheduling )
Chapter 4 ( Threads )
Chapter 5 ( Concurrency and Process Synchronization )
Chapter 6 ( Deadlock )
Chapter 7 ( Memory Management )
Chapter 8 ( Virtualization )
Chapter 1 ( introduction )
1.1 What is Operating System
Operating System is A program that acts as an intermediary between a user of a computer and the computer hardware.
Operating System = Kernel + System Programs
Kernel It's the core that provides basic services for all other parts of the OS.
Operating System is a :
resource allocator → manages all resources, Decides between conflicting requests for efficient and fair resource use.
control program → Controls execution of programs to prevent errors and improper use of the computer.
Operating System Controls and coordinates use of hardware among various applications and users
Application Program define the ways in which the system resources are used to solve the computing problems of the users
Users
operating systems 1
Bootstrap
Set of programs stored in ROM/EPROM contains procedures that handle the H/W devices
interrupts
interrupt is a signal sent to the CPU to indicate that an event has occurred
an internal event trigger by software ( TRAP ) e.g. system calls → means application needs a service from the OS. It calls a
service
interrupt handler
Interrupt transfers control to the interrupt service routine (ISR), through the interrupt vector then the ISR handles the event
interrupt vector → an array contains the addresses of all the service routines
I/O Structure
A general-purpose computer system consists of CPUs and multiple device controllers connected through a common bus.
The device controller is responsible for moving the data between the peripheral devices that it controls and its local buffer
storage.
operating systems 2
The operating system provides a device driver that understands the device controller and provides a uniform interface to
the device.
I/O Operations
3. Device controller transfers data between device and its local buffer.
Device controller transfers an entire block of data directly between its local buffer and main memory, with no intervention
by the CPU.
Device controller generates one interrupt per block, to inform the device driver rather than one interrupt per byte.
While the device controller is transferring a whole data block, the CPU is available to accomplish other work.
Storage Structure
Main Memory
is the only large storage media that the CPU can access directly.
Secondary Storage
Storage hierarchy
operating systems 3
Caching
Cache memory is a high-speed storage layer that temporarily stores frequently accessed data to improve overall processing
speed.
Caching refers to the use of high-speed memory to hold a copy of recently-accessed data assuming that it will be needed again
soon.
Batch → users submit jobs to the system in a batch, this jobs are processed sequentially without user interaction.
Time-sharing → allows multiple users to interact with the computer at the same time.
Real-time → used in computers that controls critical systems such as: control machinery, scientific instruments.
A general-purpose processor supports a complete instruction set. Therefore, it can execute user processes. ( CPU )
A special-purpose processor supports a limited instruction set. Therefore, it cannot execute user processes. ( include device-
specific processors, such as disk, keyboard, and graphics controllers. )
Multiprocessor System
Advantages include
2. economy of scale → sharing peripherals, mass storage and power supplies means less cost.
graceful reliability : the system's ability to continue providing service proportional to the level of surviving hardware.
fault tolerance : enable the system to detect and recover from hardware failures. if one processor fails, another can take over
without interrupting service.
Types of multiprocessing
all processors share the same memory and I/O resources each processer may have its own memory and I/O
operating systems 4
Clustered system
Usually sharing storage via a storage-area network (SAN), Provides a high-availability service which survives failures.
2. Symmetric clustering has multiple nodes running applications, monitoring each other.
Multiprogramming
multiple programs are loaded into memory, the CPU switch between a logical extension of multiprogramming in which CPU switches jobs so
them when one program is waiting for I/O operations. frequently.
the CPU is divided into small intervals ( time slices ) and each user / task gets a
share of the CPU for a short time before the CPU switches to the next one.
allow the user to interact with each job while it is running. allow multiple users
maximize CPU utilization but still no direct user interaction.
to interact with the system at the same time.
user mode
kernel mode
Mode bit → provided by hardware Provides ability to distinguish when system is running user code or kernel code.
The CPU restricts access to hardware resources and memory. has directed access to all system resources.
provide isolation between user application and the system to ensure that
if program crashed during the execution doesn’t crash the operating if program happens to crash the entire system would crash.
system.
in user mode when the program needs access to hardware / software it makes a call to the operating system telling that it needs
access ( system call ). after the call, the program is switched from user mode to kernel mode so that the program can use those
resources.
system call is a programmatic way in which computer program requests a service from the kernel.
operating systems 5
Chapter 2 ( process )
2.1 Process
a program in execution. ( Program becomes process when executable file loaded into memory )
Stack section : contains temporary data ( Function parameters, return addresses, local variables )
operating systems 6
Terminated: The process has finished execution
PCB stores all of the information required to maintain track of a process. Such as :
PCB
I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
CPU-bound process – spends more time doing computations; few very long CPU bursts
operating systems 7
characteristics of a good scheduling algorithm
2. The total number of jobs processed every hour should be as high as possible
Scheduling Queues
Device ( I/O )Queue Contains processes that are waiting for I/O operations to complete
Ready Queue Contains all processes that are ready to execute and waiting for CPU time
Waiting Queue Contains processes that cannot continue execution until a certain event occurs
it can issue an I/O request and then be placed in the I/O queue.
removed forcibly from the CPU due to an interrupt and be put back in the Ready queue.
Queueing Diagram
Schedulers
Short-term scheduler ( CPU scheduler )
operating systems 8
Selects which process should be executed next and allocates CPU
Medium-term scheduler
Remove process from memory, store on disk, bring back in from disk to continue execution swapping
Context Switch
Context switch Saves the current context of the current process in its PCB, Loads the saved context of the other process
scheduled to run.
Context switch time is overhead, the system does no useful work while switching
Process Creation
The creating process is called a parent process, the new processes are called children.
operating systems 9
Each of these new processes may create other processes.
2. The parent waits until some or all of its children have terminated.
Process Termination
A parent process may terminate its child using the abort system call.
Some reasons for doing so :
The parent is exiting and the operating systems does not allow a child to continue if its parent terminates ( cascading
termination System ).
operating systems 10
models of IPC
Shared memory
The communication is under the control of the users processes not the operating system.
Determining the form of the data and the location of shared data.
Ensuring that they are not writing to the same location simultaneously. ( Synchronization )
Message Passing
Direct Communication
A direct communication link has the following properties
Symmetric Asymmetric
both sender and receiver name each other only the sender names the receiver process
send(P, message): Send a message to process P send (P, message): Send a message to process P
receive(Q, message): Receive a message from process Q receive (id, message): Receive a message from any process
Indirect Communication
Messages are sent to and received from mailboxes (or ports)
operating systems 11
In indirect communication, send() and receive() are defined as:
Synchronization
Blocking (synchronous) send. The sending process is blocked until the message is received by the receiving process or by the
mailbox.
Non-blocking (asynchronous) send. The sending process sends the message and resumes operation, immediately.
Blocking (synchronous) receive. The receiver blocks until a message is available. It resumes operation only after receiving the
message.
Non-blocking (asynchronous) receive. The receiver retrieves either a valid message or a null and resumes operation,
immediately.
Chapter 3 ( Scheduling )
3.1 CPU and I/O Burst Cycle
Process execution consists of a cycle of CPU execution and I/O wait.
Process execution begins and ends with a CPU burst.
I/O burst : a time interval when a process uses I/O devices only.
operating systems 12
CPU scheduling decisions may take place when a process :
4. Terminates
Scheduler Latency → time it takes for the Scheduler to perform the select
3.3 Dispatcher
module gives control of the CPU to the process selected by the scheduler. its functions involves :
Switching context
jumping to the proper location in the user program to restart that program
Dispatch Latency → time it takes for the dispatcher to stop one process and start another running.
Throughput → number of processes that complete their execution per time unit. Maximize
Turnaround time → amount of time to execute a particular process ( time from submission to termination ). Minimize
T urnaroundtime = completiontime − arrivaltime
Waiting time → amount of time a process has been waiting in ready queue. Minimize
Waitingtime = T urnaroundtime − bursttime( CPU time required )
Response time → amount of time it takes from when a request is submitted until the first response is produced, not output.
Minimize
Responsetime = starttime − arrivaltime
Fairness → Give CPU to all processes in a fair manner.
Same as FIFO.
The process that requests the CPU first is allocated the CPU first.
The average waiting time is often quite long because of Convoy effect.
Convoy effect occur when processes wait for one process to get of the CPU, resulting in lower CPU and device utilization.
operating systems 13
FCFS example
Shortest-Job-First (SJF)
Associate with each process the length of its next CPU burst
Assign the CPU to the process that has the smallest next CPU burst
if the next CPU bursts of two processes are the same, then use FCFS
Non-preemptive SJF
The Scheduler must wait until the executing process completes its CPU burst.
preemptive SJF
If a new process arrives, the scheduler compares its CPU burst length to the remaining time of the currently
executing process.
operating systems 14
preemptive SJF example
The Real difficulty with the SJF algorithm is knowing the length of the next CPU request, We may not know but we can
predict.
Priority
SJF is a priority scheduling where priority is the predicted next CPU burst time
Non-preemptive Priority
preemptive Priority
The dispatched process continues running until finish or a comer with higher priority enters ready queue
operating systems 15
Example of Priority Scheduling ( preemptive )
A major problem with priority Scheduling is starvation ( low priority processes may never execute )
Solution to starvation is Aging increasing the priority of processes as time progresses
Round Robin ( RR )
Like FCFS scheduling, but each process gets a small unit of CPU time, called a time quantum or time slice
Running process is preempted and added to the end of the ready queue.
Scheduler selects the process at the head of the ready queue for execution.
Example of RR
operating systems 16
If the time quantum is extremely large If the time quantum is extremely small
The RR scheduler becomes a FCFS scheduler Too many context switches, which can degrade system performance
Time quantum should be large enough compared to the context switch time
Multilevel Queue
Suitable for the case where processes can be classified in different groups according to some process criteria (type, size,
priority,... )
divides the ready queue into multiple separate queues, each queue has its own scheduling algorithm
Foreground processes demand high response times and carry higher external priority, typically scheduled using Round Robin
(RR).
Background processes require less interaction and lower priority, scheduled using First-Come, First-Served (FCFS).
All processes in a higher-priority ( foreground ) queue are served first. Starvation my occur
The amount of time given to a queue is used to schedule the processes in that queue
a process enters a queue will remain there. System does not move it from queue to queue
If a process uses too much CPU, it will be moved to lower priority queue
If a process stay too long to get CPU, move it to higher priority queue ( Aging )
operating systems 17
number of queues
method used to determine which queue a process will enter when that process needs service
Chapter 4 ( Threads )
4.1 Introduction
Threads Called lightweight processes (LWPs) are independently scheduled parts of a single program.
A Thread Comprises
Thread id
Program counter
Register set
operating systems 18
Stack
Code section
data section
if a process has multiple threads of control, it can perform more than one task at a time. ( Kernels and Most modern
applications )
Responsiveness → multithreading an interactive application may allow a program to continue running even if part of it is blocked
or is performing a lengthy operation, thereby increasing responsiveness to the user.
Resource Sharing
Threads share the memory and the resources of the process to which they belongs.
allows an application to have several different threads of activity all within the same address space. ( efficiency , easy
interaction )
the benefits of multithreading can be greatly increased in a multiprocessor architecture, where each thread may be running in
parallel on a different processor.
a single threaded process can only run on one CPU, no matter how many are available
Identifying tasks divide program into tasks that can run in parallel
Balance ensure that these tasks perform equal work of equal value
Data splitting divide data between tasks that run on separate cores
operating systems 19
Concurrency
When two or more tasks can start, run, and complete in overlapping time periods )
Single core
Parallelism
Tasks are split into subtasks that are executed at the same time on different cores or processors.
Multiple cores
Types of Parallelism
Data parallelism distributes subsets of the same data across multiple cores, same operation on each.
Task parallelism distributing tasks across cores, each thread performing unique operation
operating systems 20
4.5 Multithreading Models
User threads
Three primary thread libraries : POSIX Pthreads , Windows threads , Java threads
Kernel threads
Multithreading Models
Many-to-One Model
only one user thread can access the kernel at a time , multiple threads are unable to run in parallel in multiprocessors
the entire process will block if a thread makes a blocking system call
One-to-One Model
multiple threads can access the kernel at a given time , multiple threads can run in parallel in multiprocessors
when a thread makes a blocking system call, another thread can run
Many-to-Many Model
multiple threads can access the kernel at a given time , multiple threads can run in parallel in multiprocessors
operating systems 21
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to kernel thread
Thread Cancellation
Deferred cancellation allows the target thread to periodically check if it should be cancelled
Invoking thread cancellation requests cancellation, but actual cancellation depends on thread state
If thread has cancellation disabled, cancellation remains pending until thread enables it
operating systems 22
Chapter 5 ( Concurrency and Process Synchronization )
Introduction
Concurrency
Concurrency can be viewed at different levels ( Principles of concurrency are similar across these categories )
Software view
multiprogramming : interaction between multiple processes running on one CPU ( pseudo parallelism )
Hardware view
multiprocessors : interaction between multiple CPUs running multiple processes / threads ( real parallelism )
Cooperating processes
Cooperating processes share a specific region of memory They can directly read from and write to this shared memory space (
Shared memory )
Example
operating systems 23
count++ could be implemented as count-- could be implemented as
Situations like this where processes access the same data concurrently and the outcome of execution depends on the particular
order in which the access takes place is called a Race condition
A Critical Section is a segment of code, in which the process may be accessing / updating data that is shared with other processes
The important feature of the system is that, when one process is executing in its critical section, no other process is to be
allowed to execute in its critical section
When a process executes code that manipulates shared data (or resource), we say that the process is in it’s critical section
operating systems 24
Requirements for a valid solution to the critical section problem
Mutual Exclusion : If process is executing in its critical section, then no other processes can be executing in their critical
sections.
Progress : only those processes that are not executing in their remainder sections can participate in deciding which will enter
its critical section next, and this selection cannot be postponed indefinitely
Bounded Waiting : After a process has made a request to enter it’s CS, there is a bound on the number of times that the other
processes are allowed to enter their CS
Types of solutions
Hardware solutions
Software solutions
Hardware Solutions
interrupt disabling
Uniprocessor System
Efficiency Degraded While a process is in its critical section, other processes that don’t need the critical section are also
blocked.
Multiprocessor System
Disabling interrupts only affects the processor executing the instruction. Other processors in a multiprocessor system can
continue executing may also access the shared resource
operating systems 25
Special hardware instructions are atomic ( execute as one uninterruptible unit )
There is a shared lock variable which can take either of the two values 0 (unlocked) , 1(locked)
Before entering into the critical section, a process inquire about the lock
if it is locked it keeps on waiting till it becomes free , if it is not locked it takes the lock and and executes the critical section
Assume first that the lock initial value = False ( The lock is open )
do {
// Try to acquire the lock (wait if already locked)
while (TestAndSet(ref lockPtr))
{
// Do nothing (busy-wait)
}
// Critical section
// Release the lock
lockPtr = false;
// Remainder section
} while (true);
Shared Lock Variable: Initialize a shared lock variable which can take values 0 (unlocked) or 1 (locked).
Compare and Swap Function: a function that takes three arguments: Value , the expected value , and the new value. It
atomically compares the lock's current value with the expected value. If they match, it sets the lock to the new value and
returns the original value.
Value ( the memory location of the lock ), the expected value ( 0 ) , and the new value ( 1 )
If the return value is 0, the process acquired the lock and can enter the critical section.
If the return value is 1, the lock is already held by another process, and the process continues to loop until the lock
becomes available.
operating systems 26
while (true)
{
while (CompareAndSwap(ref lockFlag, 0, 1) != 0)
; // do nothing ( busy - wait )
// Critical section
lockFlag = 0;
// Remainder section
}
Disadvantages
Starvation is possible when a process leaves a critical section and more than one process is waiting.
Deadlock
Peterson’s Solution
Peterson’s solution is restricted to two processes that alternate execution between their critical sections and reminder sections
bool flag[2] array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is
ready
Assume P( i ) is 1 and P( j ) is 2
while (true)
{
flag[i] = true;
turn = 2; // Give priority to process Pj
// to enter the critical section if it also wants access
while (true)
{
flag[i] = true;
turn = 1; // Give priority to process Pi
// to enter the critical section if it also wants access
operating systems 27
// Critical section
// Indicate that process Pj is leaving the critical section
flag[i] = false;
// Remainder section
}
Semaphore
Semaphore is simply a variable which is non-negative and shared between threads. this variable is used to solve the critical section
problem and to achieve process synchronization
A Semaphore S is an integer variable that is accessed only through two standard atomic operations wait( ) and signal ( )
wait( ) → P( )
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
signal ( ) → V( )
signal(S) {
S++;
}
wait( ) and signal( ) operations must be executed atomically. That is, when one process modifies the semaphore value, no
Types of Semaphore
Binary semaphore
integer value can range only between 0 (resource is unavailable) and 1 (resource is available). one resource
Semaphore S; // initialized to 1
wait(S);
// Critical Section
signal(S);
Counting Semaphore
Counting semaphores can be used to control access to a give resource consisting of a finite number of instances
When the count for the semaphore goes to 0, all resources being used. After that, processes that wish to use a resource it will be put
in a blocked queue of processes waiting for the same event until the count becomes greater than 0 To avoid busy waiting.
operating systems 28
Classical Problems of Synchronization
There is a buffer of n slots each slot is capable of storing one unit of data.
The Producer tries to insert data into an empty slot of the buffer
The consumer tries to remove data from a filled slot in the buffer
The Problems
The Producer must not insert data when the buffer is full.
The Consumer must not remove data when the buffer is empty.
The Producer and Consumer should not insert and remove data simultaneously.
1. Count Solution
while (true) {
// produce an item and put in nextProduced
while (count == BUFFER_SIZE)
; // do nothing
operating systems 29
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE; // circular buffer
count++;
}
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
// consume the item in nextConsumed
}
m(mutex) a binary semaphore which is used to acquire and release the lock
empty a counting semaphore whose initial value is the number of slots in the buffer, since initially all slots are empty
do {
wait (empty); // wait until empty > 0 then decrement empty
wait (mutex); // acquire lock
// add the item to the buffer
signal (mutex); // release lock
signal (full); // increament full
} while (true);
do {
wait (full); // wait until full > 0 then decrement full
wait (mutex); // acquire lock
// remove an item from buffer
signal (mutex); // release lock
signal (empty); // increment empty
} while (true);
Readers only read the data set; they do not perform any updates
if two readers access the shared data simultaneously, no affects will result
if a writer and some other ( reader or writer ) access the data simultaneously, a chaos may ensue
mutex a semaphore ( initialized to 1 ) which is used to ensure mutual exclusion when readcount is updated
readcount an integer variable ( initialized to 0 ) that keeps track of how many processes are currently reading the
object
operating systems 30
do {
// writer requests for critical section
wait(wrt);
// writing is performed
signal(wrt); //releasing the wrt
} while (true)
do {
wait(mutex); // Lock to update readcount
readcount++; // increase the number of readers
if (readercount == 1) {
wait(wrt); // First reader blocks writing or If there is a writer already
}
signal(mutex) // release the lock so other readers can enter
// reading is performed
wait(mutex); // Lock to update readcount
readcount--; // a reader wants to leave
if (readcount == 0) {
signal(wrt); // writers can enter if there is no readers left
}
signal(mutex); // release the lock
} while(true)
Dinning Philosopher
Philosopher either
Thinks → when a philosopher thinks, he does not interact with his colleagues
Eats
when a philosopher gets hungry he tries to pick up the two forks that are closest to him ( Left & Right )
when he has finished eating, he puts down both of his forks and start thinking again
operating systems 31
A philosopher tries to grab a fork by executing a wait( ) operation
Thus the shared data are Semaphore chopsticks[5] which are all elements of chopsticks initialized to 1
Do {
wait (chopstick[i]);
wait (chopStick[(i + 1) % 5]);
// eat
signal (chopstick[i]);
signal (chopstick[(i + 1) % 5]);
// think
} while (true) ;
/* Although this solution guarantees that no two neighbors are eating simultaneously
it could still create a deadlock */
preempts a process while it is running in kernel mode allows a process to run until it exits kernel mode, block
Must be carefully designed to ensure that kernel shared data Because there is max one process in kernel mode, it is free from
are free from Race conditions Race conditions
Chapter 6 ( Deadlock )
Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource
acquired by some other process.
request
use
release
Deadlock Conditions
Hold and wait : a process holding at least one resource is waiting to acquire additional resources held by other processes
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.
Resource-Allocation Graph
P = {P1, P2, …, Pn} / T = {T1,T2,…,Tn} the set consisting of all the processes / Threads in the system
R = {R1, R2, …, Rn}, the set consisting of all resource types in the system
edges E
operating systems 32
assignment edge – directed edge Rj → Ti
Example
deadlock vs no deadlock
operating systems 33
Methods for Handling Deadlocks
Ignore the problem and pretend that deadlocks never occur in the system
Deadlock Prevention
Deadlock Avoidance
Deadlock Prevention
Mutual Exclusion
1. Require process to request and be allocated all its resources before it begins execution
2. allow process to request resources only when the process has none allocated to it
No Preemption
If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all
resources currently being held are released
Preempted resources are added to the list of resources for which the process is waiting
Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting
Circular Wait
Impose a specific order for all resource types, and ensure that each process requests resources in that order
Deadlock Avoidance ( Requires that the system has some additional a priori information available )
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 no circular wait can
occur
Safe State
When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state
A system is in a safe state if there exists a sequence of processes <P1,P2,…,Pn> such that for each process Pi :
The resources that Pi can still request can be satisfied by the currently available resources plus the resources held by
all previous processes Pj, where j < i
If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate
operating systems 34
When Pi terminates, Pi +1 can obtain its needed resources, and so on
Deadlock Detection
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
operating systems 35
Recovery from Deadlock
Process Termination
2. How long process has computed, and how much longer to completion
Resource Preemption
Selecting a victim : which resources and which processes are to be preempted ? ( 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
operating systems 36
Base and Limit Registers
operating systems 37
base and limit registers define the logical address space for a process
When a logical address is generated, the value from the base register is added to it to convert it into a physical address
Together, they ensure memory protection, separating each process’s memory space from others to avoid unauthorized memory
access
The base register holds the starting address of a process's memory segment.
Address Binding
Logical address
Physical address
The value in the relocation register ( base register ) is added to every address generated by a user process at the time it is sent to
memory
The user program deals with logical addresses, it never sees the real physical addresses
operating systems 38
Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued
execution
Backing store : fast disk large enough to accommodate copies of all memory images for all users
Roll out, roll in : swapping variant used for priority-based scheduling algorithms
System maintains a ready queue of ready-to-run processes which have memory images on disk
I/O problem
Assume that the I/O operation is queued because the device is busy. If we were to swap out process P1 and swap in process P2,
I/O operation might then attempt to
use memory that now belongs to process P2
Solution
operating systems 39
Contiguous Allocation
Resident operating system, usually held in low memory with interrupt vector
Relocation registers ( Base and Limit ) used to protect user processes from each other and from changing operating-system code
and data
MMU maps logical address dynamically, Can then allow actions such as kernel code being transient and kernel changing size
Multiple-partition allocation
When a process arrives, it is allocated memory from a hole large enough to accommodate it
allocated partitions
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
operating systems 40
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
Shuffle memory contents to place all free memory together in one large block
EXAMPLE
operating systems 41
Logical address space of a process can be noncontiguous
Segmentation ( non-Contiguous )
main program, procedure, function, method, object, local variables, global variables, common block, stack, symbol table,
arrays
Segmentation Architecture
Segment table maps two-dimensional physical addresses <segment-number, offset> each table entry has:
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
Sharing
Allocation
EXAMPLE
operating systems 42
Physical Address = Base + Offset ( if offset < Length / Limit )
Paging ( non-Contiguous )
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 ( OS maintains a page table for each process )
internal fragmentation → allocated memory may be slightly larger than requested memory. this size difference is memory internal
to a partition, but not being used
operating systems 43
Logical Address is divided into :
Page number ( p ) – used as an index into a page table which contains base address of each page in physical memory
Page offset ( d ) – combined with base address to define the physical memory address that is sent to the memory unit
Page table Contains the frame location for each page in the process
Paging hardware
operating systems 44
Problem
Assuming a 1-KB page size, what are the page numbers and offsets for the following address references (provided as decimal
numbers):
a. 3085
b. 42095
Answer
operating systems 45
Shared Pages
Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems).
Shared code must appear in same location in the logical address space of all processes
The pages for the private code and data can appear anywhere in the logical address space
In this scheme every data/instruction access requires two memory accesses. ( One for the page table and one for the data/instruction
)
can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers
(TLBs)
parallel search
The system retrieves the frame number from the page table in main memory
operating systems 46
Page Size
Smaller page size, less amount of internal fragmentation But more pages required per process means larger page tables
Hierarchical Paging
A logical address (on 32-bit machine with 4K page size) is divided into:
Since the page table is paged, the page number is further divided into:
p2 is the displacement ( offset ) within the page of the outer page table ( a 10-bit page offset )
logical address
Address-Translation Scheme
operating systems 47
Two-Level Page-Table Scheme
A logical address (on 64-bit machine with 8K page size) is divided into 3 levels :
The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location
Virtual page numbers are compared in this chain searching for a match
operating systems 48
Inverted Page Tables
One entry for each real page of memory ( frame ) instead of one entry foreach virtual page
Entry consists of the virtual address of the page stored in that real memory location, with information about the process that
owns that page
Decreases memory needed to store each page table ( Only one page table in the system ), but increases time needed to search
the table when a page reference occurs
Use a hash table to limit the search to one or, at most, a few page-table entries
Demand Paging
Why ?
How ?
Process generates logical (virtual) addresses which are mapped to physical addresses using a page table
operating systems 49
If the requested page is not in memory, kernel brings it from hard disk by making a page fault
Valid-Invalid Bit
Memory protection implemented by associating protection bit with each frame Valid-invalid bit
Each page table entry has a valid–invalid bit ( Initially, it is set to i for all entries )
v → in-memory
i → not-in-memory
it could be :
Legal reference but not in memory → page fault (bring the page from the disk
Page fault
When a process references a page that hasn't been brought into memory, the first reference causes a page fault ( trap to operating
system )
4. Reset tables
operating systems 50
Handling a Page Fault
Page Replacement
is used to find some page in memory, but not really in use, then swap it out
If there is no free frame, use a page replacement algorithm to select a victim frame
3. Bring the desired page into the (newly) free frame. update the page and frame tables
operating systems 51
It Replaces page that has been in memory the longest ( oldest page ).
it focuses on the length of time a page has been in memory rather than on how much the page is being use.
Its behavior is not particularly well suited to the behavior of most programs. Few programs use it.
Example
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
remove 1 ( oldest
pages remove 2 remove
page )
1 1 1 1 4 4 4
2 - 2 2 2 1 1
3 - - 3 3 3 2
9 page faults
4 frames
pages
1 1 1 1 1 1 1
2 - 2 2 2 2 2
3 - - 3 3 3 3
4 - - - 4 4 4
10 Page faults
Advantages
Disadvantages
It replace page that will not be used for longest period of time
Optimal Algorithm is difficult to implement, because it requires future knowledge of the reference string
has the lowest page-fault rate of all algorithms and will never suffer from Belady’s anomaly.
Example
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
4 Frames
pages
1 1 1 1 1 1 1
2 - 2 2 2 2 2
3 - - 3 3 3 3
4 - - - 4 4 4
6 Page faults
operating systems 52
Least Recently Used (LRU) Algorithm
Replace page that has not been used in the most amount of time.
Example
12 Page faults
Paging vs Segmentation
Paging Segmentation
memory is partitioned into fixed-size pages memory is partitioned into the variable size segments.
with the help of hardware page size is determine the user gives the size of the segments
there may be a chance of internal fragmentation there may be a chance of external fragmentation
the logical address is partitioned into the page number and the the logical address is partitioned into the Segment number and the
page offset Segment offset
The MULTICS system (Multiplexed Information and Computing Service) solved problems of external fragmentation and lengthy
search times by paging the segments.
The solution differs from pure segmentation in that the segment-table entry contains not the base address of the segment, but rather
the base address of a page table for this segment.
operating systems 53
Chapter 8 ( Virtualization )
Definition
Fundamental idea behind a virtual machine is to abstract hardware of a single computer ( CPU, Memory,…) into several different
execution environments.
creating the illusion that each environment is running on its own private computer.
Virtualization can be seen as a layered approach, where a layer creates virtual systems on which operating systems or applications
can run.
Components
Host : The physical hardware system
Virtual machine manager VMM / hypervisor : Creates and runs virtual machines (VMs) by providing an interface identical to the
host
A single physical machine can run multiple operating systems concurrently, each in its own virtual machine.
Types of Hypervisors
Type 0 hypervisors Type 1 hypervisors Type 2 hypervisors
Runs directly on physical hardware Runs directly on physical hardware Runs on top of a host operating system
Direct interaction with hardware, no host OS Direct interaction with hardware, no host OS Interacts with hardware indirectly through the host
required required OS
Advanced features like live migration, resource Easy to install and use, no changes required to the
Can run other hypervisors inside a VM.
balancing, Cloning and snapshots. host OS.
operating systems 54
Benefits and features
Isolation and Security : Host system is protected from VMs and VMs are protected from each other. Viruses are less likely to
spread.
Flexibility
Snapshots can be taken of a system’s state, allowing for restoration back to that point.
Templating : You can create an OS + application VM, provide it to customers, use it to create multiple instances of that
combination
Live Migration : Running VMs can be moved between hosts with no interruption of user access
Virtualization Techniques
Paravirtualization
Technique in which the guest operating system is modified to work in cooperation with the VMM to optimize performance.
where the guest OS interacts with virtualized hardware as if it's real hardware, in paravirtualization, the guest OS is aware of the
virtualization layer.
Programming-environment virtualization
Instead of virtualizing hardware, VMMs create an optimized virtual system for specific languages.
Emulators
Allow applications written for one hardware environment to run on a very different hardware environment, such as a different
type of CPU.
Application containment
Provides virtualization-like features by segregating ( ) فصل او عزلapps from the OS for improved security and
manageability.
SO
Most VMMs implement a Virtual CPU (VCPU) to represent the state of the CPU for each guest .
The VMM loads and stores the information in the VCPU when switching a guest's context onto the physical CPU.
Emulation
the virtualizing software mimics( ) يقلد أو ُي حاكيhardware portions for the guest OS in the VM.
because every instruction from the guest OS is translated by the VMM to work on the physical hardware.
takes the basic of the emulation but improves performance by using interpretation selectively.
The guest OS runs in user mode, but when it tries to access kernel mode, it triggers a "trap" to the hypervisor.
operating systems 55
Instead of interpreting every instruction like in emulation, only privileged instructions that require kernel mode access are
intercepted and handled by the hypervisor.
Binary Translation
Solves issues with CPUs that lack clean separation between privileged and non-privileged instructions.
Special instructions, like popf , behave differently in user and privileged modes and don't generate traps, making them
unsuitable for Trap and Emulate
special instructions are translated into equivalent instructions ( that can be executed directly ) without waiting for a trap
Hardware Assistance
The CPU manages VCPU and performs context switching between the guest and host.
operating systems 56